KeyError: 'zope' when starting some Python apps (supervisord, gunicorn, …)

KeyError: 'zope' when starting some Python apps (supervisord, gunicorn, …)

我不确定这个问题是否真的与 supervisord 有关,但这是我尝试启动它时得到的结果。看起来这个问题有点与 Python2 本身有关……

$ supervisord
Traceback (most recent call last):
  File "/usr/bin/supervisord", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2727, in <module>
    add_activation_listener(lambda dist: dist.activate())
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 700, in subscribe
    callback(dist)
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2727, in <lambda>
    add_activation_listener(lambda dist: dist.activate())
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2230, in activate
    map(declare_namespace, self._get_metadata('namespace_packages.txt'))
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1815, in declare_namespace
    path = sys.modules[parent].__path__
KeyError: 'zope'

我的记忆可能出卖了我,但我想记得在我尝试启动另一个程序时也发生过这种情况,但我不记得是哪个 atm。 [1]

我使用 supervisord 来监控一个进程是否崩溃并自动重启,但如果它连续崩溃,它就会放弃,我必须杀死 supervisord 并手动重启它。它曾经有效,但现在无效了。

编辑: [1] 当我尝试启动 gunicorn 时也会发生。

$ /var/www/bin/gunicorn -b 127.0.0.1:5000 index:app
Traceback (most recent call last):
  File "/var/www/bin/gunicorn", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/var/www/lib/python3.3/site-packages/pkg_resources.py", line 3027, in <module>
    add_activation_listener(lambda dist: dist.activate())
  File "/var/www/lib/python3.3/site-packages/pkg_resources.py", line 741, in subscribe
    callback(dist)
  File "/var/www/lib/python3.3/site-packages/pkg_resources.py", line 3027, in <lambda>
    add_activation_listener(lambda dist: dist.activate())
  File "/var/www/lib/python3.3/site-packages/pkg_resources.py", line 2514, in activate
    list(map(declare_namespace, self._get_metadata('namespace_packages.txt')))
  File "/var/www/lib/python3.3/site-packages/pkg_resources.py", line 2082, in declare_namespace
    path = sys.modules[parent].__path__
KeyError: 'zope'

编辑 2: 似乎与此问题有关:https://bugs.launchpad.net/ubuntu/+source/zope.app.pagetemplate/+bug/851038 ……但我不确定该怎么做。 编辑编辑: 嗯,也许不是,它已经 4 岁了。

是的,我也使用 Ubuntu 服务器。

基于 long discussion in chat,问题是您弄乱了您的环境,导致系统上的某些 python 安装变得混乱。

您的 .bashrc 文件包含行 export PYTHONPATH=/usr/local/lib/python3.3/dist-packages/。但是,所有 Python都会使用这个路径,甚至Python 2.

您使用 apt 安装了 supervisor 和 gunicorn,这些版本使用 python2。当您 运行 gunicorn 时,该命令使用 python2,它会选择带有 Python 3 个特定内容的 PYTHONPATH。沿线的某些东西试图检查路径并以某种方式混淆。

删除 .bashrc 文件中修改 PYTHONPATH 的行。