2

I have a python project that has a following files tree:

\main_dir
    gui.py
    data.py
    \lib
        \files
            file1.txt
            file2.txt
     ... more_dirs and files in lib.

The gui.py imports data.py. data.py parses file1.txt as part of it constructor. I want to run gui.py as an executable in windows and therefore use pyinstaller.

data.py opens file1.txt whlie using a relative path: file1_dir = os.path.join(os.path.curdir, "lib", "files")

I run the following command:

pyinstaller "..fullpath..\main_dir\gui.py" -p "..fullpath..\main_dir\" --runtime-hook "..fullpath..\main_dir\lib"

The pyistaller succesfully package data.py but when run the executable file I get the following error:

"FileNotFoundError: the system cannot find the path specified: '.\lib\files\'

I tried to change the hook to be <fullpath>\main_dir\lib\files but got the same error.

What am I doing wrong? How do I add relative dir & files to the executable?

NirMH
  • 4,769
  • 3
  • 44
  • 69

1 Answers1

1

If I understand correctly you don't need to use --runtime-hook, it's for running another script before your main script starts, for example if you add --runtime-hook=file1.py in your command, the execution order at run time would be: 1) run file1.py, 2) run your main script. (Of course they are compiled/packed into an exe file already)

So in your case, you use gui.py to import data.py, and data.py operates on files in lib. You don't need to include lib in your pyinstaller command, just put it into same folder with the compiled exe file, it'll automatically look for files as it does in your data.py; if you want you could include them (files in lib) as datas in your spec file, this way those files will be copied to target folder (same folder where the exe file will be at) on compilation, but it's really not necessary.

Also remove -p since all your scripts (gui.py and data.py) are in same folder, no need to tell pyinstaller to search for imports in other places.

Shane
  • 2,231
  • 1
  • 13
  • 17