Skip to main content
 首页 » 编程设计

python之如何调试 ImportError(sys.path 正确)

2024年10月25日10webabcd

我通过 CherryPy 提供 django 页面。当 CherryPy 在前台启动时,一切正常。当我用

守护 CherryPy 时
Daemonizer(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 发生在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

请您参考如下方法:

这只是一个理论,但它可能是原因。

鉴于:

  1. Deamonizer 插件 changes the directory to the root os.chdir('/') .
  2. 包裹plinth.modules.first_boot正在显式导入 first_boot而不是 __init__.py 上的中间件包的。

可能是在 Daemonizer 插件更改目录之前,模块 plinth.modules.first_boot被导入但没有中间件,所以当 django y 尝试动态导入模块时,它只是在导入缓存中找到模块,但它找不到中间件,因为路径是相对的,当 Daemonizer 插件更改目录时,它变成无法访问。

尝试在 __init__ 上导入中间件模块包的。

基本上加一个from . import middleware在 __init__ 上