【发布时间】:2014-09-03 09:38:59
【问题描述】:
我通过 CherryPy 提供 django 页面。当 CherryPy 在前台启动时,一切正常。当我用
守护 CherryPyDaemonizer(cherrypy.engine).subscribe()
我得到一个 ImportError。
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 发生在 djangos
import_module函数中 https://github.com/django/django/blob/master/django/utils/importlib.py。 -
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 repo 来更容易地演示这个问题:https://github.com/fonfon/ImportError-demo
【问题讨论】:
标签: python django cherrypy importerror