Django可重用的应用程序教程,ImportError:没有名为'polls'的模块

时间:2015-09-25 15:43:05

标签: python django

我试图关注 this tutorial 重新使用民意调查应用。但在我成功打包应用程序并安装软件包之后:

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...



当我使用 $python manage.py runserver运行服务器时,会出现以下错误:

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'



正如教程所暗示的,到现在为止,我应该能够正确运行服务器。我无法弄清楚为什么我的工作不起作用。

我尝试在mysite / settings.py中修改已安装的应用,并将polls更改为django-polls,但再次出现相同的错误。

这是我的 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/'

提前致谢!

2 个答案:

答案 0 :(得分:4)

我遇到了同样的问题。

问题产生于我在虚拟环境中运行以下代码

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Download(string id)
    {
        string path = @"https://filestorageideagen.blob.core.windows.net/documentuploader/";

        return View(path + id);
    }

问题来自同时使用--user关键字和虚拟环境。我相信应该使用一个或另一个。

所以,我卸载它并重新安装在我的虚拟环境中(在mysite目录中),而不使用--source关键字。

这要求我使用完整路径名。在我的系统中,mysite和django-polls都存在于〜/ Documents中,但对你来说可能会有所不同。

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

在此之后,一切正常。希望这有用!

答案 1 :(得分:1)

朱莉娅 - 我用更新的django做了同样的事情 - 1.9.1,教程代码看起来像这样:binascii.unhexlify并且得到了同样的错误。我将代码改为看起来像你的代码,即rbytes = bytes.fromhex(rhexonly) # Returns b'\xea\xb4\x80' 并且它有效。版本问题?