2

In my python file, the "df = pd.read_csv('table.csv')" bit causes a FileNotFoundError even though it resides in the same directory. I run my executable file that I built with pyinstaller.

import tkinter as tk
import pandas as pd

df = pd.read_csv('table.csv')

When I attempt to build the exe with pyinstaller I use the following code in terminal:

pyinstaller --onefile --add-data 'table.csv:.' gui_calc.py

When I run the executable file...

Traceback (most recent call last):
  File "gui_calc.py", line 9, in <module>
  File "pandas/io/parsers.py", line 686, in read_csv
  File "pandas/io/parsers.py", line 452, in _read
  File "pandas/io/parsers.py", line 936, in __init__
  File "pandas/io/parsers.py", line 1168, in _make_engine
  File "pandas/io/parsers.py", line 1998, in __init__
  File "pandas/_libs/parsers.pyx", line 382, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas/_libs/parsers.pyx", line 674, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] No such file or directory: 'table.csv'
[58900] Failed to execute script gui_calc

[Process completed] 

My environment is:

Mac OSX 10.15.6

Python 3.7.7

pandas Version: 1.1.1

zsh 5.7.1

pyinstaller Version: 4.1.dev0

  • 1
    Did you copy the exe from `dist` folder to the project directory? I dont think `--add-data 'table.csv:.'` this will actually allow you to store the csv within in – Delrius Euphoria Oct 19 '20 at 13:20
  • 1
    This worked! But only when I execute the file via terminal in the file directory typing `./gui_calc` – Brian Rolly Oct 19 '20 at 14:53

1 Answers1

2

Based on this answer pyinstaller in --onefile mode creates a temporary directory every time you run the executable and stores its path in sys._MEIPASS variable. When accessing the CSV file in your code, you need to refer to this path.

For example (modified code from answer I mentioned above):

import os, sys

try: # running using executable
    path = sys._MEIPASS

except: # running using .py sript
    path = os.path.abspath('.')

csv_path = os.path.join(path, 'table.csv') # valid path of the csv file

For more info see: How the One-File Program Works, Defining the Extraction Location, Run-time Information

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Theobaldus
  • 103
  • 7