【问题标题】:How to run 'python setup.py install' from within Python?如何从 Python 中运行“python setup.py install”?
【发布时间】:2010-12-12 00:17:34
【问题描述】:

我正在尝试创建一个通用的 python 脚本来启动一个 python 应用程序,如果目标系统中缺少任何依赖的 python 模块,我想安装它们。如何从 Python 本身运行相当于命令行命令“python setup.py install”的命令?我觉得这应该很容易,但我无法弄清楚。

【问题讨论】:

标签: python


【解决方案1】:

对于那些使用 setuptools 的人,您可以使用 setuptools.sandbox

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

【讨论】:

  • 请注意,这仅在 setup.py 位于您的当前目录中时才有效。 (这可能是他们将第一个参数称为“script_name”而不是“script_path”的原因。)正如当前编写的那样,它更改为基本目录,但也未能更新 setup.py 的相对路径,导致没有此类文件的异常。
  • 这里究竟是什么沙盒?
  • @CMCDragonkai 写成 here Run a distutils setup script, sandboxed in its directory
【解决方案2】:

这对我有用 (py2.7)
我在主项目的子文件夹中有一个带有 setup.py 的可选模块。

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

谢谢

更新:
挖了一会 你可以在 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.

所以上面的代码应该改为

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

【讨论】:

  • 与投票最多的答案不同,这个答案适用于不在当前目录中的 setup.py 文件。使用 Python 3.6.8 测试。
【解决方案3】:

您可以使用subprocess 模块:

import subprocess
subprocess.call(['python', 'setup.py', 'install'])

【讨论】:

  • 如果 setup.py 在其他路径上,例如 c:\foo\bar\setup.py 怎么办?
  • 传入完整路径作为第二个参数。
【解决方案4】:
import os
string = "python setup.py install"
os.system(string)

【讨论】:

  • 如果 setup.py 在其他路径上,例如 c:\foo\bar\setup.py 怎么办?
【解决方案5】:

试试这个

sudo apt install python-dev  # or python3-dev
pip install --user cython    # or pip3

然后

import os.path
from distutils.core import setup
from distutils.extension import Extension

from Cython.Distutils import build_ext
from Cython.Shadow import *  # This is important!

if __name__ == "__main__":
    setup(
        script_args=["build_ext", "--inplace"],  # Simulate CLI arguments
        cmdclass={'build_ext': build_ext},
        zip_safe=False,
        ext_modules=[
            Extension("hello",
                      ["hello.pyx"],
                      language='c++',
                      include_dirs=[os.path.dirname(__file__)])]  # Same folder
    )

如果hello.pyxinclude_dirs 位于同一文件夹中,则运行上述脚本会将hello.cpphello.so (Linux) 文件放在同一文件夹中。享受以编程方式调用 Cython。

那么就

#!/usr/bin/env python
import hello

参考号:https://cython.readthedocs.io/en/latest/src/quickstart/build.html

【讨论】:

    【解决方案6】:

    只需导入它。

    import setup
    

    【讨论】:

    • 我是这么想的。导入后,如何调用“安装”?
    • 坏主意。它可能具有未安装在您的系统上的依赖项导入。
    【解决方案7】:

    晚了 - 但如果有人像我一样发现他/她在这里 - 这对我有用; (蟒蛇3.4)。我的脚本是 setup.py 的一个包。请注意,我相信您必须在 setup.py 上安装 chmod +x。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2016-01-28
      • 2013-06-04
      • 2016-04-01
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      相关资源
      最近更新 更多