Python 如何调试ImportError(sys.path正确时)
我通过CherryPy提供django页面。当CherryPy在前台启动时,一切正常。当我把CherryPy和Python 如何调试ImportError(sys.path正确时),python,django,cherrypy,importerror,Python,Django,Cherrypy,Importerror,我通过CherryPy提供django页面。当CherryPy在前台启动时,一切正常。当我把CherryPy和 Daemonizer(cherrypy.engine).subscribe() 我感到恐惧。 sys.path在这两种情况下(后台监控和非后台监控)完全相同。我如何调试这个,除了影响python导入的sys.path之外还有什么 补充资料 回溯: [02/Sep/2014:03:08:46] ENGINE ImproperlyConfigured('Error importing m
Daemonizer(cherrypy.engine).subscribe()
我感到恐惧。
sys.path
在这两种情况下(后台监控和非后台监控)完全相同。我如何调试这个,除了影响python导入的sys.path
之外还有什么
补充资料
回溯:
[02/Sep/2014:03:08:46] ENGINE ImproperlyConfigured('Error importing module plinth.modules.first_boot.middleware: "No module named middleware"',)
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/cherrypy/wsgiserver/wsgiserver2.py", line 1353, in communicate
req.respond()
File "/usr/lib/python2.7/dist-packages/cherrypy/wsgiserver/wsgiserver2.py", line 868, in respond
self.server.gateway(self).respond()
File "/usr/lib/python2.7/dist-packages/cherrypy/wsgiserver/wsgiserver2.py", line 2267, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "/usr/lib/python2.7/dist-packages/cherrypy/_cptree.py", line 299, in call
return app(environ, start_response)
File "/usr/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in call
self.load_middleware()
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py", line 45, in load_middleware
mw_class = import_by_path(middleware_path)
File "/usr/lib/python2.7/dist-packages/django/utils/module_loading.py", line 26, in import_by_path
sys.exc_info()[2])
File "/usr/lib/python2.7/dist-packages/django/utils/module_loading.py", line 21, in import_by_path
module = import_module(module_path)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
import(name)
ImproperlyConfigured: Error importing module plinth.modules.first_boot.middleware: "No module named middleware"
- 要导入的文件:
/home/fbx/code/plinth/plinth/modules/first\u boot/middleware.py
- 相关的sys.path条目(发生导入错误时也会出现):
'/home/fbx/code/plinth'
- 导入错误发生在中的djangos
函数中李>import\u模块
的参数import\u module
是name
“plinth.modules.first\u boot.middleware”
- django MIDDLEWARE\u CLASSES设置为
'plinith.modules.first\u boot.MIDDLEWARE.firstboot MIDDLEWARE'
我使用目录
/home/fbx/code/plinth
中的python-m plinth
运行守护服务器当我用
/usr/bin/python/home/fbx/code/plinth/plinth/\uuuuuuu main\uuuuuuuuuuuupy
启动守护服务器时,一切正常!在这种情况下,sys.path
有一个附加条目:/home/fbx/code/plinth/plinth
。但是在启动时手动添加此路径并不能修复作为python-m plinth运行时的导入错误
我正在运行以下代码:
更新
感谢@cyraxjoe,os.chdir()
加上\uuuu init\uuuu.py
中缺少的模块是问题所在。对我来说,这种行为是出乎意料的,我没有找到很多有用的信息/文档,所以我建立了一个github repo来更容易地演示这个问题:这只是一个理论,但可能是原因
鉴于:
Deamonizer插件os.chdir('/')
包plinth.modules.first\u boot
显式导入first\u boot
,而不是包的\uuuu init\uuuuuuuuuupy
上的中间件
可能是在Daemonizer插件更改目录之前,导入了模块plinth.modules.first\u boot
,但没有中间件,因此,当django y尝试动态导入模块时,它只在导入缓存中找到模块,但找不到中间件,因为路径是相对的,当Daemonizer插件更改目录时,它将变得不可访问
尝试在包的\uuuu init\uuu
上导入中间件模块
基本上从中添加一个。在上导入中间件