dkr*_*roy 5 python pip setuptools setup.py
是否有一种标准方法允许python包的要求在包安装脚本中失败?
我正在创建一个类似于Python Social Auth的软件包,因为它有许多提供程序.
有没有一种标准的方法可以让某些提供程序的安装要求失败,但仍然可以将软件包安装得很好吗?
用户在安装时指定的可选依赖项:
您可以使用该extras_require参数setup():http://pythonhosted.org/setuptools/setuptools.html#declaring-extras-optional-features-with-their-own-dependency
setup(
name="MyProject",
...
extras_require = {
'ProviderX': ["DependencyX1", "DependencyX2"],
'ProviderY': ["DependencyY"],
}
)
Run Code Online (Sandbox Code Playgroud)
通过这种方法,用户可以要求安装特定的扩展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):
...
Run Code Online (Sandbox Code Playgroud)
然后它会迭代 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99 次 |
| 最近记录: |