Python 没有名为“polls.apps.PollsConfigdjango”的模块;Django项目教程2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35484263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:29:51  来源:igfitidea点击:

No module named 'polls.apps.PollsConfigdjango'; Django project tutorial 2

pythondjango

提问by ShogunT

So, I've been following the tutorial steps here https://docs.djangoproject.com/en/1.9/intro/tutorial02/and I got to the step where I am supposed to run this command:

所以,我一直在遵循https://docs.djangoproject.com/en/1.9/intro/tutorial02/ 上的教程步骤,我到了应该运行此命令的步骤:

python manage.py makemigrations polls

When I run it, I get this error:

当我运行它时,我收到此错误:

python manage.py makemigrations polls
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2218, in_find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/tgumm/pythonenv/tutorial/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
  File "/home/tgumm/pythonenv/tutorial/lib/python3.4/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
  File "/home/tgumm/pythonenv/tutorial/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/tgumm/pythonenv/tutorial/lib/python3.4/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
  File "/home/tgumm/pythonenv/tutorial/lib/python3.4/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
  File "/home/tgumm/pythonenv/tutorial/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_an``d_load
  File "<frozen importlib._bootstrap>", line 2221, in _find_and_load_unlocked
ImportError: No module named 'polls.apps.PollsConfigdjango'; 'polls.apps' is not a package

Here are my models:

这是我的模型:

from django.db import models

# Create your models here.
from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

回答by Alasdair

The first problem is this warning in the traceback:

第一个问题是回溯中的这个警告:

No module named 'polls.apps.PollsConfigdjango'

That means that you are missing a comma after 'polls.apps.PollsConfigin your INSTALLED_APPSsetting. It should be:

这意味着您'polls.apps.PollsConfigINSTALLED_APPS设置之后缺少逗号。它应该是:

INSTALLED_APPS = (
    ...
    'polls.apps.PollsConfig',
    'django....',
    ...
)

The second problem is the warning 'polls.apps' is not a package. That suggests that you have installed Django 1.8, but you are following the Django 1.9 tutorial.

第二个问题是警告'polls.apps' is not a package。这表明您已经安装了 Django 1.8,但您正在关注 Django 1.9 教程。

If you are using Django 1.8, then follow the 1.8 tutorial so that you don't hit problems like this. Adding the polls app to INSTALLED_APPSis covered herein the Django 1.8 tutorial. Note that it doesn't use PollsConfig.

如果您使用的是 Django 1.8,请按照 1.8 教程进行操作,以免遇到此类问题。添加投票应用INSTALLED_APPS这里所涉及的Django的1.8教程。请注意,它不使用PollsConfig.

INSTALLED_APPS = (
    ...
    'polls',
)

回答by Rahul_cs12

check for comma "," after 'polls.apps.PollsConfigdjango'

在“polls.apps.PollsConfigdjango”之后检查逗号“,”

回答by CurryChen

ImportError: No module named 'polls.apps.PollsConfigdjango'

You forgot to add ',' in the list

您忘记在列表中添加“,”

回答by iDevFS

I was getting a similar error: ImportError: No module named 'polls'

我收到了类似的错误:ImportError: No module named 'polls'

The cause was that I stored my apps inside the "apps" directory. The solution is to change the code inside apps.py

原因是我将我的应用程序存储在“apps”目录中。解决办法是修改apps.py里面的代码

from:

从:

class PollsConfig(AppConfig):
    name = 'polls'

to ("apps" is the name of my django apps directory):

to(“apps”是我的 django 应用程序目录的名称):

class PollsConfig(AppConfig):
    name = 'apps.polls'

回答by Adam Heaton

Quoted from https://code.djangoproject.com/ticket/27139

引自https://code.djangoproject.com/ticket/27139

Description

描述

In tutorial 02, Writing your first Django app, part 2, typing in

在教程 02,编写您的第一个 Django 应用程序,第 2 部分,输入

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
   ....

will cause an ImportError: No module named 'polls.apps.PollsConfig'; 'polls.apps' is not a package

将导致 ImportError: No module named 'polls.apps.PollsConfig'; 'polls.apps' 不是一个包

This is resolved by instead putting

这是通过而不是把

INSTALLED_APPS = [
    'polls',
   ....

回答by Mohammad Zarei

You just missed a comma after 'polls.apps.PollsConfig'.

你只是在 之后漏了一个逗号'polls.apps.PollsConfig'

Don't worry, it happens for the best of us!

别担心,它发生在我们最好的人身上!

回答by dkjhaj2ee

If we are getting error regarding not a packagethan most of the time it may happen that we missed to add __init__.pyfile within the directory where we are getting this error.

如果我们在not a package大多数情况下遇到错误,可能会发生我们错过__init__.py在出现此错误的目录中添加文件的情况。