Convert Python script to exe / command executable but be able to read from an editable .env file

200 views
Skip to first unread message

Jimmy McMahon

unread,
May 31, 2023, 10:28:29 AM5/31/23
to PyInstaller

Good afternoon all.

I hope that you are well.

I have a Python script that I would like to share with others, however it relies on the executable file being able to read a value from a .env file stored within the same location as where the executable is being ran from. I am running Mac OSX.

Here is some example code :-

import os from dotenv
import load_dotenv

load_dotenv()

test_environment_variable = os.environ.get("TEST_ENVIRONMENT_VARIABLE") print(test_environment_variable)


The aim is to convert my script into a command executable file and then for it to be able to read from an editable .env file within the same directory as the executable file. If the .env file is edited and saved, the next time the executable is ran, the output will be the contents of the newly saved .env file.

I have converted my script to an executable using the following command :-

pyinstaller --onefile main.py

This results in a dist folder being created with the executable file in there. If I place the .env file in the same directory as the executable file, then the output of the exe returns "None" whereas I would expect it to return "This is a new test environment variable" as per my .env file contents :-

TEST_ENVIRONMENT_VARIABLE = "This is a new test environment variable"

I can only assume that when the command application / executable file cannot find the .env path that is within the same directory as it and that this issue is due to the a problem with absolute vs relative paths. Do I need to run pyinstaller with different switches in order to be able to make the executable "aware" of the path to look at for the .env file at any time it is loaded (it will always be in the same directory as the executable file in my use case)? Alternatively, does my code need to be amended to be more definitive about the .env file location?

Any help would be greatly appreciated.

Many thanks James

Adrià Cereto i Massagué

unread,
Jun 2, 2023, 4:44:54 AM6/2/23
to pyins...@googlegroups.com
How are you determining where to look for the .env file?
IIRC you can determine the location of the executable being called by inspecting sys.argv and extracting it from there and turning it into an absolute path and resolving symlinks to avoid problems.

Adrià Cereto i Massagué


Missatge de Jimmy McMahon <jimmymcma...@gmail.com> del dia dc., 31 de maig 2023 a les 16:28:
--
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/50b7392d-cbb1-40bc-b038-5900b3253df3n%40googlegroups.com.

Vishesh Mangla

unread,
Jun 2, 2023, 6:11:06 AM6/2/23
to pyins...@googlegroups.com
You need to put the path in the spec file

Reply all
Reply to author
Forward
0 new messages