Skip to content
Advertisement

How to set environment variables in PyCharm?

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.

Advertisement

Answer

You can set environmental variables in Pycharm’s run configurations menu.

  1. Open the Run Configuration selector in the top-right and cick Edit Configurations...

Edit Configurations...

  1. Select the correct file from the menu, find Environmental variables and click ...

Environment variables

  1. Add or change variables, then click OK

Editing environmental variables

You can access your environmental variables with os.environ

import os
print(os.environ['SOME_VAR'])
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement