Forums

ImportError cannot import name patterns

Hi,

I seem to be having a import problem and i can't really figure out where its going wrong at the moment...

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response   101.                             request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  250.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_url_patterns
  279.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_urlconf_module
  274.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/Haze/sc/urls.py" in <module>
  2. from django.conf.urls import patterns, include, url

Exception Type: ImportError at /
Exception Value: cannot import name patterns

It appears that these are the imports required for Django 1.4 whereas PA's default Django version is currently 1.3. See this SO answer for some more detail.

I would suggest that if your application is already written against 1.4 then it doesn't make sense to "downgrade" it because you'll only need to change it back again if 1.4 becomes standard, which it eventually presumably will (but it's hard to know whether that's weeks, months or years away right now). Fortunately, you can install and run your own personal version of 1.4 on PA as detailed in these instructions.

Hi Cartroo,

Thanks for the quick answer! Yes indeed, i been working with Django 1.5 , I will look into the virtuelenvdemo and report back! Thanks again!

Ah yes, I'd forgotten 1.5 had been released - I don't use Django much myself so I don't tend to keep up to date with its release schedule.

If you have any issues getting virtualenv working, or any other aspect, then hopefully me or someone else here can help out with that too.

I got virtualenv working now, but there is still a import error for some reason...

 Traceback:
File "/home/Haze/.virtualenvs/django15/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/home/Haze/.virtualenvs/django15/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/home/Haze/.virtualenvs/django15/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/Haze/.virtualenvs/django15/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/home/Haze/.virtualenvs/django15/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/Haze/sc/urls.py" in <module>
  8. admin.autodiscover()
File "/usr/local/lib/python2.7/dist-packages/mezzanine/boot/__init__.py" in autodiscover
  76.     django_autodiscover(*args, **kwargs)
File "/home/Haze/.virtualenvs/django15/lib/python2.7/site-packages/django/contrib/admin/__init__.py" in autodiscover
  25.         mod = import_module(app)
File "/home/Haze/.virtualenvs/django15/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)

Exception Type: ImportError at /
Exception Value: No module named main

So i went and checked but that module is in the "sc" map...do i have to put the "main" in the virtualenvs?

What you need in the virtualenv should only be the libraries you depend on (and any dependencies that they themselves have, but pip should take care of that for you).

It looks like it's trying to do a standard Python import to find your code - perhaps you need to add the directory containing your code to sys.path in your WSGI file?

Heya,

Yeh that's what i thought too, i installed through pip in the console like i did locally and i have changed the WSGI That looks as follows, but it seems its not grabbing the main map which is in home/Haze/sc/main

But seems im working on it might not work sometimes...

activate_this = '/home/Haze/.virtualenvs/django15/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import os
import sys

path = '/home/Haze/'
if path not in sys.path:
    sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'sc.settings'

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

If i ever figure this out ill put the answer here ofcourse, so frustrating but fun for some reason :)

UPDATE: You were right! I had not put in the sys.path and now it's working fine :) This is how it the wsgi looks like now

activate_this = '/home/Haze/.virtualenvs/django15/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import os
import sys
sys.path.insert(0, '/home/Haze/sc')

path = '/home/Haze/'
if path not in sys.path:
    sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'sc.settings'

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