简体   繁体   English

强制`setup.py`使用setuptools

[英]Force `setup.py` to use 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. 执行setup.py并安装包。 The problem occurs when setup.py uses distutils instead of setuptools: --single-version-externally-managed is not recognized by distutils. setup.py使用distutils而不是setuptools时会出现问题:distutils无法识别--single-version-external-managed。

How can I force setup.py to use setuptools? 如何强制setup.py使用setuptools?

What you have written is basically what pip does. 你所写的基本上就是pip所做的。 Based on the code you wrote, you will be using setuptools' setup function because you've imported from setuptools. 根据您编写的代码,您将使用setuptools的setup功能,因为您已从setuptools导入。 Setuptools paves over Distutils' setup function in its __init__.py . Setuptools在其__init__.py为Distutils的setup功能铺平了道路。 Therefore, it doesn't mater if the setup.py script imports distutils or not. 因此,如果setup.py脚本导入distutils,它就不会生成。 Setuptools will always win... Setuptools将永远赢得......

If for some reason you still have issues while running your command. 如果由于某种原因,在运行命令时仍然存在问题。 Try compiling the file before execution. 尝试在执行前编译文件。 exec(compile(...)) rather than execfile(...) exec(compile(...))而不是execfile(...)

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). 为了回应@jknair的答案...我也不鼓励使用ez_setup.py,因为它的代码重复,具有意外的行为,并且在程序包分发期间经常被排除(这使得像pip这样的工具很难运行设置。 py没有ImportError)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM