I need to set some access token environment variables for my python project that I am running in a pipenv. I will want to set these environment variables every time I start the pipenv.
How do I do this?
pipenv stores the packages inside your local machines shared directory called virtualenvs (~/. local/share/virtualenvs/<env-name> ). It ensures that you don't need to use pip and virtualenv separately.
Basic Usage. This creates the project_folder folder inside ~/Envs . Alternatively, you can make a project, which creates the virtual environment, and also a project directory inside $WORKON_HOME , which is cd -ed into when you workon project_folder .
If you want to load automatically some environment variables each time you start the project, you can set a .env
file at the root folder of the project, next to the Pipfile
. See Automatic Loading of .env.
You can run the following command from the right folder to create this .env file :
echo MY_TOKEN=SuperToKen >.env # create the file and write into echo MY_VAR=SuperVar >>.env # append to the file
or just create it manually to obtain:
MY_TOKEN=SuperToKen MY_VAR=SuperVar
This file will be loaded automatically with pipenv shell
or pipenv run your_command
and the environment variables will be available.
You can access/check them in your code with :
print(os.getenv('MY_TOKEN', 'Token Not found'))
Not sure about other IDE, but within Pycharm you need the plugin Env File to load it automatically (access Env File tab from the Run/Debug configurations).
You can add comments in this file with a leading #
# My test token MY_TOKEN=SuperToKen
Note : Of course you must exclude this file from your version control (like git).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With