[英]ImportError: No module named TestModel when use manage.py to runserver
I user Django create a project named testdj
, and also in it I created a APP named TestModel
, see my tree: 我使用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
But when I cd
to the testdj/
directory, I want to run the wsgi server: 但是,当我cd
到testdj/
目录时,我想运行wsgi服务器:
$ python manage.py runserver
I get the below error: 我收到以下错误:
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
It says ImportError: No module named TestModel
in it. 它说ImportError: No module named TestModel
。
In the testdj/testdj/settins.py
, I have added the TestModel as APP: 在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',
]
Bro bro bro, why arent you following any tutorial? 兄弟,兄弟,为什么不让您跟随任何教程? you messed up 你搞砸了
the app folder and the main testdj folder will be there where you have manage.py
, app文件夹和主testdj文件夹将位于您拥有manage.py
,
at this point you have inserted the testmodel inside the testdj folder where settings.py
is , 至此,您已经将testmodel插入了testdj文件夹中,其中settings.py
是,
so take it out and place at the place of manage.py, the whole folder, 因此,将其取出并放在整个文件夹的manage.py位置,
then run 然后跑
python manage.py runserver
This ( https://stackoverflow.com/a/44673465/6563567 ) will work but it is not wrong to move your app to settings.py level. 这( https://stackoverflow.com/a/44673465/6563567 )可以工作,但是将您的应用程序移到settings.py级别也没错。 You just have to tell django where to find the apps. 您只需要告诉django在哪里可以找到应用程序。 Since your apps are not in manage.py level, your installed apps should look like this: 由于您的应用程序不在manage.py级别,因此已安装的应用程序应如下所示:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'testdj.TestModel',
]
Further you can add 'testdj', in installed apps if you want to add views.py file in setting level. 此外,如果要在设置级别添加views.py文件,则可以在已安装的应用程序中添加“ testdj”。 Remember that your app is a python package and each file inside the app is a python module and that's how django uses them. 请记住,您的应用程序是一个python程序包,应用程序中的每个文件都是一个python模块,这就是django使用它们的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.