Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run 'python setup.py install' from within Python?

Tags:

python

I'm trying to create a generic python script for starting up a python app and I would like to install any dependent python modules if they are missing from the target system. How can I run the equivalent of the command line command 'python setup.py install' from within Python itself? I feel like this should be pretty easy, but I can't figure it out.

like image 421
jamesaharvey Avatar asked Dec 12 '10 00:12

jamesaharvey


2 Answers

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

from setuptools import sandbox
sandbox.run_setup('setup.py', ['clean', 'bdist_wheel'])
like image 168
Bartek Jablonski Avatar answered Nov 15 '22 20:11

Bartek Jablonski


This works for me (py2.7)
I have a optional 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 a while 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.

so the above code shold be changed to

import sys
from distutils.core import run_setup
run_setup('subfolder/setup.py', script_args=sys.argv[1:],stop_after='run')
like image 24
LittleEaster Avatar answered Nov 15 '22 20:11

LittleEaster