Running pyinstaller on code containing relative paths

1,398 views
Skip to first unread message

Anthony Nash

unread,
Jan 29, 2023, 7:01:20 AM1/29/23
to PyInstaller
Hi all,

I have a large python codebase developed inside PyCharm. I want to use PyInstaller for obvious reasons; however, I'm struggling with relative paths for data files due to the project code file hierarchy.

The file hierarchy was a usual top-down structure, i.e., the point of execution is within a file found in the project root folder, with the additional python files stored in a sensible folder, (please pay particular attention to the version.txt file on the same level as the Main.py file) e.g.,

Project/
--Main.py
--version.txt
--Engines/
----somefile.py
--Controllers/
----somefile.py
--Entities/
----somefile.py

A year ago, I built a GUI front end whilst maintaining the console-based point of execution. The GUI point of execution is within MainGUI.py. But that file is not at the project root. It looks a bit like this:

Project/
--Main.py
--version.txt
--GUI/
----MainGUI.py
--Engines/
----somefile.py
--Controllers/
----somefile.py
--Entities/
----somefile.py

Inside MainGUI.py, I have the code to open the "../version.txt" file:

with open("../version.txt") as file: version = file.readline().strip()

I navigate to the Project/GUI folder in the PyCharm Terminal and execute pyinstaller MainGUI.py --onefile It seems to work until I try and execute the built MainGUI.exe. I'm given the error:

Traceback (most recent call last): File "MainGUI.py", line 10, in <module> FileNotFoundError: [Errno 2] No such file or directory: '../version.txt' [17232] Failed to execute script 'MainGUI' due to unhandled exception!

I could move the version.txt file to be on the same level as MainGUI.py, but this was a reduced example. There are lots of data files referenced using relative paths.

I would be grateful for any assistance. Thank you.

Jasper Harrison

unread,
Jan 29, 2023, 7:35:35 AM1/29/23
to pyins...@googlegroups.com
Anthony,

Our documentation covers how to solve this. Link below.
https://pyinstaller.org/en/stable/runtime-information.html#using-file

Jasper Harrison, aka Legorooj
Core Developer on PyInstaller


-------- Original Message --------
--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/5e84c822-3872-452c-bea9-7851d94a32e9n%40googlegroups.com.
publickey - EmailAddress(s=legorooj@protonmail.com) - 0x164CF234.asc
signature.asc

Chris Barker

unread,
Jan 31, 2023, 4:28:25 AM1/31/23
to pyins...@googlegroups.com
The solution here isn't PyInstaller specific -- it's about Python, paths, relative paths, adn the current working dir.

A few notes:

Python respects the "current working directory" -- if you start a script from the comamnd line, the cwd will be teh dir you started it in. If you start it by point and clicking on a PyInstaller (or any other app), then it's up to the app builder to make sure it' set to something sensible -- but what that is depends on the app, etc.

when you call open() with a relative path, like: 

open("../version.txt")

Then it will look for it relative to the cwd, which is only sometimes what you want.

Solution: Python has a __file__ dunder for modules, so if you want to find a file relative to the *current module file", you do that by using file -- ( I like pathlib, but that's not necessary )

from pathlib import Path

verfile = open(Path(__file__).parent.parent  / "version.txt")

(untested)

Design note:

I usually only use the above pattern in tests and the like -- in production code, I would either:

- put your version info in a python file (I usually put it in __init__.py)
- have a place for data files that is configured at program start up, and may be different depending on how the app is deployed.

But for one simple file, the above is fine, as long as you make sure the file is copied / installed where it is needed.

HTH,
-CHB







--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov
Reply all
Reply to author
Forward
0 new messages