Forums

Environment variables in wsgi file are not working

Hi.

I'm setting a couple of variables in my wsgi file in the Web tab. Here is my wsgi file:

# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:

activate_this = '/home/skoer/.virtualenvs/citystories/bin/activate_this.py'
with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(__file__=activate_this))

import os
import sys

# assuming your django settings file is at '/home/skoer/mysite/mysite/settings.py'
# and your manage.py is is at '/home/skoer/mysite/manage.py'
path = '/home/skoer/citystories/citystories'
if path not in sys.path:
    sys.path.append(path)

os.environ['SECRET_KEY'] = 'my_secret_key'
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings.production'

# then, for django >=1.5:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# or, for older django <=1.4
#import django.core.handlers.wsgi
#application = django.core.handlers.wsgi.WSGIHandler()

But trying to run console commands like "django-admin collectstatic" results in the following error:

Traceback (most recent call last): File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/core/management/init.py", line 179, in fetc h_command app_name = commands[subcommand] KeyError: 'collectstatic'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/skoer/.virtualenvs/citystories/bin/django-admin", line 11, in <module>
sys.exit(execute_from_command_line())
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in exec
ute_from_command_line
utility.execute()
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in exec
ute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/core/management/__init__.py", line 182, in fetc
h_command
settings.INSTALLED_APPS
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either
define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

and "python manage.py collectstatic" set with a specific path to the settings file in manage.py results in this error:

Traceback (most recent call last):
File "/home/skoer/citystories/citystories/config/settings/base.py", line 31, in get_env_variable
return os.environ[var_name]
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/os.py", line 631, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in exec
ute_from_command_line
utility.execute()
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in exec
ute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/core/management/__init__.py", line 182, in fetc
h_command
settings.INSTALLED_APPS
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/skoer/.virtualenvs/citystories/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1448, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/skoer/citystories/citystories/config/settings/production.py", line 4, in <module>
SECRET_KEY = get_env_variable('SECRET_KEY')
File "/home/skoer/citystories/citystories/config/settings/base.py", line 34, in get_env_variable
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

This works well in my local development environment where I also use environments variables with virtualenvwrapper. I've had this issue before when deploying a new Django, where it somehow resolved itself after some time. I'm not sure what is causing this.

I'm using Django 1.8 and Python 3.4 with a virtual environment.

The env vars set in wsgi is for the web server. For general cosole usage, you still need to export the vars inside the preactivate/postactivate files inside your virtual environment folder e.g. /home/username/.virtualenvs/yourvirtualenv/bin/postactivate

Hmm. I do this on my local environment, but haven't done it on PA before. This of course makes my console usage work. But I'm still stuck with a Bad Request (400) when trying to access my app via the url. My error log is not showing anything relevant. It might be corrupt because I deleted a previous app and virtualenv before adding my current app using the same app name and url.

There are a few reasons you might get 400 Bad request. Search the forums for "Django bad request"

Awesome that you can search your forums for such a generic error. I needed to set ALLOWED_HOSTS.

As always great support!

Have a nice Sunday.

Cool. Glad to help.

So I need to export the Django Secret Key in WSGI and in my vritual environment postactivate? Not very DRY!

Agreed :-(