我通过 CherryPy 提供 django 页面。当 CherryPy 在前台启动时,一切正常。当我用
守护 CherryPy 时Daemonizer(cherrypy.engine).subscribe()
我收到导入错误。
sys.path
在两种情况下(守护进程和非守护进程)完全相同。我该如何调试这个,除了 sys.path
之外还有什么会影响 python 导入?
其他信息
回溯:
[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_boot/middleware.py
- 相关的 sys.path 条目(发生 ImportError 时也会出现):
'/home/fbx/code/plinth'
- ImportError 发生在 https://github.com/django/django/blob/master/django/utils/importlib.py 中的 djangos
import_module
函数中。 import_module
的参数name
为“plinth.modules.first_boot.middleware”
- django MIDDLEWARE_CLASSES 设置为
'plinth.modules.first_boot.middleware.FirstBootMiddleware'
还有一点:
我在目录 /home/fbx/code/plinth
中使用 python -m plinth
运行守护进程服务器。
当我使用 /usr/bin/python/home/fbx/code/plinth/plinth/__main__.py
启动守护进程服务器时,一切正常!在本例中,sys.path
多了一个条目:/home/fbx/code/plinth/plinth
。但在启动时手动添加此路径并不能修复作为 python -m plinth
运行时的 ImportError。
我正在运行此代码:https://github.com/freedombox/Plinth/tree/0b5af376102f4210395c15b2366b96a6e56fefb2
更新
感谢@cyraxjoe,os.chdir()
与__init__.py
中缺少的模块相结合就是问题所在。对我来说,这种行为是出乎意料的,我没有找到很多有用的信息/文档,所以我设置了一个 github 存储库来更轻松地演示该问题:https://github.com/fonfon/ImportError-demo
最佳答案
这只是一个理论,但这可能是原因。
鉴于:
- Deamonizer 插件 changes the directory to the root
os.chdir('/')
. - 包
plinth.modules.first_boot
显式导入first_boot
,而不是包的__init__.py
上的中间件。<
可能是在 Daemonizer 插件更改目录之前,导入了模块 plinth.modules.first_boot
但没有中间件,因此当 django 尝试动态导入该模块时,它只是找到导入缓存上的模块,但它找不到中间件,因为路径是相对的,当 Daemonizer 插件更改目录时,它就变得无法访问。
尝试在包的 __init__
中导入中间件模块。
基本上添加一个 from 。在
__init__
上导入中间件
关于python - 如何调试 ImportError(sys.path 正确),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25641003/