Search code examples
pythonpython-3.xwindowspyinstallerexecutable

Including multiple folder to make standalone executable with pyinstaller


I'm trying to make a standalone executable from my python project, but I'm unable to make it work. Below is complete detail....

My project structure look something like this ( in project root dir )

csab:
  2022:
    round1:
      some_files.csv
tool.py

Since I wanted to create standalone executable form it, and ofcrs i need those csv files as its contain data which is required by my program so i used pyinstaller for this purpose.

On windows i tried following command.

pyinstaller --noconfirm --onefile --console --add-data "E:/python/jee_counsellor/csab;csab/" --add-data "E:/python/jee_counsellor/josaa;josaa/"  "E:/python/jee_counsellor/tool.py"

And on linux, i tried with this

python -m PyInstaller --onefile --add-data 'csab/*:csab' --add-data 'josaa/*:josaa' --exclude-module _tkinter tool.py

But none of them seems to work as it cant find my csv paths correctly. Here is the error details ( same on both linux and windows )

Traceback (most recent call last):
  File "tool.py", line 250, in <module>
  File "tool.py", line 16, in pre_setup
  File "tool.py", line 36, in csab_rounds
  File "tool.py", line 54, in csv_files
  File "pandas\io\parsers\readers.py", line 912, in read_csv
  File "pandas\io\parsers\readers.py", line 577, in _read
  File "pandas\io\parsers\readers.py", line 1407, in __init__
  File "pandas\io\parsers\readers.py", line 1661, in _make_engine
  File "pandas\io\common.py", line 859, in get_handle
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\python\\jee_counsellor\\output\\csab\\2022\\round_2\\ranks.csv'
[19596] Failed to execute script 'tool' due to unhandled exception!

Snippet from my code where it fails.

        cwd = os.getcwd()
        csab_rounds = round
        csv_path = os.path.join(cwd, "csab", "2022", f"round_{csab_rounds}", "ranks.csv")
        df = pd.read_csv(csv_path)

Do I need to update my code or any other workaround for this with pyinstaller ?


Solution

  • Your build command has a slight mistake. On Linux, it should be

    python -m PyInstaller --onefile --add-data 'csab:csab' --add-data 'josaa:josaa' --exclude-module _tkinter tool.py.

    Notice the --add-data 'josaa:josaa'.

    From the documentation,

    1. The first string (josaa here) specifies the file or files as they are in this system now.
    2. The second string (again josaa here) specifies the name of the folder to contain the files at run-time.

    You do not need to write /*, the entire content of the folder will be automatically included.

    Link to documentation: https://pyinstaller.org/en/stable/usage.html#cmdoption-add-data

    Recommended way would be to use a spec file.

    https://pyinstaller.org/en/stable/spec-files.html#adding-data-files