Forums

Error: Setting up a new project with virtualenv

Hello, I try to set up a new Project with a virtualenv with Django 1.7 and Python 3.4.

I get an error when I try to call the site. Maybe one of you knows the problem.

Here the error.log

2014-12-31 14:03:04,631 :Internal Server Error: / Traceback (most recent call last): File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/urlresolvers.py", line 364, in urlconf_module return self._urlconf_module AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/handlers/base.py", line 98, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/urlresolvers.py", line 343, in resolve
    for pattern in self.url_patterns:
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/urlresolvers.py", line 372, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/urlresolvers.py", line 366, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/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/Wanderwichtl/WeingutBender/weingutBender/weingutBender/urls.py", line 9, in <module>
    url(r'^admin/', include(admin.site.urls)),
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 260, in urls
    return self.get_urls(), self.app_name, self.name
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 221, in get_urls
    self.check_dependencies()
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 159, in check_dependencies
    if not apps.is_installed('django.contrib.admin'):
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/apps/registry.py", line 223, in is_installed
    self.check_apps_ready()
  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
2014-12-31 14:03:04,724 :Traceback (most recent call last):
2014-12-31 14:03:04,724 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/urlresolvers.py", line 364, in urlconf_module
2014-12-31 14:03:04,725 :    return self._urlconf_module
2014-12-31 14:03:04,725 :AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module'
2014-12-31 14:03:04,725 :
2014-12-31 14:03:04,726 :During handling of the above exception, another exception occurred:
2014-12-31 14:03:04,726 :
2014-12-31 14:03:04,726 :Traceback (most recent call last):
2014-12-31 14:03:04,726 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/handlers/base.py", line 98, in get_response
2014-12-31 14:03:04,727 :    resolver_match = resolver.resolve(request.path_info)
2014-12-31 14:03:04,727 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/urlresolvers.py", line 343, in resolve
2014-12-31 14:03:04,728 :    for pattern in self.url_patterns:
2014-12-31 14:03:04,728 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/urlresolvers.py", line 372, in url_patterns
2014-12-31 14:03:04,732 :    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
2014-12-31 14:03:04,733 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/urlresolvers.py", line 366, in urlconf_module
2014-12-31 14:03:04,736 :    self._urlconf_module = import_module(self.urlconf_name)
2014-12-31 14:03:04,736 :  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
2014-12-31 14:03:04,736 :    return _bootstrap._gcd_import(name[level:], package, level)
2014-12-31 14:03:04,736 :  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
2014-12-31 14:03:04,737 :  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
2014-12-31 14:03:04,737 :  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
2014-12-31 14:03:04,738 :  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
2014-12-31 14:03:04,738 :  File "<frozen importlib._bootstrap>", line 1129, in _exec
2014-12-31 14:03:04,739 :  File "<frozen importlib._bootstrap>", line 1448, in exec_module
2014-12-31 14:03:04,739 :  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
2014-12-31 14:03:04,739 :  File "/home/Wanderwichtl/WeingutBender/weingutBender/weingutBender/urls.py", line 9, in <module>
2014-12-31 14:03:04,742 :    url(r'^admin/', include(admin.site.urls)),
2014-12-31 14:03:04,742 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 260, in urls
2014-12-31 14:03:04,745 :    return self.get_urls(), self.app_name, self.name
2014-12-31 14:03:04,745 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 221, in get_urls
2014-12-31 14:03:04,747 :    self.check_dependencies()
2014-12-31 14:03:04,747 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 159, in check_dependencies
2014-12-31 14:03:04,749 :    if not apps.is_installed('django.contrib.admin'):
2014-12-31 14:03:04,749 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/apps/registry.py", line 223, in is_installed
2014-12-31 14:03:04,752 :    self.check_apps_ready()
2014-12-31 14:03:04,752 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/apps/registry.py", line 124, in check_apps_ready
2014-12-31 14:03:04,753 :    raise AppRegistryNotReady("Apps aren't loaded yet.")
2014-12-31 14:03:04,753 :django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
2014-12-31 14:03:04,753 :
2014-12-31 14:03:04,753 :During handling of the above exception, another exception occurred:
2014-12-31 14:03:04,754 :
2014-12-31 14:03:04,754 :Traceback (most recent call last):
2014-12-31 14:03:04,754 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/template/defaultfilters.py", line 766, in date
2014-12-31 14:03:04,758 :    return formats.date_format(value, arg)
2014-12-31 14:03:04,758 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/formats.py", line 126, in date_format
2014-12-31 14:03:04,763 :    return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))
2014-12-31 14:03:04,763 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/formats.py", line 98, in get_format
2014-12-31 14:03:04,766 :    return getattr(settings, format_type)
2014-12-31 14:03:04,766 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/conf/__init__.py", line 47, in __getattr__
2014-12-31 14:03:04,771 :    return getattr(self._wrapped, name)
2014-12-31 14:03:04,772 :AttributeError: 'Settings' object has no attribute 'r'
2014-12-31 14:03:04,772 :
2014-12-31 14:03:04,772 :During handling of the above exception, another exception occurred:
2014-12-31 14:03:04,772 :
2014-12-31 14:03:04,772 :Traceback (most recent call last):
2014-12-31 14:03:04,772 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 186, in _fetch
2014-12-31 14:03:04,778 :    app_configs = reversed(list(apps.get_app_configs()))
2014-12-31 14:03:04,778 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/apps/registry.py", line 137, in get_app_configs
2014-12-31 14:03:04,780 :    self.check_apps_ready()
2014-12-31 14:03:04,780 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/apps/registry.py", line 124, in check_apps_ready
2014-12-31 14:03:04,781 :    raise AppRegistryNotReady("Apps aren't loaded yet.")
2014-12-31 14:03:04,781 :django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
2014-12-31 14:03:04,781 :
2014-12-31 14:03:04,781 :During handling of the above exception, another exception occurred:
2014-12-31 14:03:04,782 :
2014-12-31 14:03:04,782 :Traceback (most recent call last):
2014-12-31 14:03:04,782 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2014-12-31 14:03:04,782 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-12-31 14:03:04,782 :  File "/usr/lib/python3.4/logging/__init__.py", line 1296, in exception
2014-12-31 14:03:04,783 :    self.error(msg, *args, **kwargs)
2014-12-31 14:03:04,783 :  File "/usr/lib/python3.4/logging/__init__.py", line 1289, in error
2014-12-31 14:03:04,784 :    self._log(ERROR, msg, args, **kwargs)
2014-12-31 14:03:04,784 :  File "/usr/lib/python3.4/logging/__init__.py", line 1395, in _log
2014-12-31 14:03:04,785 :    self.handle(record)
2014-12-31 14:03:04,785 :  File "/usr/lib/python3.4/logging/__init__.py", line 1404, in handle
2014-12-31 14:03:04,786 :    if (not self.disabled) and self.filter(record):
2014-12-31 14:03:04,786 :  File "/usr/lib/python3.4/logging/__init__.py", line 692, in filter
2014-12-31 14:03:04,787 :    for f in self.filters:
2014-12-31 14:03:04,787 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2014-12-31 14:03:04,787 :    app_iterator = self.app(environ, start_response)
2014-12-31 14:03:04,787 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
2014-12-31 14:03:04,790 :    response = self.get_response(request)
2014-12-31 14:03:04,791 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/handlers/base.py", line 199, in get_response
2014-12-31 14:03:04,792 :    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
2014-12-31 14:03:04,792 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/core/handlers/base.py", line 236, in handle_uncaught_exception
2014-12-31 14:03:04,793 :    return debug.technical_500_response(request, *exc_info)
2014-12-31 14:03:04,793 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/views/debug.py", line 91, in technical_500_response
2014-12-31 14:03:04,797 :    html = reporter.get_traceback_html()
2014-12-31 14:03:04,797 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/views/debug.py", line 350, in get_traceback_html
2014-12-31 14:03:04,799 :    return t.render(c)
2014-12-31 14:03:04,799 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/template/base.py", line 148, in render
2014-12-31 14:03:04,802 :    return self._render(context)
2014-12-31 14:03:04,802 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/template/base.py", line 142, in _render
2014-12-31 14:03:04,803 :    return self.nodelist.render(context)
2014-12-31 14:03:04,803 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/template/base.py", line 844, in render
2014-12-31 14:03:04,805 :    bit = self.render_node(node, context)
2014-12-31 14:03:04,805 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/template/debug.py", line 80, in render_node
2014-12-31 14:03:04,808 :    return node.render(context)
2014-12-31 14:03:04,808 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/template/debug.py", line 90, in render
2014-12-31 14:03:04,809 :    output = self.filter_expression.resolve(context)
2014-12-31 14:03:04,809 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/template/base.py", line 624, in resolve
2014-12-31 14:03:04,811 :    new_obj = func(obj, *arg_vals)
2014-12-31 14:03:04,811 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/template/defaultfilters.py", line 769, in date
2014-12-31 14:03:04,812 :    return format(value, arg)
2014-12-31 14:03:04,812 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/dateformat.py", line 343, in format
2014-12-31 14:03:04,814 :    return df.format(format_string)
2014-12-31 14:03:04,814 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/dateformat.py", line 35, in format
2014-12-31 14:03:04,816 :    pieces.append(force_text(getattr(self, piece)()))
2014-12-31 14:03:04,816 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/dateformat.py", line 268, in r
2014-12-31 14:03:04,818 :    return self.format('D, j M Y H:i:s O')
2014-12-31 14:03:04,818 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/dateformat.py", line 35, in format
2014-12-31 14:03:04,819 :    pieces.append(force_text(getattr(self, piece)()))
2014-12-31 14:03:04,819 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/encoding.py", line 83, in force_text
2014-12-31 14:03:04,822 :    s = six.text_type(s)
2014-12-31 14:03:04,822 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/functional.py", line 144, in __text_cast
2014-12-31 14:03:04,824 :    return func(*self.__args, **self.__kw)
2014-12-31 14:03:04,825 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 83, in ugettext
2014-12-31 14:03:04,827 :    return _trans.ugettext(message)
2014-12-31 14:03:04,827 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 319, in gettext
2014-12-31 14:03:04,829 :    return do_translate(message, 'gettext')
2014-12-31 14:03:04,829 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 306, in do_translate
2014-12-31 14:03:04,831 :    _default = translation(settings.LANGUAGE_CODE)
2014-12-31 14:03:04,831 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 209, in translation
2014-12-31 14:03:04,832 :    default_translation = _fetch(settings.LANGUAGE_CODE)
2014-12-31 14:03:04,832 :  File "/home/Wanderwichtl/.virtualenvs/django17/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 189, in _fetch
2014-12-31 14:03:04,833 :    "The translation infrastructure cannot be initialized before the "
2014-12-31 14:03:04,833 :django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

