没有名为' polls.apps.PollsConfigdjango&#39 ;; Django项目教程2

时间:2016-02-18 14:32:47

标签: python django

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

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

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

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)

以下是我的模特:

import javax.swing.*;
import awt.Graphics;

public class basicWindow {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setTitle("Basic Window");
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel();

        frame.add(panel);

        frame.setVisible(true);

    }

    public void paint(Graphics g) {

        g.fillRect(5, 15, 50, 75);

    }

}

9 个答案:

答案 0 :(得分:72)

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

No module named 'polls.apps.PollsConfigdjango'

这意味着您在'polls.apps.PollsConfig设置中INSTALLED_APPS后缺少逗号。它应该是:

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

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

如果您使用的是Django 1.8,请按照1.8教程进行操作,以免出现类似问题。在Django 1.8教程中,将民意调查应用添加到INSTALLED_APPScovered here。请注意,它不使用PollsConfig

INSTALLED_APPS = (
    ...
    'polls',
)

答案 1 :(得分:23)

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

答案 2 :(得分:23)

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

您忘了在列表中添加','

答案 3 :(得分:1)

我遇到了类似的错误: ImportError:没有名为'polls'的模块

原因是我将我的应用程序存储在“apps”目录中。 解决方案是更改apps.py

中的代码

从:

class PollsConfig(AppConfig):
    name = 'polls'

to(“apps”是我的django apps目录的名称):

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

答案 4 :(得分:1)

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

描述

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

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

将导致ImportError:没有名为&#39; polls.apps.PollsConfig&#39 ;;的模块&#39; polls.apps&#39;不是包

通过改为

来解决这个问题
INSTALLED_APPS = [
    'polls',
   ....

答案 5 :(得分:1)

如果您来自Django,这就是问题所在,您只是在'polls.apps.PollsConfig'之后错过了逗号所以应该是

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    ....
]

答案 6 :(得分:0)

'polls.apps.PollsConfig'之后,您刚错过了逗号。

别担心,它对我们所有人来说都是最好的!

答案 7 :(得分:0)

如果以上/以下内容均不能解决您的问题,并且出现相同的错误,请确保您的顶级目录中没有__init__.py。希望这对外面的人有帮助!

答案 8 :(得分:-1)

如果在大多数情况下,如果我们遇到关于not a package的错误,则可能是因为我们错过了在发生此错误的目录中添加__init__.py文件的情况。