Pycharm: set environment variable for run manage.py Task

Question:

I have moved my SECRET_KEY value out of my settings file, and it gets set when I load my virtualenv. I can confirm the value is present from python manage.py shell.

When I run the Django Console, SECRET_KEY is missing, as it should. So in preferences, I go to Console>Django Console and load SECRET_KEY and the appropriate value. I go back into the Django Console, and SECRET_KEY is there.

As expected, I cannot yet run a manage.py Task because it has yet to find the SECRET_KEY. So I go into Run>Edit Configurations to add SECRET_KEY into Django server and Django Tests, and into the project server. Restart Pycharm, confirm keys.

When I run a manage.py Task, such as runserver, I still get KeyError: 'SECRET_KEY'.

Where do I put this key?

Asked By: Cole

||

Answers:

Same here, for some reason PyCharm cant see exported env vars.
For now i set SECRET_KEY in PyCharm Run/Debug Configurations -> “Environment variables”

Answered By: ammonium

Because Pycharm is not launching from a terminal, your environment will not be loaded. In short, any GUI program will not inherit the SHELL variables. See this for reasons (assuming a Mac).

However, there are several basic solutions to this problem. As @user3228589 posted, you can set this up as a variable within PyCharm. This has several pros and cons. I personally don’t like this approach because it’s not a single source. To fix this, I use a small function at the top of my settings.py file which looks up the variable inside a local .env file. I put all of my “private” stuff in there. I also can reference this in my virtualenv.

Here is what it looks like.

— settings.py

def get_env_variable(var_name, default=False):
    """
    Get the environment variable or return exception
    :param var_name: Environment Variable to lookup
    """
    try:
        return os.environ[var_name]
    except KeyError:
        import StringIO
        import ConfigParser
        env_file = os.environ.get('PROJECT_ENV_FILE', SITE_ROOT + "/.env")
        try:
            config = StringIO.StringIO()
            config.write("[DATA]n")
            config.write(open(env_file).read())
            config.seek(0, os.SEEK_SET)
            cp = ConfigParser.ConfigParser()
            cp.readfp(config)
            value = dict(cp.items('DATA'))[var_name.lower()]
            if value.startswith('"') and value.endswith('"'):
                value = value[1:-1]
            elif value.startswith("'") and value.endswith("'"):
                value = value[1:-1]
            os.environ.setdefault(var_name, value)
            return value
        except (KeyError, IOError):
            if default is not False:
                return default
            from django.core.exceptions import ImproperlyConfigured
            error_msg = "Either set the env variable '{var}' or place it in your " 
                        "{env_file} file as '{var} = VALUE'"
            raise ImproperlyConfigured(error_msg.format(var=var_name, env_file=env_file))

# Make this unique, and don't share it with anybody.
SECRET_KEY = get_env_variable('SECRET_KEY')

Then the env file looks like this:

#!/bin/sh
#
# This should normally be placed in the ${SITE_ROOT}/.env
#
# DEPLOYMENT DO NOT MODIFY THESE..
SECRET_KEY='XXXSECRETKEY'

And finally your virtualenv/bin/postactivate can source this file. You could go further and export the variables as described here if you’d like, but since settings file directly calls the .env, there isn’t really a need.

Answered By: rh0dium

To set your environment variables in PyCharm do the following:

  • Open the ‘File’ menu
  • Click ‘Settings’
  • Click the ‘+’ sign next to ‘Console’
  • Click Python Console
  • Click the ‘…’ button next to environment variables
  • Click the ‘+’ to add environment variables
Answered By: nu everest

You can set the manage.py task environment variables via:

Preferences| Languages&Frameworks| Django| Manage.py tasks

Setting the env vars via the run/debug/console configuration won’t affect the built in pycharm’s manage.py task.

Answered By: OmriToptix

based on @rh0dium amazing answer i’ve made this class:

here’s my settings.py:

import os


