强制 `setup.py` 使用 setuptools
我正在使用以下代码:
code = 'import setuptools;__file__={0!r};execfile(__file__)'.format(os.path.join(path, 'setup.py'))
args = ['install', '--single-version-externally-managed']
subprocess.check_call([sys.executable, '-c', code, args])
执行 setup.py
并安装该包。当 setup.py 使用 distutils 而不是 setuptools 时会出现问题:distutils 无法识别 --single-version-externally-management。
如何强制 setup.py 使用 setuptools?
I'm using this code:
code = 'import setuptools;__file__={0!r};execfile(__file__)'.format(os.path.join(path, 'setup.py'))
args = ['install', '--single-version-externally-managed']
subprocess.check_call([sys.executable, '-c', code, args])
To execute a setup.py
and install the package. The problem occurs when setup.py
uses distutils instead of setuptools: --single-version-externally-managed is not recognized by distutils.
How can I force setup.py
to use setuptools?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你所写的基本上就是 pip 所做的。根据您编写的代码,您将使用 setuptools 的
setup
函数,因为您是从 setuptools 导入的。 Setuptools 覆盖了 Distutils 的设置
函数位于其__init__.py
中。 因此,setup.py 脚本是否导入并不重要是否有 distutils。 Setuptools 总是会获胜如果由于某种原因您在运行命令时仍然遇到问题, 。尝试在执行之前编译该文件。
exec(compile(...))
而不是execfile(...)
作为对 @jknair 回答的回应...我也不鼓励使用 ez_setup。 py,因为它是代码重复,具有意外的行为,并且通常在包分发期间被排除(这使得 pip 等工具很难在没有 ImportError 的情况下运行 setup.py)。
What you have written is basically what pip does. Based on the code you wrote, you will be using setuptools'
setup
function because you've imported from setuptools. Setuptools paves over Distutils'setup
function in its__init__.py
. Therefore, it doesn't mater if the setup.py script imports distutils or not. Setuptools will always win...If for some reason you still have issues while running your command. Try compiling the file before execution.
exec(compile(...))
rather thanexecfile(...)
In response to @jknair answer... I'd also discourage the use of ez_setup.py, because it's code duplication, has unexpected behavior and is often excluded during package distribution (which makes it hard for tools like pip to run the setup.py without an ImportError).