I have created an executable Python script with pyinstaller that reads txt files with Pandas.

When I am running it through terminal it works fine, but when I double click it, console appears, and disappears immediately. I have also change the .spec file by defining datas=["./f.txt"],hiddenimports=['pandas','os','numpy'] Python script and f.txt are under the same directory.

Below is the script:

#!/usr/bin/python2
import os.path
import pandas as pd
import numpy as np

myfile = os.path.join(os.path.dirname(__file__), './f.txt')

df = pd.read_csv(myfile,sep='\s+,|\t',skiprows=[0,1,2,3,4,5,6,7,8,9,10,11,12], engine='python')

print df
input("Hit enter to close.")
1

There are 1 best solutions below

2
valcarcexyz On

When using pyinstaller, it will be run from 'INSTALLATION_DIR/_internal' folder, so most likely it cannot find the 'f.txt' file, you can verify it by running it from the terminal, and should find something like:

Traceback (most recent call last):
  File "a.py", line 7, in <module>
  File "pandas/io/parsers/readers.py", line 1026, in read_csv
  File "pandas/io/parsers/readers.py", line 620, in _read
  File "pandas/io/parsers/readers.py", line 1620, in __init__
  File "pandas/io/parsers/readers.py", line 1880, in _make_engine
  File "pandas/io/common.py", line 873, in get_handle
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/dist/a/_internal/./f.txt'
[38960] Failed to execute script 'a' due to unhandled exception!

If this is your case, you have 2 possible solutions:

  1. Add it manually to the _internal folder
  2. When 'compiling', add it as a data source:
pyinstaller --add-data f.txt:. script.py