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

后端 未结 6 1873
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 17:15

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.

相关标签:
6条回答
  • 2021-02-05 17:55

    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')
    
    0 讨论(0)
  • 2021-02-05 17:59

    Just import it.

    import setup
    
    0 讨论(0)
  • 2021-02-05 18:09

    Way late - but if someone finds him/herself here like I did - this worked for me; (python 3.4). My script was one package down from setup.py. Note, you have to have chmod +x on setup.py, I believe.

    cwd = os.getcwd()
    parent = os.path.dirname(cwd)
    os.chdir(parent)
    os.system("python setup.py sdist")
    
    0 讨论(0)
  • 2021-02-05 18:14
    import os
    string = "python setup.py install"
    os.system(string)
    
    0 讨论(0)
  • 2021-02-05 18:16

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

    from setuptools import sandbox
    sandbox.run_setup('setup.py', ['clean', 'bdist_wheel'])
    
    0 讨论(0)
  • 2021-02-05 18:16

    You can use the subprocess module:

    import subprocess
    subprocess.call(['python', 'setup.py', 'install'])
    
    0 讨论(0)
提交回复
热议问题