簡體   English   中英

ImportError:使用manage.py來運行服務器時,沒有名為TestModel的模塊

[英]ImportError: No module named TestModel when use manage.py to runserver

我使用Django創建了一個名為testdj的項目,並且還在其中創建了一個名為TestModel的APP,請參見我的樹:

aircraftdeMacBook-Pro:TestPython ldl$ tree testdj/
testdj/
├── db.sqlite3
├── manage.py
├── templates
│   ├── base.html
│   ├── ext-1.html
│   ├── hello.html
│   └── index.html
└── testdj
    ├── TestModel
    │   ├── __init__.py
    │   ├── admin.py
    │   ├── apps.py
    │   ├── migrations
    │   │   └── __init__.py
    │   ├── models.py
    │   ├── tests.py
    │   └── views.py
    ├── __init__.py
    ├── __init__.pyc
    ├── helloworld.py
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    ├── urls.pyc
    ├── view.py
    ├── view.pyc
    ├── wsgi.py
    └── wsgi.pyc

但是,當我cdtestdj/目錄時,我想運行wsgi服務器:

$ python manage.py runserver

我收到以下錯誤:

Unhandled exception in thread started by <function wrapper at 0x10b80ede8>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/utils/autoreload.py", line 250, in raise_last_exception
    six.reraise(*_exception)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named TestModel

它說ImportError: No module named TestModel


testdj/testdj/settins.py ,我將TestModel添加為APP:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'TestModel',
]

兄弟,兄弟,為什么不讓您跟隨任何教程? 你搞砸了

app文件夾和主testdj文件夾將位於您擁有manage.py

至此,您已經將testmodel插入了testdj文件夾中,其中settings.py是,

因此,將其取出並放在整個文件夾的manage.py位置,

然后跑

python manage.py runserver

這( https://stackoverflow.com/a/44673465/6563567 )可以工作,但是將您的應用程序移到settings.py級別也沒錯。 您只需要告訴django在哪里可以找到應用程序。 由於您的應用程序不在manage.py級別,因此已安裝的應用程序應如下所示:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'testdj.TestModel',
]

此外,如果要在設置級別添加views.py文件,則可以在已安裝的應用程序中添加“ testdj”。 請記住,您的應用程序是一個python程序包,應用程序中的每個文件都是一個python模塊,這就是django使用它們的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM