python - Django - 导入错误 : No module named city

标签 python django linux importerror

Django 一直告诉我这个,即使在重新编码所有内容之后,我似乎无法在 Google 上找到帮助。项目文件夹如下:

$ tree
.
|-- project
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.py~
|   |-- settings.pyc
|   |-- urls.py
|   |-- urls.pyc
|   |-- wsgi.py
|   `-- wsgi.pyc
|-- city
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- models.py
|   |-- models.pyc
|   |-- tests.py
|   `-- views.py
|-- course
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- models.py
|   |-- models.pyc
|   |-- tests.py
|   `-- views.py
|-- manage.py
|-- perk
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- models.py
|   |-- models.pyc
|   |-- static
|   |-- tests.py
|   `-- views.py
|-- preuniversity
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- models.py
|   |-- models.py~
|   |-- models.pyc
|   |-- tests.py
|   `-- views.py
`-- user
    |-- __init__.py
    |-- __init__.pyc
    |-- models.py
    |-- models.py~
    |-- models.pyc
    |-- tests.py
    `-- views.py

结构没有变,除了models.py我也没有修改任何东西,就是:

$ cat city/models.py
from django.db import models

class State(models.Model):
    acronym = models.CharField(max_length=2)
    name = models.CharField(max_length=32)

class City(models.Model):
    name = models.CharField(max_length=32)
    state = models.ForeignKey(State)

我只是按照Django的教程,不多不少,也没有解决办法。我在每个应用程序上都使用了 startapp(而不仅仅是从另一个应用程序复制和粘贴)并且应用程序名称在 settings.py 中。

如果我删除所有对城市的引用(外键、导入和从 installed_apps 中删除),它会提示下一个应用程序(类(class))。

最佳答案

我假设您使用的是 django 1.4 或更高版本,因为您的项目文件夹中有这些内容。

您是否使用django-admin.py startproject 创建了顶级目录?我没有在顶层看到 __init__.py 文件,尽管 project 目录包含 settings.pyurl。 py 在那里。在顶层创建一个名为 __init__.py 的空文件(manage.py 的同级文件),看看是否有帮助。

关于python - Django - 导入错误 : No module named city,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17432238/

相关文章:

c++ - 静态库,链接到共享库

python - python Python : conda update resulted in AssertionError() and No module named 'conda_package_handling'

Python 循环缺少结果

jquery - Django 自动完成灯 : field not populated

python - 如何在没有 .py 扩展名的情况下运行 argparse python 程序?

创建一个新的瘦进程、 fork 还是线程?

python - 使用 vscode 新调试器调试 python 版本 < 3.5

Python - 尝试使用意外的 mimetype : 解码 JSON

mysql - Django/MySQL 错误

Javascript 模块不可用模板不是从基本 Django 模板继承的