pycharm指定运行环境,Pycharm:设置运行manage.py Task的环境变量

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?

解决方案

Because Pycharm is not launching from a terminal your environment will not 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 call the .env there isn't really a need.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值