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?
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 than 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).