为什么 this simple program 在运行时会导致 pkg_resources.DistributionNotFound 错误,我们该如何解决?

#setup.py
from setuptools import setup

setup(name='my_project',
    version='0.1.0',
    packages=['my_project'],
    entry_points={
        'console_scripts': [
            'my_project = my_project.__main__:main'
        ]
     },
)

.
##my_project/__main__.py

import sys

def main(args=None):
     print("Do Something")

if __name__ == "__main__":
    main()

构建: python setup.py install --root=target --prefix=usr 运行: .\target\usr\Scripts\my_project.exe
结果
Traceback (most recent call last):
  File "D:\code-maphew\scraps\bug-dist-not-found\target\usr\Scripts\my_project-script.py", line 6, in <module>
    from pkg_resources import load_entry_point
  File "C:\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pkg_resources\__init__.py", line 3105, in <module>
    @_call_aside
  File "C:\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pkg_resources\__init__.py", line 3089, in _call_aside
    f(*args, **kwargs)
  File "C:\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pkg_resources\__init__.py", line 3118, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "C:\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pkg_resources\__init__.py", line 578, in _build_master
    ws.require(__requires__)
  File "C:\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pkg_resources\__init__.py", line 895, in require
    needed = self.resolve(parse_requirements(requirements))
  File "C:\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pkg_resources\__init__.py", line 781, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'my-project==0.1.0' distribution was not found and is required by the application

这个 Q 类似于 pkg_resources.DistributionNotFound when using a module installed from a bdist_rpm ,但不是构建 RPM。在那个 Q 中使用 --prefix 解决了这个问题。那对我没有用。我已经在 Windows 10、Linux Mint 和 Debian 上复制了同样的问题。

此处存储库中的完整代码:https://github.com/maphew/scraps/tree/master/bug-dist-not-found

最佳答案

我刚刚遇到了同样的问题。问题是代码可以在一台计算机上运行,​​但不能在另一台计算机上运行。我也在使用我制造的轮子。无论如何,我找到的解决方案是升级 pip 和我的包。例如

pip install --upgrade pip path/to/my_package

重新安装了 pip 和我的包,然后入口点就可以工作了。

10-07 23:55