【发布时间】:2010-12-12 00:17:34
【问题描述】:
我正在尝试创建一个通用的 python 脚本来启动一个 python 应用程序,如果目标系统中缺少任何依赖的 python 模块,我想安装它们。如何从 Python 本身运行相当于命令行命令“python setup.py install”的命令?我觉得这应该很容易,但我无法弄清楚。
【问题讨论】:
标签: python
我正在尝试创建一个通用的 python 脚本来启动一个 python 应用程序,如果目标系统中缺少任何依赖的 python 模块,我想安装它们。如何从 Python 本身运行相当于命令行命令“python setup.py install”的命令?我觉得这应该很容易,但我无法弄清楚。
【问题讨论】:
标签: python
对于那些使用 setuptools 的人,您可以使用 setuptools.sandbox :
from setuptools import sandbox
sandbox.run_setup('setup.py', ['clean', 'bdist_wheel'])
【讨论】:
Run a distutils setup script, sandboxed in its directory
这对我有用 (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')
【讨论】:
您可以使用subprocess 模块:
import subprocess
subprocess.call(['python', 'setup.py', 'install'])
【讨论】:
import os
string = "python setup.py install"
os.system(string)
【讨论】:
试试这个
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.pyx 与include_dirs 位于同一文件夹中,则运行上述脚本会将hello.cpp 和hello.so (Linux) 文件放在同一文件夹中。享受以编程方式调用 Cython。
那么就
#!/usr/bin/env python
import hello
参考号:https://cython.readthedocs.io/en/latest/src/quickstart/build.html
【讨论】:
只需导入它。
import setup
【讨论】:
晚了 - 但如果有人像我一样发现他/她在这里 - 这对我有用; (蟒蛇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")
【讨论】: