简体   繁体   中英

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

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!

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!

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, ie, 'polls', and it works. Version issue?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM