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 <[email protected]> del dia dc., 31 de
maig 2023 a les 16:28:

> 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
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pyinstaller/50b7392d-cbb1-40bc-b038-5900b3253df3n%40googlegroups.com
> <https://groups.google.com/d/msgid/pyinstaller/50b7392d-cbb1-40bc-b038-5900b3253df3n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyinstaller/CAB8Wu-K57Tv4HXSqSz56%3D2rnxi4tbYSpFAQawN%2BnPW8HdaBjHw%40mail.gmail.com.

Reply via email to