Django说-没有名为'blog'的模块 [英] Django says - No module named 'blog'
问题描述
将我的博客应用添加到settings.py
的INSTALLED_APPS部分时,出现"ModuleNotFoundError: No module named 'blog'"
错误.我确定这与我在INSTALLED_APPS下添加博客"应用程序的方式有关.当我从INSTALLED_APPS
中删除博客"引用时,错误消失了.看来Django
找不到我的博客应用程序的目录?
I am getting "ModuleNotFoundError: No module named 'blog'"
error when add my blog app to the INSTALLED_APPS section of settings.py
. I have determined that it has something to do with the way I have added the "blog" app under INSTALLED_APPS. When I remove the 'blog' reference from INSTALLED_APPS
error goes away. It looks like that Django
is unable to find directory for my blog app?
我做了另一件事,那就是使用:
I have done one thing differently and that is use:
python manage.py startapp blog /myproject
此处的区别是指定/myproject
目录,而不使用:
Difference here is specifying the /myproject
directory and not using:
python manage.py startapp blog
将其放置在根目录myproject
下.我想避免在根文件夹中添加应用程序目录,以便保持井井有条.但是看起来Django不喜欢这个,或者我没有在INSTALLED_APPS
部分正确地引用它?
Which will place it under root directory myproject
. I wanted to avoid adding app directory in the root folder so i stay more organized. But it looks like Django does not like this or i am not referencing this correctly in the INSTALLED_APPS
section?
我的项目目录如下:
myproject/
├── myproject
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── blog
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── migrations
│ │ │ └── __init__.py
│ │ ├── models.py
│ │ ├── tests.py
│ │ └── views.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── db.sqlite3
└── manage.py
在我的settings.py
内部,我已经设置了我的应用程序blog
:
Inside my settings.py
I have setup my app blog
:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
推荐答案
Django需要能够导入您的应用程序,通常这意味着包括相对于根目录'myproject.blog'
的完整路径.
Django needs to be able to import your application, usually this means including the full path relative to the root directory 'myproject.blog'
.
您可以将<full_path_to_your_project>/myproject/myproject
添加到PYTHONPATH
,以便可以导入blog
,但我不建议这样做
You could add <full_path_to_your_project>/myproject/myproject
to PYTHONPATH
so that you can import blog
, but I would not recommend it
这篇关于Django说-没有名为'blog'的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!