用户在安装时指定的可选依赖项:
setup(
name="MyProject",
...
extras_require = {
'ProviderX': ["DependencyX1", "DependencyX2"],
'ProviderY': ["DependencyY"],
}
)
使用此方法,用户可以要求安装特定的扩展名pip install Myproject [ProviderX].
基于现有包的可选依赖项:
为了自动检测已安装的软件包,您可以动态构建需求列表.例如,你可以看看matplotlib如何做到这一点(他们有许多可选的后端用于绘图等):https://github.com/matplotlib/matplotlib.
基本上,setup.py只是常规的python代码,因此您可以运行一个检查可选依赖项的函数,并调整需求和相应的软件包列表.
matplotlib这样做的方法是为依赖关系定义一个类,它为每个依赖项扩展(在setupExt.py中).
class SetupPackage(object):
optional = False
def check(self):
"""
Checks whether the dependencies are met. [...]
"""
pass
def get_packages(self):
"""
Get a list of package names to add to the configuration.
These are added to the `packages` list passed to
`distutils.setup`.
"""
return []
def get_namespace_packages(self):
"""
Get a list of namespace package names to add to the configuration.
These are added to the `namespace_packages` list passed to
`distutils.setup`.
"""
return []
def get_py_modules(self):
"""
Get a list of top-level modules to add to the configuration.
These are added to the `py_modules` list passed to
`distutils.setup`.
"""
return []
...
class Numpy(SetupPackage):
...
然后迭代setup.py中的每个依赖项,检查是否应该安装它,并相应地扩展每个列表以传递给setup()
mpl_packages = [
'Building Matplotlib',
setupext.Six(),
setupext.Dateutil(),
...
good_packages = []
for package in mpl_packages:
[...]
# check and append
if ...
good_packages.append(package)
[...]
for package in good_packages:
if isinstance(package, str):
continue
packages.extend(package.get_packages())
namespace_packages.extend(package.get_namespace_packages())
py_modules.extend(package.get_py_modules())
ext = package.get_extension()