The tree of my django17:

├── Dropbox
     ├── README.txt
     └── __init__.py
├── README.txt
└── WeingutBender
     └── weingutBender
         ├── manage.py
         └── weingutBender
             ├── __init__.py
             ├── __pycache__
                 ├── __init__.cpython-34.pyc 
                 ├── settings.cpython-34.pyc 
                 └── urls.cpython-34.pyc
             ├── settings.py
             ├── urls.py
             └── wsgi.py

the / > var > www > wanderwichtl_pythonanywhere_com_wsgi.py

# This file contains the WSGI configuration required to serve up your
# web application at http://Wanderwichtl.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# Below are templates for Django and Flask.  You should update the file
# appropriately for the web framework you're using, and then
# click the 'Reload /yourdomain.com/' button on the 'Web' tab to make your site
# live.

# +++++++++++ VIRTUALENV +++++++++++
# If you're using a virtualenv, you should activate it at the top of your
# wsgi file, using code like this:
#
activate_this_path = '/home/Wanderwichtl/.virtualenvs/django17/bin/activate_this.py'
with open(activate_this_path) as f:
    code = compile(f.read(), activate_this_path, 'exec')
    exec(code, dict(__file__=activate_this_path))



# +++++++++++ CUSTOM WSGI +++++++++++
# If you have a WSGI file that you want to serve using PythonAnywhere, perhaps
# in your home directory under version control, then use something like this:
#
#import os
#import sys
#
#path = '/home/Wanderwichtl/WeingutBender/weingutBender'
#if path not in sys.path:
#    sys.path.append(path)

