python manage.py cause an ImportError: module not found

python manage.py cause an ImportError: module not found

当尝试通过 运行 python manage.py runserver 编译 heroku 网络应用程序时,我收到以下错误:

Unhandled exception in thread started by <function wrapper at 0x1046deb18>
Traceback (most recent call last):
  File "/Users/User/Desktop/App/App/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/User/Desktop/App/App/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/Users/User/Desktop/App/App/lib/python2.7/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/User/Desktop/App/App/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/User/Desktop/App/App/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/User/Desktop/App/App/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/User/Desktop/App/App/lib/python2.7/site-packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named profiles

主要问题是 No module named profiles。配置文件在 app.py:

中定义
from __future__ import unicode_literals

from django.apps import AppConfig

class ProfilesConfig(AppConfig):
    name = 'profiles'

并在 urls.py

中调用
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.contrib import admin

from profiles import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home, name='home'),
    url(r'^one/', views.one, name='one'),
    url(r'^two/', views.two, name='two'),
    url(r'^three/', views.three, name='three'),
    url(r'^four/', views.four, name='four'),
    url(r'^five/', views.five, name='five'),
]

配置文件也添加到 settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    # Disable Django's own staticfiles handling in favour of WhiteNoise, for
    # greater consistency between gunicorn and `./manage.py runserver`. See:
    # http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    'profiles',
]

Profiles 是我项目目录中的一个文件夹,其中包含 views.py 文件,我需要该文件才能在 Django 中显示我的网址。我试过修改导入语句,摆弄 settings.py,但没有任何效果,这就是显示的错误。

项目结构:

|—App
    |—App
        |—__init__.py
        |—__init__.pyc
        |—bin
        |— include
        |—lib
        |—profiles
            |—__init__.py
            |—__init__.pyc
            |— admin.py
            |— admin.pyc
            |— apps.py
            |— models.pyc
            |— templates
            |— tests.py
            |— views.py
            |— views.pyc
        |—static
        |—staticfiles
        |— urls.py
        |— urls.pyc
        |—wsgi.py
        |—wsgi.pyc
        |—db.sqlite3
        |—manage.py
        |—Procfile
        |—requirements.txt
        |—runtime.txt

知道为什么会出现 ImportError: No module named profiles 吗?

正如@dirkgroten 正确指出的那样,问题最终变成了我的 Heroku 目录的结构不正确。我使用此 repo https://github.com/heroku/heroku-django-template 中描述的结构组织目录并解决了问题。