0

I'm trying to make an executable from a python script, and I found many good reviews of PyInstaller.

My script contains some pandas instruction to read a csv file. I tried adding the file to the bundle by running:

pyinstaller --add-data "data.csv:." test.py

The executable is created fine but when I try to open it, I get the following error:

Traceback (most recent call last):
  File "test.py", line 20, in <module>
  File "test.py", line 10, in __init__
  File "test.py", line 13, in play
  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 946, in __init__
  File "pandas/io/parsers.py", line 1178, in _make_engine
  File "pandas/io/parsers.py", line 2008, 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: 'data.csv'
[16046] Failed to execute script test
Saving session...completed.

[Process completed]

My code:

#!/usr/bin/env python
# coding: utf-8

import pandas as pd
import numpy as np

class MyGame():

    def __init__(self):
        self.play()

    def play(self):
        data = pd.read_csv("data.csv")
        with pd.option_context('display.max_rows', None, 'display.max_columns', None):
            print(data)
        input()


if __name__ == "__main__" : 
    MyGame()

And my project folder

test_pyinstaller
 ├───test.py
 └───data.csv

What am I missing?

UPDATE:

So I added this command in my code: os.getcwd() to find out why the script couldn't find the csv file and it turns out the working directory was the home folder of my computer and not the one where the script was located. How do I correct this to point the working directory to the actual script location? Thanks.

Fabio Magarelli
  • 1,031
  • 4
  • 14
  • 47
  • 1
    What operating system are you using? –  Jan 13 '21 at 17:11
  • Your wording implies you get that error when running pyinstaller - but the error message says it is when you run the generated exe - which is it? Try reading the documentation e.g. https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html - refer to this documentation for how to find bundled files at runtime https://pyinstaller.readthedocs.io/en/stable/runtime-information.html – DisappointedByUnaccountableMod Jan 13 '21 at 17:23
  • @barny Yeah I'm sorry the error is when I try to run the executable file generated with pyinstaller, I'm updating my question. Thanks – Fabio Magarelli Jan 14 '21 at 08:30
  • 1
    @tdelozie I'm on Mac – Fabio Magarelli Jan 14 '21 at 08:31

2 Answers2

1

I prefer to develop code on Linux-based operating systems, but I have found it easier to use auto-py-to-exe in a windows environment using Anaconda. auto-py-to-exe is also a GUI tool that allows you to easily configure your .exe file as needed. It should work on a virtual Windows virtual machine as well.

Link: https://pypi.org/project/auto-py-to-exe/

  • 1
    thanks I'll have a look and I'll let you know if it worked for me. – Fabio Magarelli Jan 13 '21 at 17:21
  • Hi there, sorry for the delay. I still get the same error. Could it be that the path of the csv file is not correct when translated in the bundle? e.g. I should not access the file like this: `data = pd.read_csv("data.csv")`?? – Fabio Magarelli Jan 14 '21 at 08:51
0

I found a solution thanks to this other post on StackOverflow: Determining application path in a Python EXE generated by pyInstaller

SOLUTION:

#!/usr/bin/env python
# coding: utf-8

import pandas as pd
import numpy as np
import os
import sys

class MyGame():

    def __init__(self):
        # get script directory where also the csv file is.
        if getattr(sys, 'frozen', False):
            application_path = sys._MEIPASS
        else:
            application_path = os.path.dirname(os.path.abspath(__file__))

        self.path = application_path
        self.play()

    def play(self):
        print(self.path)
        print(self.path + "/data.csv")
        data = pd.read_csv(self.path + "/data.csv")
        with pd.option_context('display.max_rows', None, 'display.max_columns', None):
            print(data)
        input()


if __name__ == "__main__" : 
    MyGame()
Fabio Magarelli
  • 1,031
  • 4
  • 14
  • 47