【问题标题】:Can't get rid of a ghost module无法摆脱幽灵模块
【发布时间】:2016-05-15 02:53:43
【问题描述】:

所以这是一个奇怪的... 我承认,我安装了我自己的模块,混合了各种

  • python setup.py 安装
  • python setup.py 开发
  • 点安装。
  • 点安装 -e .

但在使用“pip 卸载”并手动删除任何剩余的鸡蛋或鸡蛋链接,甚至 *.pth 文件中的条目后,我无法确定导入模块 (planet4) 的剩余能力的来源。

我尝试了明显的:planet4.__file__ 但那是空的:

In [12]: planet4.__file__
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-09c196cdfffe> in <module>()
----> 1 planet4.__file__

AttributeError: module 'planet4' has no attribute '__file__'

Python 仍然认为这是一个模块:

In [16]: type(planet4)
Out[16]: module

当我尝试使用 pkg_resources 获取一些隐藏文件路径时,我得到了最奇怪的错误,即使是 Google 也只见过几次,至少与 删除 一个包有关:

In [10]: import pkg_resources as pr
In [11]: pr.resource_exists('planet4', 'data')
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-11-3ac559fe861b> in <module>()
----> 1 pr.resource_exists('planet4', 'data')

/Users/klay6683/miniconda3/envs/py35/lib/python3.5/site-packages/setuptools-19.6.2-py3.5.egg/pkg_resources/__init__.py in resource_exists(self, package_or_requirement, resource_name)
   1137     def resource_exists(self, package_or_requirement, resource_name):
   1138         """Does the named resource exist?"""
-> 1139         return get_provider(package_or_requirement).has_resource(resource_name)
   1140
   1141     def resource_isdir(self, package_or_requirement, resource_name):

/Users/klay6683/miniconda3/envs/py35/lib/python3.5/site-packages/setuptools-19.6.2-py3.5.egg/pkg_resources/__init__.py in has_resource(self, resource_name)
   1603
   1604     def has_resource(self, resource_name):
-> 1605         return self._has(self._fn(self.module_path, resource_name))
   1606
   1607     def has_metadata(self, name):

/Users/klay6683/miniconda3/envs/py35/lib/python3.5/site-packages/setuptools-19.6.2-py3.5.egg/pkg_resources/__init__.py in _has(self, path)
   1658     def _has(self, path):
   1659         raise NotImplementedError(
-> 1660             "Can't perform this operation for unregistered loader type"
   1661         )
   1662

NotImplementedError: Can't perform this operation for unregistered loader type

有人知道我可以尝试什么吗?

【问题讨论】:

    标签: python pip setuptools pkg-resources


    【解决方案1】:

    groan,很明显,PYTHONPATH(我很久没有再使用它)甚至在模块命名空间中为没有__init__.py 文件的文件夹创建条目。 所以,不知道这一点,加上我不记得我仍然定义了 PYTHONPATH 环境变量这一事实,造成了难题。

    PYTHONPATH 真的是万恶之源... :(

    更新 因此,虽然此条目是在我的特定情况下出现问题的答案,但公认的答案是我如何找到它以及在类似情况下如何找到它:我查看了 sys.path 是否有任何可疑之处。

    【讨论】:

    • 最好的解决方案是使用 virtualenv 并让 setup.py 自动为您处理 PYTHONPATH 变量。这样您就不必手动处理它。如果出现问题,您还可以核对 virtualenv 并快速重新创建
    • 我正在使用 virtualenvs,但仍然在已加载的旧 .zshrc 文件中设置了 PYTHONPATH。为什么投反对票?我正确地指出 PYTHONPATH 设置不好。
    • 我没有投反对票。别人做了。 PYTHONPATH 很糟糕,我完全同意。
    【解决方案2】:

    打开 python 提示符

    import sys
    
    for p in sys.path:
        print p
    

    这将为您提供目录列表。在所有这些目录中查找名称为planet4 的目录或文件。还要查找任何.pth 文件。如果您发现任何内容,请在文本编辑器中打开它们,它们将为您提供更多目录供您查看、冲洗和重复。

    【讨论】:

    • 里面有很多东西,所以我先避开了它。但后来我在那里看到了一个意想不到的文件夹,并想到了我过去使用 PYTHONPATH 的经历并进一步调查。正如我在帖子中所写,我确实检查了 .pth 文件。
    • 我还是给你答案,最后是b/c,如果指向模块的指针不再告诉你,这是真正深入了解它的唯一方法关于它自己。
    猜你喜欢
    • 1970-01-01
    • 2017-07-25
    • 2021-08-21
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2015-04-06
    • 2013-06-20
    • 2015-10-03
    相关资源
    最近更新 更多