Question

I'm working through Effective Django's tutorial series. I'm currently having an issue trying to create a custom form to use in an app. I created the forms.py file as instructed in this part of the tutorial, and made the alterations to my views.py file. My directory structure looks like this:

(project root)
|
├── address.db
├── addressbook
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── urls.py
│   └── wsgi.py
├── contacts
│   ├── __init__.py
│   ├── admin.py
│   ├── forms.py
│   ├── models.py
│   ├── templates
│   ├── tests.py
│   └── views.py
├── manage.py
└── requirements.txt

The problem is that when I try to load the site I get the following error:

Traceback (most recent call last):
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/site-packages/django/core/handlers/base.py", line 101, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/site-packages/django/core/urlresolvers.py", line 318, in resolve
    for pattern in self.url_patterns:
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/site-packages/django/core/urlresolvers.py", line 346, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/site-packages/django/core/urlresolvers.py", line 341, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/Users/wtodom/.virtualenvs/tutorial/lib/python3.3/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1586, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1534, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1024, in load_module
  File "<frozen importlib._bootstrap>", line 1005, in load_module
  File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 870, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/Users/wtodom/.virtualenvs/tutorial/addressbook/urls.py", line 4, in <module>
    import contacts.views
  File "/Users/wtodom/.virtualenvs/tutorial/contacts/views.py", line 5, in <module>
    import forms
ImportError: No module named 'forms'

Line 5 is the line in views.py where I import the forms.py file. The code snippet (with a few lines of buffer) is:

from django.shortcuts import render
from django.views.generic import ListView, CreateView, UpdateView, DeleteView, DetailView
from django.core.urlresolvers import reverse
from contacts.models import Contact
import forms


class ListContactView(ListView):

    model = Contact
    template_name = 'contact_list.html'

As you can see from the directory tree, the forms.py file should be accessible. I'm not sure why I'm getting the error. Any ideas?

Edit 1

I tried changing the import to from forms import ContactForm before posting originally. I still got the same error:

  File "/Users/wtodom/.virtualenvs/tutorial/contacts/views.py", line 5, in <module>
    from forms import ContactForm
ImportError: No module named 'forms'
Was it helpful?

Solution

As far as I know there were changes in import system in Python 3. Just be more specific about what you want to import. I assume you want to import forms.py from contacts so

from contacts import forms

or you can try

import .forms

OTHER TIPS

It's propper to do it like this:

from forms import FormClassName

Or you can

from forms import *

but it's not nessesary. From other apps you must

from contacts.forms import FormClassName # or wildcard '*'

When importing the directory which python searches for import is project root and there is no module named as forms.

So if you wanna test create a file named forms in project root and it will import it.

That way following will surely work

from contacts.forms import FormClassName # or wildcard '*'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top