Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set environment variables in pipenv?

Tags:

python

pipenv

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?

like image 572
user1283776 Avatar asked Jul 07 '18 21:07

user1283776


People also ask

Where are Pipenv environments stored?

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.

Does Pipenv create virtual environment?

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 .


1 Answers

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).

like image 193
PRMoureu Avatar answered Oct 17 '22 23:10

PRMoureu