Django ImportError: No module named 'app'
Django ImportError: No module named 'app'
我现在一直在使用 django 1.3
。但是想从 1.8.1
和 python3.4
的最新版本开始,我已经开始了这个项目。
这是我的目录结构:
├── myapp
│ ├── common
│ │ ├── admin.py
│ │ ├── __init__.py
│ │ ├── migrations
│ │ │ └── __init__.py
│ │ ├── models.py
│ │ ├── tests.py
│ │ └── views.py
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── templates
│ │ └── home.html
│ ├── urls.py
│ └── wsgi.py
└── manage.py
*urls.py
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', "home", name='homepage'),
]
views.py
from django.shortcuts import render
def home(request):
render(request, "home.html", {"text":"Hello World!!!"})
设置
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'common',
)
运行服务器出现以下错误
$ 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/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/local/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.4/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.4/site-packages/django/apps/config.py", line 86, in create
module = import_module(entry)
File "/usr/local/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 2224, in _find_and_load_unlocked
ImportError: No module named 'common'
manage.py
的路径对我来说看起来很不寻常。 manage.py
通常在顶级项目目录中。您遇到的问题源于以下事实:django 将搜索路径(即 sys.path
)置于 manage.py
脚本的路径中。
在你的例子中,common
的相对模块路径实际上是myapp.common
。
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp.common',
)
当然 myapp
实际上是 django 术语中的 项目 。
我现在一直在使用 django 1.3
。但是想从 1.8.1
和 python3.4
的最新版本开始,我已经开始了这个项目。
这是我的目录结构:
├── myapp
│ ├── common
│ │ ├── admin.py
│ │ ├── __init__.py
│ │ ├── migrations
│ │ │ └── __init__.py
│ │ ├── models.py
│ │ ├── tests.py
│ │ └── views.py
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── templates
│ │ └── home.html
│ ├── urls.py
│ └── wsgi.py
└── manage.py
*urls.py
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', "home", name='homepage'),
]
views.py
from django.shortcuts import render
def home(request):
render(request, "home.html", {"text":"Hello World!!!"})
设置
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'common',
)
运行服务器出现以下错误
$ 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/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/local/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.4/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.4/site-packages/django/apps/config.py", line 86, in create
module = import_module(entry)
File "/usr/local/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 2224, in _find_and_load_unlocked
ImportError: No module named 'common'
manage.py
的路径对我来说看起来很不寻常。 manage.py
通常在顶级项目目录中。您遇到的问题源于以下事实:django 将搜索路径(即 sys.path
)置于 manage.py
脚本的路径中。
在你的例子中,common
的相对模块路径实际上是myapp.common
。
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp.common',
)
当然 myapp
实际上是 django 术语中的 项目 。