Pycharm: set environment variable for run manage.py Task

74,334

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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

Share:
74,334

Related videos on Youtube

Cole
Author by

Cole

Updated on July 09, 2022

Comments

  • Cole
    Cole almost 2 years

    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?

    • Games Brainiac
      Games Brainiac over 10 years
      Why have you done this in the first place?
    • Cole
      Cole over 10 years
      Secret keys in plain texts files, particularly as part of a shared repo somewhere, is a bad idea.
    • David Fraser
      David Fraser about 8 years
      EnvFile is an extension that allows you to read environment variables from a file. That allows you to set the variables in a file without having to manage your code...
  • danfromisrael
    danfromisrael about 9 years
    thank you so much! this is really really helpful! i've encapsulated & generalized it for easy use (removed django exceptions since i'm using flask): stackoverflow.com/a/29546356/303114
  • dondublon
    dondublon almost 9 years
    Please, can you tell me, how I can read this environment variable from code? 'os.getenv' returns only whole-system variables.
  • Ben
    Ben almost 8 years
    This is the correct approach IMO. I expanded upon it by providing my own answer.
  • Kim
    Kim over 7 years
    Refer to cvng's answer. He links the django-environ package (github.com/joke2k/django-environ) which serves exactly that functionality.
  • Karuhanga
    Karuhanga over 5 years
    You want to do something like os.environ.get("KEY")
  • PHPirate
    PHPirate about 5 years
    In my PyCharm 2019.1.3 the option is in File | Settings | Build, Execution, Deployment | Console | Python Console | Environment variables
  • Bill
    Bill over 4 years
    This is a great solution - works very well, doesn’t require copying settings to multiple places - thanks!
  • greatvovan
    greatvovan over 3 years
    This is the correct approach and must be the accepted answer.
  • Benedikt S. Vogler
    Benedikt S. Vogler over 3 years
    Note that this package is called "python-environ" and not "environ"
  • Olivier Pons
    Olivier Pons about 2 years
    I was looking for that for 2 hours: there are 2 consoles options (see answers above) but yours it the right one.