我如何在不使用子流程的情况下从python自动脚本中运行python'sdist'命令? [英] How do i run the python 'sdist' command from within a python automated script without using subprocess?

查看:106
本文介绍了我如何在不使用子流程的情况下从python自动脚本中运行python'sdist'命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来自动包装自制" python模块并将其分发到远程计算机上.

I am writing a script to automate the packaging of a 'home-made' python module and distributing it on a remote machine.

我正在使用Pip并创建了setup.py文件,但随后我必须调用子进程模块以调用"python setup.py sdist"命令.

i am using Pip and have created a setup.py file but i then have to call the subprocess module to call the "python setup.py sdist" command.

我已经看过distutils.core中的"run_setup"方法,但是我试图避免一起使用子流程模块. (如果我已经在python中,则打开外壳运行python命令毫无意义...)

i have looked at the "run_setup" method in distutils.core but i am trying to avoid using the subprocess module alltogether. (i see no point in opening a shell to run a python command if i am already in python...)

有没有一种方法可以将distutils模块导入到我的脚本中,并将设置信息直接传递到其方法之一,而又避免完全使用shell命令?或其他可能对我有帮助的建议

is there a way to import the distutils module into my script and pass the setup information directly to one of its methods and avoid using the shell command entirely? or any other suggestions that may help me

谢谢

推荐答案

出于完整性考虑,我想回答这个问题,因为我偶然发现了它,试图自己找出解决方法.就我而言,我想确保使用 same python版本执行命令,这就是为什么使用子进程不是一个好选择的原因. (正如评论中指出的那样,我可以将sys.executable与子进程一起使用,尽管编程执行是IMO仍然是一种更简洁的方法-显然非常简单.)

Just for the sake of completeness, I wanted to answer this since I came across it trying to find out how to do this myself. In my case, I wanted to be sure that the same python version was being used to execute the command, which is why using subprocess was not a good option. ( as pointed out in comment, I could use sys.executable with subprocess, though programmatic execution is IMO still a cleaner approah -- and obviously pretty straightforward.)

(使用 distutils.core.run_setup 可以不是调用子进程,而是在受控范围/环境中使用exec.)

(Using distutils.core.run_setup does not call subprocess, but uses exec in a controlled scope/environment.)

from distutils.core import run_setup

run_setup('setup.py', script_args=['sdist'])

另一个选择可能是使用setuptools命令,尽管我尚未对此进行探讨.显然,您仍然必须弄清楚如何避免重复您的proj元数据.

Another option, may be to use the setuptools commands, though I have not explored this to completion. Obviously, you still have to figure out how to avoid duplicating your proj metadata.

from setuptools.dist import Distribution
from setuptools.command.sdist import sdist

dist = Distribution({'name': 'my-project', 'version': '1.0.0'}) # etc.
dist.script_name = 'setup.py'
cmd = sdist(dist)
cmd.ensure_finalized()
cmd.run()  # TODO: error handling

无论如何,希望能对正确的方向有所帮助.毕竟,有很多正当的理由要以编程的方式执行打包操作.

Anyway, hopefully that will help someone in the right direction. There are plenty of valid reasons to want to perform packaging operations programmatically, after all.

这篇关于我如何在不使用子流程的情况下从python自动脚本中运行python'sdist'命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