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.
You can set environmental variables in Pycharm"s run configurations menu.
Open the Run Configuration selector in the top-right and cick
Environmental variables and click
Add or change variables, then click
import os print(os.environ["SOME_VAR"])
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:
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.