Forums

ImportError at / No module named urls

Hello everyone. I am working on a django project, which runs perfectly at local machine. I changed my wsgi file as pythonanywhere requires and tried to enter the web-site. It gives me an ImportError for urls, which is pretty weird. I tried to debug it following this article, but I didn't succeed: https://help.pythonanywhere.com/pages/DebuggingImportError.

Here is the short description of the Error:

Request Method: GET
Request URL: http://initiativa.pythonanywhere.com/

Django Version: 1.9
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'crispy_forms',
 'registration',
 'newsletter')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')



Traceback:

File "/home/initiativa/.virtualenvs/django19/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  134.                 resolver_match = resolver.resolve(request.path_info)

File "/home/initiativa/.virtualenvs/django19/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  374.             for pattern in self.url_patterns:

File "/home/initiativa/.virtualenvs/django19/local/lib/python2.7/site-packages/django/utils/functional.py" in __get__
  33.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/initiativa/.virtualenvs/django19/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  417.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/home/initiativa/.virtualenvs/django19/local/lib/python2.7/site-packages/django/utils/functional.py" in __get__
  33.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/initiativa/.virtualenvs/django19/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  410.             return import_module(self.urlconf_name)

File "/usr/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)

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

And here is my wsgi.py code:

import os
import sys

path = '/home/initiativa/trydjango18/src/'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'trydjango18.src.trydjango18.settings'

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

And the tree of my app (without statics and templates):

.
├── LICENSE
├── README.md
├── __init__.py
├── __init__.pyc
├── src
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── db.sqlite3
│   ├── db2.sqlite3
│   ├── manage.py
│   ├── newsletter
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── admin.py
│   │   ├── admin.pyc
│   │   ├── forms.py
│   │   ├── forms.pyc
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── 0001_initial.pyc
│   │   │   ├── __init__.py
│   │   │   └── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── polls_urls.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   ├── views.py
│   │   └── views.pyc
│   └── trydjango18
│       ├── __init__.py
│       ├── __init__.pyc
│       ├── settings.py
│       ├── settings.pyc
│       ├── urls.py
│       ├── urls.pyc
│       ├── views.py
│       ├── views.pyc
│       ├── wsgi.py
│       └── wsgi.pyc

Did somebody meet the same problem?

It looks like you missed the bit where you compare your directory layout to your sys.path and your DJANGO_SETTINGS value. Here it is again followed by working through your situation as an example:


Django-specific issues

In Django, we sometimes don't import modules directly in the WSGI file, but we do specify a dot-notation import path to the settings in an environment variable. Django will try and import it later, so you need to get this right as if it was an import.

eg:

path = "/home/myusername/myproject"
if path not in sys.path
    sys.path.append(path)

os.environ("DJANGO_SETTINGS_MODULE") = "myproject.settings"

What this implies is that you have a directory tree that looks like this:

/home/myusername
`-- myproject/
    |-- __init__.py
    `-- myrpoject/
        |-- __init__.py
        `-- settings.py

NB - this is the typical django project structure for django versions greater than 1.4. You will probably need to use a virtualenv to get this to work, see VirtualenvForNewerDjango


So here's how it works with your stuff:

  • Your sys.path is /home/initiativa/trydjango18/src/, which looks correct
  • Your settings.py file is at /home/initiativa/trydjango18/src/trydjango18/settings.py
  • So to get from your sys.path to your settings file, you need a DJANGO_SETTINGS_MODULE of trydjango18.settings

Is there some way we could make the documentation more clear on this point? We'd really like to improve our documentation to make it easier for our users to get their webapps working.

I've done exactly as you said. The error persists. I tried several paths but it still doesn't work.

Have you tried running python manage.py shell with your virtualenv activated? What happens? If it starts, can you import your app and its models?

i want to install python 3.5 in pythonanywhere ..how can i do this?

python3.5 is already installed