Skip to main content
 首页 » 编程设计

python之PyPI 包装 : install_requires `pillow`

2025年01月19日29cmt

在上传之前,我在包目录中使用 pip3 install -e . 测试我的 PyPI 包。

依赖于pillow(在代码中导入PIL)

当我使用已安装的枕头进行测试时,它起作用了。

但是,我卸载了 pillow,然后使用 pip3 install -e 重新安装了我的包。,它没有工作:

Obtaining file:///Users/hongbook/dev/identicon 
    Complete output from command python setup.py egg_info: 
    Traceback (most recent call last): 
      File "<string>", line 1, in <module> 
      File "/Users/hongbook/dev/identicon/setup.py", line 4, in <module> 
        import Identicon 
      File "/Users/hongbook/dev/identicon/Identicon/__init__.py", line 2, in <module> 
        from .Identicon import render 
      File "/Users/hongbook/dev/identicon/Identicon/Identicon.py", line 5, in <module> 
        from PIL import Image, ImageDraw 
    ModuleNotFoundError: No module named 'PIL' 

我预计当我安装它时,应该安装 pillow 因为我在 setup.py 中写了 install_requires 的值(还有那个在 requirements.txt 中):

# setup.py 
from setuptools import setup, find_packages 
 
... 
 
setup( 
    name='Identicon', 
    version=Identicon.__version__, 
    ... 
    install_requires=[ 
        'pillow',   
    ], 
) 
 
# requirements.txt 
pillow 

如何使我的项目对 pillow 的依赖性正确?

请您参考如下方法:

我认为你的问题源于此:

version=Identicon.__version__, 

为此,您要导入 Identicon,这是您的包,它导入 PIL。因此,您的 setup.py 已损坏。它需要已经安装依赖项才能执行,但是首先安装这些依赖项是 setup.py 作业。

这是包装中常见的“先有鸡还是先有蛋”的情况。解决方案是使用不同的方式从包中解析版本号,或者将版本号存储在不会触发依赖项导入的地方。