在使用django框架搭建web中如果需要单独运行py脚本出现AppRegistryNotReady("Apps aren't loaded yet.")问题
2018-01-25 20:44
2561 查看
使用django2.0时发现能够访问web,但是单独运行某一个py文件时会出现如下错误:
Traceback (most recent call last):
File "D:/Python_work/pycharm/py_work/mysite/mysite/urls.py", line 35, in <module>
path('admin/', admin.site.urls),
File "D:\Anaconda3\lib\site-packages\django\contrib\admin\sites.py", line 285, in urls
return self.get_urls(), 'admin', self.name
File "D:\Anaconda3\lib\site-packages\django\contrib\admin\sites.py", line 237, in get_urls
from django.contrib.contenttypes import views as contenttype_views
File "D:\Anaconda3\lib\site-packages\django\contrib\contenttypes\views.py", line 2, in <module>
from django.contrib.contenttypes.models import ContentType
File "D:\Anaconda3\lib\site-packages\django\contrib\contenttypes\models.py", line 134, in <module>
class ContentType(models.Model):
File "D:\Anaconda3\lib\site-packages\django\db\models\base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "D:\Anaconda3\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "D:\Anaconda3\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.解决方案:
在urls.py的开始加上
import os,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup() 然后单独运行改脚本就不出出错了。
如果在命令行中需要导入其他py模块,这是需要先导入该urls模块,不然出现导入不成功的情况:
会出现一下错误from learn.models import Person
Traceback (most recent call last):
File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-8-7faa32097dbe>", line 1, in <module>
from learn.models import Person
File "D:\Python_work\pycharm\PyCharm Community Edition 2017.1.5\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "D:\Python_work\pycharm\py_work\mysite\learn\models.py", line 7, in <module>
class Person(models.Model):
File "D:\Anaconda3\lib\site-packages\django\db\models\base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "D:\Anaconda3\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "D:\Anaconda3\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.
但是先导入该urls模块就能成功
Traceback (most recent call last):
File "D:/Python_work/pycharm/py_work/mysite/mysite/urls.py", line 35, in <module>
path('admin/', admin.site.urls),
File "D:\Anaconda3\lib\site-packages\django\contrib\admin\sites.py", line 285, in urls
return self.get_urls(), 'admin', self.name
File "D:\Anaconda3\lib\site-packages\django\contrib\admin\sites.py", line 237, in get_urls
from django.contrib.contenttypes import views as contenttype_views
File "D:\Anaconda3\lib\site-packages\django\contrib\contenttypes\views.py", line 2, in <module>
from django.contrib.contenttypes.models import ContentType
File "D:\Anaconda3\lib\site-packages\django\contrib\contenttypes\models.py", line 134, in <module>
class ContentType(models.Model):
File "D:\Anaconda3\lib\site-packages\django\db\models\base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "D:\Anaconda3\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "D:\Anaconda3\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.解决方案:
在urls.py的开始加上
import os,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup() 然后单独运行改脚本就不出出错了。
如果在命令行中需要导入其他py模块,这是需要先导入该urls模块,不然出现导入不成功的情况:
会出现一下错误from learn.models import Person
Traceback (most recent call last):
File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-8-7faa32097dbe>", line 1, in <module>
from learn.models import Person
File "D:\Python_work\pycharm\PyCharm Community Edition 2017.1.5\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "D:\Python_work\pycharm\py_work\mysite\learn\models.py", line 7, in <module>
class Person(models.Model):
File "D:\Anaconda3\lib\site-packages\django\db\models\base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "D:\Anaconda3\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "D:\Anaconda3\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.
但是先导入该urls模块就能成功
相关文章推荐
- django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.的解决办法
- django中使用celery 任务AppRegistryNotReady: Apps aren't loaded yet.
- django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.的解决办法
- pycharm运行Django发生AppRegistryNotReady: Apps aren't loaded yet.
- 用源码安装xadmin出现django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
- (上一篇的问题解决后又遇到的新问题)在eclipse中导入工程后运行任意文件出现"The selection is not within a valid module"
- 关于Qt开发MySQL程序移植后出现"Driver not loaded"的问题
- 解决:浏览页面时,出现"WebDev.WebServer.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。"问题
- 在Eclipse中运行C++程序出现"Launch failed. Binary not foud"和"Program file not Specified"的问题
- 在Eclipse中运行C++程序出现"Launch failed. Binary not foud"和"Program file not Specified"的问题
- 测试的时候浏览出现 "WebDev.WebServer.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。
- Flex使用Weborb与.NET交互的问题:使用DataSet出现"提供的索引超出范围"异常
- 进行网络出现问题Error Domain=kCFErrorDomainCFNetwork Code=-1022 "The resource could not be loaded because th
- 用命令行安装xadmin出现django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
- 运行ASP.NET出现的问题:无法直接启动带有"类型输出类型"的项目
- Ubuntu 下运行 Shell 脚本时显示 "declare not found" 的问题
- nginx升级到 0.8.53 之后,使用老的配置出现duplicate MIME type "text/html"问题的解决办法
- 解决:浏览页面时,出现"WebDev.WebServer.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。"问题
- 打开Rational Rose出现"java.lang.ClassNotFoundException"的问题
- Could not load file or assembly "App_Licenses.dll" " Could not load file or assembly App_Web_"