How to set environment variables in PyCharm?
Solution 1
You can set environmental variables in Pycharm's run configurations menu.
- Open the Run Configuration selector in the top-right and cick
Edit Configurations...
- Select the correct file from the menu, find
Environmental variables
and click...
- Add or change variables, then click
OK
You can access your environmental variables with os.environ
import os
print(os.environ['SOME_VAR'])
Solution 2
I was able to figure out this using a PyCharm plugin called EnvFile. This plugin, basically allows setting environment variables to run configurations from one or multiple files.
The installation is pretty simple:
Preferences > Plugins > Browse repositories... > Search for "Env File" > Install Plugin.
Then, I created a file, in my project root, called environment.env
which contains:
DATABASE_URL=postgres://127.0.0.1:5432/my_db_name
DEBUG=1
Then I went to Run->Edit Configurations, and I followed the steps in the next image:
In 3, I chose the file environment.env
, and then I could just click the play button in PyCharm, and everything worked like a charm.
Solution 3
This functionality has been added to the IDE now (working Pycharm 2018.3)
Just click the EnvFile
tab in the run configuration, click Enable EnvFile
and click the + icon to add an env file
Update: Essentially the same as the answer by @imguelvargasf but the the plugin was enabled by default for me.
Solution 4
The original question is:
How to set environment variables in PyCharm?
The two most-upvoted answers tell you how to set environment variables for PyCharm Run/Debug Configurations - manually enter them in "Environment variables" or use EnvFile plugin.
After using PyCharm for many years now, I've learned there are other key areas you can set PyCharm environment variables. EnvFile won't work for these other areas!
Here's where ELSE to look:
- Tools > Terminal > Environment variables
- Languages & Frameworks > Django > Environment variables
- Settings > Build, Execution, Deployment > Console > Python Console > Environment variables
- Settings > Build, Execution, Deployment > Console > Django Console > Environment variables
and of course, your run/debug configurations that was already mentioned.
Solution 5
This is what you can do to source an .env (and .flaskenv) file in the pycharm flask/django console. It would also work for a normal python console of course.
Do
pip install python-dotenv
in your environment (the same as being pointed to by pycharm).Go to: Settings > Build ,Execution, Deployment > Console > Flask/django Console
In "starting script" include something like this near the top:
from dotenv import load_dotenv
load_dotenv(verbose=True)
The .env file can look like this:
export KEY=VALUE
It doesn't matter if one includes export
or not for dotenv to read it.
As an alternative you could also source the .env file in the activate shell script for the respective virtual environement.
lmiguelvargasf
I enjoy solving problems. I like math, science, philosophy and technology. LinkedIn
Updated on January 29, 2022Comments
-
lmiguelvargasf over 2 years
I have started to work on a Django project, and I would like to set some environment variables without setting them manually or having a bash file to source.
I want to set the following variables:
export DATABASE_URL=postgres://127.0.0.1:5432/my_db_name export DEBUG=1 # there are other variables, but they contain personal information
I have read this, but that does not solve what I want. In addition, I have tried setting the environment variables in Preferences-> Build, Execution, Deployment->Console->Python Console/Django Console, but it sets the variables for the interpreter.
-
jacekbj almost 7 years@lmiguelvargasf I can't thank you enough for this answer. It looks like Pycharm can't pick up env vars sources in .bashrc (I'm on Linux). This plugin saved me a lot of time as I can use postactivate file as source.
-
alexey over 6 yearsoh, I was just trying to type on that line, the triple dot looked grayed out so i didn't even think to click on it.
-
LNI over 6 yearsI tried this method but PyCharm doesn't pick the defined env var (I specifically tried to set DATABASE_URL). Looks like a PyCharm bug.
-
Daniil Mashkin over 6 years@lmiguelvargasf yes, sorry. I mean 2017.2.4
-
Roman Kazakov over 6 years@LNI in PyCharm there are two places where we can set env variables. One is for running environment(and it sets how Joran showed) and also we can set variables which will be available for console in Pycharm(and it sets here Preferences -> Build, Execution, Deployment -> Console -> Python Console )
-
losik123 about 6 yearsWorking here on 2017.3. Thanks!
-
Chris Sewell over 5 yearsYeh this seems like the right answer to me. I would add that if you set the environmental variable in the Defaults/Python config, then it will be used every time you right-click on a script and run it
-
jonathan over 5 yearsWorking in 2017.2
-
krinker over 4 yearstoo bad it does not see .env file, i had to rename it to dev.env
-
lmiguelvargasf over 4 years@krinker, are you on Mac OS?
-
krinker over 4 yearsYes. I added this to the Finder so now I can see .env there, but even after restarting PyCharm, I still don't see it when I browse through PyCharm ``` defaults write com.apple.finder AppleShowAllFiles YES. ```
-
lmiguelvargasf over 4 years@krinker, when you are browsing for the file, please press,
cmd
+shift
+.
to show the hidden files. -
Kosmonaut over 4 yearsThis worked for me. Had to restart my interpreter first though.
-
alltej about 4 yearsI am pycharm 2019, and I don't see the tab
EnvFile
. Is this available in Python configuration (your screenshot is DjangoServer)? -
schlumpfpirat about 3 yearsAnyone got this to work with the Python Console and Terminal too? It seems quite useless to me to only be able to set it for the runtime environment, but none of the other subfunctionalities.
-
Admin about 3 yearsPlease keep in mind .env variables can not be defined with export. For example
REDIS_HOST=localhost
, but if you will define withexport REDIS_HOST=localhost
then it will not work. -
Dan Ciborowski - MSFT almost 3 yearshe has the "EnvFile" plugin installed.
-
harry hartmann over 2 yearswhy doesn't work this in my PyCharm 2021.2.1 (Professional Edition), using on Ubuntu 18.04.6 bionic. This clearly 'simle things' not working that's so frustrating. Printing out sorted(os.environ.items()) from inside django.conf.__init__ doesn't even lists PYTHONUNBUFFERED - very frustrating.
-
marti_ over 2 yearsthis solution did not work for pycharm 2021
-
hurlbz over 2 yearsthank you, this answer actually addresses the env being in the terminal and not just in the run config.
-
user almost 2 yearsIf it doesn't work make sure you've set the variables for the correct category in step 2. (i accidentally didn't set it on "Python" but on "Flask")