#from my_wsgi_file import application


# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/Wanderwichtl/mysite/settings.py'
path = '/home/Wanderwichtl/WeingutBender/weingutBender'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'weingutBender.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()


# +++++++++++ FLASK +++++++++++
# Here is a simple Flask app. It is currently serving the default welcome
# page.  You can adapt this if you want.
#
# If you're using a different web framework, you'll need to comment all of this
# out

#from flask import Flask

#application = Flask(__name__)

#@application.route('/')
#def hello_world():
#    return """
#            <html>
#            <head>
#                <title>Python Anywhere hosted web application</title>
#            </head>
#            <body>
#            <h1>Hello, World!</h1>
#            <p>
#                This is the default welcome page for a
#                <a href="https://www.pythonanywhere.com/">PythonAnywhere</a>
#                hosted web application.
#            </p>
#            <p>
#                Find out more about how to configure your own web application
#                by visiting the <a href="https://www.pythonanywhere.com/web_app_setup/">web app setup</a> page
#            </p>
#            </body>
#            </html>"""

Thanks for the awesome post, you gave exactly the right information to help you debug :-)

I think this is a Django version problem; the commented-out code in the WSGI file that you uncommented to get it working with Django and a virtualenv is for Django 1.3. For 1.7 you need to replace this:

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

with this:

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

I think we should update the template WSGI file, as you're not the first person to be tripped up by this.

Hi gang, brief update, we've just released a new way to handle virtualenvs which should avoid the system-site-packages / shadowing issues. There's an overview here and a guide switching across from the old system here.

We've also updated the default manual-config wsgi file so it has correct instructions re: that get_wsgi_application() call...