class EnvironmentSettings():
    """Access to environment variables via system os or .env file for development

    """
    def __init__(self, root_folder_path):
        self._root_folder_path = root_folder_path

    def __getitem__(self, key):
        return self._get_env_variable(key)

    def __setitem__(self, key, value):
        raise InvalidOperationException('Environment Settings are read-only')

    def __delitem__(self, key):
        raise InvalidOperationException('Environment Settings are read-only')

    def _get_env_variable(self, var_name, default=False):
        """
        Get the environment variable or return exception
        :param var_name: Environment Variable to lookup
        """
        try:
            return os.environ[var_name]
        except KeyError:
            from io import StringIO
            from configparser import ConfigParser

            env_file = os.environ.get('PROJECT_ENV_FILE', self._root_folder_path + "/.env")
            try:
                config = StringIO()
                config.write("[DATA]n")
                config.write(open(env_file).read())
                config.seek(0, os.SEEK_SET)
                cp = ConfigParser()
                cp.readfp(config)
                value = dict(cp.items('DATA'))[var_name.lower()]
                if value.startswith('"') and value.endswith('"'):
                    value = value[1:-1]
                elif value.startswith("'") and value.endswith("'"):
                    value = value[1:-1]
                os.environ.setdefault(var_name, value)
                return value
            except (KeyError, IOError):
                if default is not False:
                    return default
                error_msg = "Either set the env variable '{var}' or place it in your " 
                            "{env_file} file as '{var} = VALUE'"
                raise ConfigurationError(error_msg.format(var=var_name, env_file=env_file))


class ConfigurationError(Exception):
    pass


class InvalidOperationException(Exception):
    pass

and inside my runserver.py i have this calling code:

from settings import EnvironmentSettings

root_folder_path = os.path.dirname(os.path.abspath(__file__))
env_settings = EnvironmentSettings(root_folder_path)
config_name = env_settings['APP_SETTINGS']
Answered By: danfromisrael

Another option that’s worked for me:

  1. Open a terminal
  2. Activate the virtualenv of the project which will cause the hooks to run and set the environment variables
  3. Launch PyCharm from this command line.

Pycharm will then have access to the environment variables. Likely because of something having to do with the PyCharm process being a child of the shell.

Answered By: Antero Ortiz

The answer by @(nu everest) did not work for me. Whatever you described in the question worked for me. You can set all environment variables in the Run/Debug Configurations dialog. This is w.r.t PyCharm 2016.1 and also as per https://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-python.html?origin=old_help

Answered By: Nitin

I’m trying this setup currently. I have an env.local file in my project root. My manage.py looks like this:

#!/usr/bin/env python
import os
import sys


if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")

    # Load environment variables from env.local, if option --load-env specified.
    # Good for injecting environment into PyCharm run configurations for example and no need to
    # manually load the env values for manage.py commands
    if "--load-env" in sys.argv:
        with open("env.local") as envfile:
            for line in envfile:
                if line.strip():
                    setting = line.strip().split("=", maxsplit=1)
                    os.environ.setdefault(setting[0], setting[1])
        sys.argv.remove("--load-env")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Then I just pass --load-env in the PyCharm run dialog and all the environment variables will be loaded in for the time of the run.

Answered By: jaywink

To paraphrase @antero-ortiz’s answer, instead of using the default Mac double-click or spotlight search to launch PyCharm, you can launch it directly from the terminal. This will inherit all of your environment variables from your terminal session to the PyCharm app.

Here’s how I solve this issue.

  1. From your terminal program, run the snippet below.
  2. Validate that things worked by following the instructions to Edit your Run/Debug Configurations and,
    1. Clicking the ... next to Environment variables
    2. Then clicking Show at the bottom right of the screen that pops up. This will show all of the environment variables inherited by the Debug process.
    3. Find your SECRET_KEY in this list. If it’s not there, post a comment on this answer and we can try to figure it out!
  3. You’ll likely need to launch PyCharm this way every time you start it.

