Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django reusable apps tutorial, ImportError: No module named 'polls'

Tags:

python

django

I was trying to follow this tutorial to reuse the polls app. But after I successfully packaged the app and installed the package:

Unpacking /home/julia/Documents/django-polls/dist/django-polls-0.1.tar.gz
  Running setup.py (path:/tmp/pip-qkUTOt-build/setup.py) egg_info for package from file:///home/julia/Documents/django-polls/dist/django-polls-0.1.tar.gz

    warning: no files found matching '*' under directory 'docs'
Installing collected packages: django-polls
  Running setup.py install for django-polls

    warning: no files found matching '*' under directory 'docs'
Successfully installed django-polls
Cleaning up...



When I run the server with $python manage.py runserver, the following error appears:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/usr/local/lib/python3.4/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line 86, in create
    module = import_module(entry)
  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 2201, in _find_and_load_unlocked
ImportError: No module named 'polls'



As the tutorial implies, by now, I should be able to run the server correctly. I couldn't figure out why mine doesn't work.

I tried to modified installed apps in mysite/settings.py and change polls to django-polls, but the same error appears again.

Here is my setup.py:

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.8.4.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'y&0-n0m*si$*br43bp5+_0nu%@mmp3^7#tj5^5p_a0z_1y_w0i'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

MIDDLEWARE_CLASSES = (
    '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',
)

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/New_York'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

Thanks in advance!

like image 358
Kennard Avatar asked Sep 25 '15 15:09

Kennard


People also ask

How do I create a poll app in Django?

In tutorial 02, Writing your first Django app, part 2, typing in INSTALLED_APPS = [ 'polls.apps.PollsConfig', .... INSTALLED_APPS = [ 'polls', .... Since Django 1.9, the startapp command will generate an apps.py file so the instructions should be correct.

Why are the s at the end of “apps” missing in Django?

You are missing the s at the end of “apps” The dotted path in your INSTALLED_APPS setting needs to correctly point to your django app package, or the app config class inside your python package. You have a package named “polls” which contains a module called “apps” which has a class in it called “PollsConfig”

Should I prepend Django- to my package name?

It’s often useful to prepend django- to your module name when creating a package to distribute. This helps others looking for Django apps identify your app as Django specific. Application labels (that is, the final part of the dotted path to application packages) must be unique in INSTALLED_APPS .

What is an app in Django?

Each application you write in Django consists of a Python package that follows a certain convention. Django comes with a utility that automatically generates the basic directory structure of an app, so you can focus on writing code rather than creating directories. What’s the difference between a project and an app?


2 Answers

I was running into the same exact problem.

The problem arose from me running the following code while already inside a virtual environment:

pip install --user django-polls/dist/django-polls-0.1.tar.gz

The problem comes from using both the --user keyword, and a virtual enviornment. I believe one should be used or the other.

So, I uninstalled it and reinstalled on my virtual-enviornment (while inside the mysite directory), without using the --source keyword.

This required me to use the full path name. On my system, both mysite and django-polls live in ~/Documents, but this will probably be different for you.

(mysite) me:~/Documents/$ pip uninstall django-polls
(mysite) me:~/Documents/mysite$ pip install ~/Documents/django-polls/dist/django-polls-0.1.tar.gz

After this, everything worked correctly. Hope this was helpful!

like image 160
Joboman555 Avatar answered Sep 28 '22 09:09

Joboman555


Julia - I was doing the same thing with a newer django - 1.9.1, and the tutorial code looked like this: 'polls.apps.PollsConfig', and getting the same error you got. I switched the code to look like yours, i.e, 'polls', and it works. Version issue?

like image 29
Jeff Ericson Avatar answered Sep 28 '22 10:09

Jeff Ericson