Django ImportError:没有名为'app'的模块

时间:2022-04-11 23:16:17

I have been using django 1.3 by now. But wanted to start with latest version 1.8.1 with python3.4I have started with the project. Here is my directory structure:

我一直在使用django 1.3。但是想从最新版本1.8.1开始,用python3.4I已经开始了这个项目。这是我的目录结构:

├── 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!!!"})

settings

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'common',
)

Getting following error on runserver

在runserver上获取以下错误

$ 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'

1 个解决方案

#1


The path of managed.py looks unusual to me. managed.py is usually at the top level project directory. The problem you encounter stems from the fact that django roots the search path (i.e. sys.path) at the path of the managed.py script.

managed.py的路径对我来说很不寻常。 managed.py通常位于*项目目录中。您遇到的问题源于django将搜索路径(即sys.path)作为managed.py脚本路径的根源。

In your case, common's relative module path is actually myapp.common.

在您的情况下,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',
)

Of course myapp is then actually the project in django lingo ...

当然myapp实际上是django lingo中的项目......

#1


The path of managed.py looks unusual to me. managed.py is usually at the top level project directory. The problem you encounter stems from the fact that django roots the search path (i.e. sys.path) at the path of the managed.py script.

managed.py的路径对我来说很不寻常。 managed.py通常位于*项目目录中。您遇到的问题源于django将搜索路径(即sys.path)作为managed.py脚本路径的根源。

In your case, common's relative module path is actually myapp.common.

在您的情况下,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',
)

Of course myapp is then actually the project in django lingo ...

当然myapp实际上是django lingo中的项目......