Skip to content

Instantly share code, notes, and snippets.

@Shrestha7
Created April 9, 2023 15:33
Show Gist options
  • Save Shrestha7/ecc6cf8b4506ca3901a0542ad82c1cda to your computer and use it in GitHub Desktop.
Save Shrestha7/ecc6cf8b4506ca3901a0542ad82c1cda to your computer and use it in GitHub Desktop.
To add a .env file outside the .exe file when using PyInstaller, you can use the --add-data option to include the .env file in the distribution. Assuming that your .env file is located in the root directory of your project, and you want to include it in the same directory as the PyInstaller-generated .exe file, you can use the following command:
pyinstaller --add-data=".env;." myscript.py
# This command tells PyInstaller to include the .env file in the distribution, and to place it in the same directory as the .exe file.
# Note that the --add-data option takes two arguments separated by a semicolon: the first argument is the path to the .env file, and the second argument is the destination directory for the file. In this case, the destination directory is the current directory, denoted by the . symbol.
# After running this command, PyInstaller will generate a new directory containing the .exe file and the .env file, as well as any other necessary files for your script to run.
@Shrestha7
Copy link
Author

To add .env file outside the .exe file using pyinstaller

@Padmahas
Copy link

Hello @Shrestha7 ,
I used this command and even the .env file was packaged inside "_internal" folder. But when the program runs, the environment variables are empty.

So I moved .env file outside "_internal" folder and kept it next to .exe file. Then it started working.

But, thank you very much for the command. I was searching for it for couple of hours. This saved my day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment