Pycharm: Set Environment Variable for Run Manage.Py Task

Pycharm: set environment variable for run manage.py Task

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.

How to set environment variables in PyCharm?

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

PyCharm 4.5.3 Django manage.py task not working after update

For version 4.5.3 only. The issue is with the Django Settings file, in TEMPLATES. It's a bug with the DIRS of TEMPLATES. See bug report. I commented out the TEMPLATES and it worked for me.

Do we have Manage.py Task in VS code

At present, there is no such button to execute the "Django" project in VSCode. We need to use the command "python manage.py runserver" to execute it.

After VSCode uses this command to execute the Django project, when we change the content of the project, the VSCode terminal will automatically update, just refresh the web page of the browser, and the updated content is already used.

In addition, they are two different tools after all, it is difficult to require them to be exactly the same in use, but if you encounter any technical problems when using VSCode, please let us know.

Reference: Django in VSCode.



Related Topics



Leave a reply



Submit