【问题标题】:How to debug ImportError (with sys.path being correct)如何调试 ImportError(sys.path 正确)
【发布时间】:2014-09-03 09:38:59
【问题描述】:

我通过 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 发生在 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


    【解决方案1】:

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

    鉴于:

    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__ 上导入中间件模块。

    基本上在__init__上添加from . import middleware

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 2022-08-18
      • 1970-01-01
      • 2020-04-28
      • 2020-04-08
      • 2020-01-08
      相关资源
      最近更新 更多