在Python中传递参数至setup.py的install_requires列表

5 投票
2 回答
3696 浏览
提问于 2025-04-18 16:14

我用pip安装了PIL这个库。在安装的时候,它需要两个额外的参数。所以安装的命令大概是这样的。

pip install PIL --allow-external PIL --allow-unverified PIL

我需要在setup.py文件中添加PIL这个包。把PIL加到install_requires列表里确实能安装PIL,但这样安装不成功,因为我需要用额外的参数来安装PIL。

那么,我该怎么把PIL和额外的参数一起加到install_requires列表里呢?

2 个回答

-1

只需把你安装的依赖中的PIL换成Pillow就可以了。Pillow是PIL的一个改进版,修复了很多问题,支持Python 3,并且有更好的托管服务。你不需要修改你的代码。

1

目前,在setup.py文件中的install_requires里,不能指定额外的参数。不过,我通过创建一个setuptools.command.install类的子类,并重写它的run()方法,解决了我安装依赖的问题,下面是我的代码示例 -

from setuptools import setup
from setuptools.command.install import install
from subprocess import call


class CustomInstall(install):
    def run(self):
        install.run(self)
        call(['pip', 'install', 'PIL', '--allow-external', 'PIL', '--allow-unverified', 'PIL'])

setup( ...
      cmdclass={
          'install': CustomInstall,
      },
)

撰写回答