Path relative to exe file location

25 views
Skip to first unread message

Judd Gledhill

unread,
Jun 4, 2025, 4:17:12 PMJun 4
to PyInstaller
Hello,

I am converting some small scripts to exe files to host on a Citrix server. I am hoping to be able to get the path to the .exe file so I can traverse to folders next to the exe file itself. Right now I use:

script_path = os.path.abspath(__file__) #path to this script

But this fails at runtime with the exe file. I read the notes about __file__ but do not want to bundle the files into the exe itself. How can I find the path to the exe that is running?

Thanks,

Judd

Chris Barker

unread,
Jun 4, 2025, 5:36:51 PMJun 4
to pyins...@googlegroups.com
I think you need to special-case when it's frozen:

# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
elif __file__:
    application_path = os.path.dirname(__file__)
from:

https://stackoverflow.com/questions/404744/determining-application-path-in-a-python-exe-generated-by-pyinstaller

--
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 visit https://groups.google.com/d/msgid/pyinstaller/84bc6f62-3d54-4aef-b6ee-ca8f667f2113n%40googlegroups.com.


--

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

Judd Gledhill

unread,
Jun 5, 2025, 3:50:02 PMJun 5
to PyInstaller
Thanks Christopher. That worked a treat, though that Stackoverflow article got s bit confusing when you read down it a bit. Thank you for the help.

Judd

bwoodsend

unread,
Jun 7, 2025, 6:43:45 AMJun 7
to PyInstaller

though that Stackoverflow article got s bit confusing when you read down it a bit

Ughh, that’s mostly because every answer on there is wrong. (At least now I know why those snippets keep appearing on the issue tracker.)

Note that you’re specifically looking for the executable path whereas that Stackoverflow question is asking about application resources so sys.executable is right for your use case but wrong for what that question is about.

bwoodsend

unread,
Jun 7, 2025, 8:03:21 AMJun 7
to PyInstaller

Actually, on rereading, it looks like the question was specifically asking the external files next to the application question. It’s just that most of the answers were for the wrong question.

Reply all
Reply to author
Forward
0 new messages