How to run "python setup.py install" from Python?

I am trying to create a generic python script to run a python application, and I would like to install any dependent python modules if they are not on the target system. How can I run the equivalent of the "python setup.py install" command line command from Python itself? I feel it should be pretty easy, but I can't figure it out.

+6
python
source share
7 answers
+8
source share

You can use the subprocess module:

import subprocess subprocess.call(['python', 'setup.py', 'install']) 
+5
source share
 import os string = "python setup.py install" os.system(string) 
+3
source share

For those using setuptools, you can use setuptools.sandbox :

 from setuptools import sandbox sandbox.run_setup('setup.py', ['clean', 'bdist_wheel']) 
+2
source share

Just import it.

 import setup 
0
source share

This works for me (py2.7)
I have an additional module with its setup.py in a subfolder of the main project.

from distutils.core import run_setup [..setup(..) config of the main project..] run_setup('subfolder/setup.py', script_args=['develop',],stop_after='run')

thanks

Update:
Digging time you can find in distutils.core.run_setup

 'script_name' is a file that will be run with 'execfile()'; 'sys.argv[0]' will be replaced with 'script' for the duration of the call. 'script_args' is a list of strings; if supplied, 'sys.argv[1:]' will be replaced by 'script_args' for the duration of the call. 

therefore the above code will be changed to

 import sys from distutils.core import run_setup run_setup('subfolder/setup.py', script_args=sys.argv[1:],stop_after='run') 
0
source share

Late, but if someone finds him here like me, it worked for me; (python 3.4). My script was one package with setup.py. Note that you should have chmod + x on setup.py, I suppose.

 cwd = os.getcwd() parent = os.path.dirname(cwd) os.chdir(parent) os.system("python setup.py sdist") 
0
source share

All Articles