Snippet:

# This file would presumably export SECRET_KEY
source /path/to/env/file
# This is just a shortcut to finding out where you installed PyCharm
# If you used brew cask, it's probably in /opt/homebrew-cask/Caskroom/pycharm
open $(locate PyCharm.app | egrep 'PyCharm.app$')

Side note

Including environment variables in a file that is then source‘d is a common pattern for Django development, and the discussion of that best practice is best left out of answers.

Answered By: Ben

In Pycharm manage.py tasks run in an isolated process that do not have access to your environment variables (not the same as Run/Debug).

The simplest solution is to make your python code aware of environment variables by reading them from your .env file directly.

Take a look at: https://github.com/joke2k/django-environ

import environ

env = environ.Env.read_env() # reading .env file

SECRET_KEY = env('SECRET_KEY')

That way you have a single source of configurations, and less settings in the IDE.

Hope that helps,

Answered By: cvng

Please note that the answer mentioned by “nu everest” works but you will not see Console tab available unless you create a project in pycharm. As individual files can be run without creating a project in pycharm some people might be confused. Although you have also to note that settings are lost to run-configurations that do not belong to a project when pycharm is closed.

Answered By: Part

Here is my manage.py file in case it helps. It reads all the variables from .env file located in root directory of my project.

enter image description here

Answered By: saran3h

In PyCharm 2018 you can use django manage.py environment variables option in settings

  • File
  • Settings
  • Language and Frameworks
  • Django
  • Environment variables

You can see in image

Answered By: alireza yazdandoost

I consider that a good approach is to use django-environ which allow you to read environment variables from a .env file.

Usage

First, install django-environ

pip install django-environ

Second, create a .env file, in the same directory of your settings.py file.

$ cd project-settings-directory/
$ touch .env

Third, add the environment variables you want to use to .env:

# .env file
DEBUG=on
SECRET_KEY=your-secret-key
DATABASE_URL=psql://urser:[email protected]:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db

Fourth, in your settings file, add the following just after your import statements:

# other import statements

import environ


# the followin line expects .env file to exist. Otherwise, an IOError exception will be raised.
env = environ.Env.read_env()

Fifth, load the environment variables:

DEBUG = env('DEBUG')
SECRET_KEY = env('SECRET_KEY')

That’s it. If you want further information about please review the docs.

Note: this configuration will also help you to set environment variables when running a server from PyCharm.

Answered By: lmiguelvargasf

In PyCharm 2019+, the debug environment settings are described on their site here. I had similar issues, even with environment variables defined in the other parts of PyCharm (Console, Terminal, etc.)

Summary:

  1. Top-right where your Django project name is (next to the "run" icon), click the drop-down arrow.

    i. Note: If the project hasn’t been configured for Django, you might not see it. There should be templates available to use in the next window, though.

  2. "Edit Configurations…"

  3. You’ll see an "Environment variables" field apart from the console settings. That’s the one you actually want to use if you’re running the Django server via PyCharm’s debugger.

Answered By: Supra621

When using PyCharm along with Django, there are multiple sections where you can set environment variables (EVs):

  • File > Settings > Languages and Frameworks > Django
    • There’s no purpose to set EVs here
  • File > Settings > Build, Execution, Deployment > Console > Python Console
    • There’s no purpose to set EVs here
  • File > Settings > Build, Execution, Deployment > Console > Django Console
    • Set EVs here and they’ll be accesible when using the PyCharm Python Console (which in a Django project opens a Django shell)
  • File > Settings > Tools > Terminal
    • Set EVs here and they’ll be accesible when using the PyCharm Terminal (i.e when running python manage.py commands in the terminal)
  • Run > Edit configurations > [Your Django run configuration]
    • Set EVs here and they’ll be accesible when using the PyCharm Run button

Tested on PyCharm 2020.2 using a virtual environment.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.