How to set environment variables in PyCharm?

PythonDjangoEnvironment Variables

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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. Find Environmental variables and click ...

Environmental 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'])

Solution 2 - Python

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:

Set Environment Variables

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 - Python

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

Example with the glorious material theme

Update: Essentially the same as the answer by @imguelvargasf but the the plugin was enabled by default for me.

Solution 4 - Python

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
  • Build, Execution, Deployment > Console > Python Console > Environment variables
  • Build, Execution, Deployment > Console > Django Console > Environment variables

and of course, your run/debug configurations that was already mentioned.

Solution 5 - Python

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.

  1. Do pip install python-dotenv in your environment (the same as being pointed to by pycharm).

  2. Go to: Settings > Build ,Execution, Deployment > Console > Flask/django Console

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

Solution 6 - Python

None of the above methods worked for me. If you are on Windows, try this on PyCharm terminal:

setx YOUR_VAR "VALUE"

You can access it in your scripts using os.environ['YOUR_VAR'].

Solution 7 - Python

This method helped me a lot for calling environmental variable from a .env file and its really easy to implement too.

  1. Create a .env file at the same hierarchical level has your main.py(python file at which you are currently working with and where you want your environmental variables to be updated)

  2. Install a package called "python-dotenv"

  3. Import and load the package in your main.py as follows

     import os
     from dotenv import load_dotenv, find_dotenv
     
     load_dotenv(find_dotenv())
     any_secret_var = os.get_env("SECRET_KEY")
    

and BOOM it will work like magic

And the .env file should look like, don't place any qoutes on the values in the .env file

    SECRET_KEY=any_secret_value_you_need_to_place_in

Solution 8 - Python

In the Pycharm Python console just type

ENV_VAR_NAME="STRING VARIABLE"
INT_VAR_NAME=5

Unlike other Python text editors, you do not add export in Pycharm

Solution 9 - Python

Solution tested with virtual environment.

Create an script that define and export or source the env vars. Then define the script as the python interpreter of an existing virtual env. The solution works for any task like run, debug, console ...

Example script:

#!/bin/bash


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export VAR1="TEST1"
source $DIR/../../.env
$DIR/python "$@"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlmiguelvargasfView Question on Stackoverflow
Solution 1 - PythonJoran BeasleyView Answer on Stackoverflow
Solution 2 - PythonlmiguelvargasfView Answer on Stackoverflow
Solution 3 - PythonNino van HooffView Answer on Stackoverflow
Solution 4 - PythonJaradView Answer on Stackoverflow
Solution 5 - PythonkjoeterView Answer on Stackoverflow
Solution 6 - PythonBex T.View Answer on Stackoverflow
Solution 7 - Pythonvijayakumar kView Answer on Stackoverflow
Solution 8 - Pythonuser12246115View Answer on Stackoverflow
Solution 9 - Pythongilcu2View Answer on Stackoverflow