简体   繁体   English

不同 python 版本的子流程执行

[英]subprocess execution with different python version

I have a script which I execute within a python3 virtual environment and inside this script there is a subrocess.Popen command which starts an application that works only with python2.我有一个脚本,我在 python3 虚拟环境中执行,在这个脚本中有一个 subprocess.Popen 命令,它启动一个仅适用于 python2 的应用程序。 Is there a way to change the environment only for this process such that this subprocess finds python2 only?有没有办法只为这个进程更改环境,以便这个子进程只找到 python2?

So far I have tried the following which haven't work out.到目前为止,我已经尝试了以下没有成功的方法。

os.environ["PATH"] = "/path/to/python2/bin:"+os.environ["PATH"] 

Easiest solution I found:我发现的最简单的解决方案:

venv = os.environ.copy()
venv.update({"PATH": os.environ["PATH"].replace(venv['VIRTUAL_ENV'], 
                     'other/python/version/path')}) # might have to tweak this a bit only


proc2 = subprocess.Popen(["cmd",
                         env=venv,
                         stdout=sys.stdout,
                         stderr=sys.stderr,
                             )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM