简体   繁体   中英

Deploying Django to PythonAnywhere and cannot use SECRET_KEY from environment vars despite it being set

Currently, my error logs are spitting out nothing but this error:

Error running WSGI application 
KeyError: 'SECRET_KEY'
  File "/var/www/www_optranslations_net_wsgi.py", line 24, in <module>
    application = get_wsgi_application() 

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)

  File "/home/optranslations/translation-site/optranslations/settings.py", line 24, in <module>
    SECRET_KEY = os.environ.get('SECRET_KEY')

  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/os.py", line 725, in __getitem__
    raise KeyError(key) from None

So, from what I can gather, it can't find the SECRET_KEY environment variable. Checking from both the bash terminal and the os.environ command in the interactive python interpreter, the key is correctly set in there.

Furthermore, I tried both:

SECRET_KEY = os.environ.get('SECRET_KEY')

And:

SECRET_KEY = os.getenv('SECRET_KEY')

Using the same code to with a print in the interactive python console produces the correct key.

And it is set in my wsgi file as per PythonAnywhere's instructions like so:

os.environ["SECRET_KEY"] = 'secretkeyhere'

As well as in my postactivate file for my virtualenv:

export SECRET_KEY="secretkeyhere"

So, what's going on? It just keeps spitting out this error in my log and I can't find anything online or even begin to figure out what it is. I'm sure it's something I'm overlooking, though. I can add in anymore code you think is required, so please just let me know.

Edit

Interesting, now I'm getting this:

2017-02-15 07:46:27,883 :Error running WSGI application
2017-02-15 07:46:27,884 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2017-02-15 07:46:27,884 :  File "/var/www/www_optranslations_net_wsgi.py", line 24, in <module>
2017-02-15 07:46:27,885 :    application = get_wsgi_application()
2017-02-15 07:46:27,885 :
2017-02-15 07:46:27,885 :  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2017-02-15 07:46:27,885 :    django.setup(set_prefix=False)
2017-02-15 07:46:27,885 :
2017-02-15 07:46:27,885 :  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
2017-02-15 07:46:27,885 :    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2017-02-15 07:46:27,885 :
2017-02-15 07:46:27,886 :  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
2017-02-15 07:46:27,886 :    self._setup(name)
2017-02-15 07:46:27,886 :
2017-02-15 07:46:27,886 :  File "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
2017-02-15 07:46:27,886 :    self._wrapped = Settings(settings_module)
2017-02-15 07:46:27,886 :
2017-02-15 07:46:27,886 :  File    "/home/optranslations/.virtualenvs/optl/lib/python3.5/site-packages/django/conf/__init__.py", line 116, in __init__
2017-02-15 07:46:27,886 :    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

So now it's ImproperlyConfigured. However, all I did was deactivate my virtualenv and reload it to activate the postactivate script again. I can still echo $SECRET_KEY and see it fine (and getenv in the interactive console, too).

I also tried to django shell by running the following:

python manage.py shell
from django.conf import settings
print(settings.SECRET_KEY)

And, of course, it printed out the correct key.

Well, the answer turned out to be along embarrassing. My WSGI file had my os.environ declarations BELOW these lines:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

They had to be above those lines, and it instantly worked, for obvious reasons looking back on it now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM