跨多个 setup.py 脚本共享代码的最佳方式? [英] Best way to share code across several setup.py scripts?
问题描述
我有几个正在处理的软件包,我想在它们的 setup.py 脚本之间共享代码.有什么好的方法可以做到这一点,或者代码重复是我唯一的选择吗?
I've got several packages I'm working on, and I'd like to share code between their setup.py scripts. Is there any good way to do this or is code duplication my only option?
推荐答案
通常 setup.py
是分发不同包的入口点.因此,很难在这些包之间共享代码.
Normally setup.py
is the entry point for distribution of distinct packages. As such, it's hard to then share code between those packages.
如果您使用 setuptools
(或者它是 fork,distribute
) 在你的 setup.py
中,你可以指定使用 setup_requires
条目安装包时必须安装的包.
If you use setuptools
(or it's fork, distribute
) in your setup.py
, you can specify packages that must be installed when installing your package with the setup_requires
entry.
不幸的是,你的 setup.py
被先执行了;一旦 setup_requires
行被解析,那里列出的额外包将被安装在本地,但这对于您的需求来说可能为时已晚.
Unfortunately, your setup.py
is executed first; as soon as the setup_requires
line is parsed, the extra packages listed there will be installed locally, but this may be too late for your needs.
解决方法是在调用定义 setup_requires
条目的 setup
之前创建一个单独的 Distribution
对象:
The work-around is to create a separate Distribution
object before you call setup
which defines the setup_requires
entries:
import setuptools
setuptools.dist.Distribution(dict(setup_requires='yoursharedsetuppackage'))
# `setup_requires` is parsed and acted upon immediately; from here on out
# the yoursharedsetuppackage is installed and importable.
import yoursharedsetuppackage
setup(...)
这篇关于跨多个 setup.py 脚本共享代码的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!