dragonbean
2019-04-25 13:26
看视频import_data的代码,自己也写了一个import_data工具自动把文章导入数据库,但是好像导入models模块出问题了
下面是我的代码:
import os import sys sys.path.append(r'E:\Python\Pycharm\django_introduction') from blog.models import Article
import_data.py 在tools目录下,tools和templates同级(models.py在templates下)
报错提示是:
Traceback (most recent call last): File "E:/Python/Pycharm/django_introduction/tools/import_data.py", line 5, in <module> from blog.models import Article File "E:\Python\Pycharm\django_introduction\blog\models.py", line 5, in <module> class Article(models.Model): File "D:\Python\Python37-32\lib\site-packages\django\db\models\base.py", line 100, in __new__ app_config = apps.get_containing_app_config(module) File "D:\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config self.check_apps_ready() File "D:\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 127, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
麻烦老师帮忙看一下是怎么回事,嘻嘻嘻
是不是新建的Django应用没有配置到settings.py的INSTALLED_APPS属性里面呀?
在ide中执行python程序,都已经在默认的项目路径中,所以直接执行是没有问题的。但是在cmd中执行程序,所在路径是python的搜索路径,如果涉及到import引用就会报类似ImportError: No module named xxx这样的错误,解决方法:
在报错的模块中添加:
import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
这样就可以解决了
/home/ns/anaconda3/envs/flask01/bin/python /home/ns/PycharmProjects/django_introduction/tools/import_data.py
Traceback (most recent call last):
File "/home/ns/PycharmProjects/django_introduction/tools/import_data.py", line 9, in <module>
django.setup()
File "/home/ns/anaconda3/envs/flask01/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ns/anaconda3/envs/flask01/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/ns/anaconda3/envs/flask01/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/home/ns/anaconda3/envs/flask01/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
我也遇到同样问题, 应该是版本不兼容问题.
三小时带你入门Django框架 新版上架
25629 学习 · 248 问题
相似问题
回答 2
回答 4