[英]Python calling subprocess that requires no virtual environment
我有一个 Python 3.6 脚本,它使用 subprocess 调用第三方工具。
main_script.py:
#!/usr/bin/env python
import subprocess
result = subprocess.run(['third-party-tool', '-arg1'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
问题是, main_script.py
必须在虚拟环境中运行,而third-party-tool
必须在任何虚拟环境中运行。
我对third-party-tool
不太了解,除了它在我的道路上。 在虚拟环境处于活动状态时调用它会导致它卡住并稍后抛出异常。 我不知道它是使用默认的 python 二进制文件,还是它启动自己的虚拟环境并在其中执行操作。 它不是 Python 脚本,但显然以某种方式调用了一个脚本。
如何告诉子进程退出我的虚拟环境并在默认 shell 环境中运行命令?
我检查了几个类似的问题:
third-party-tool
不是 Python 脚本(我相信它是 bash)。从子流程的文档中:
https://docs.python.org/3/library/subprocess.html
接受的参数是
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None,
capture_output=False, shell=False, cwd=None, timeout=None, check=False,
encoding=None, errors=None, text=None, env=None, universal_newlines=None)
特别是,
如果 env 不是 None,则它必须是定义新进程的环境变量的映射; 这些用于代替继承当前进程环境的默认行为。 它直接传递给 Popen。
因此,传递一个空字典env={}
(从空环境开始)并使用bash --login
(作为登录 shell 运行,读取 env 默认值)应该可以解决问题。
subprocess.run(['bash', '--login', '-c', '/full/path/to/third-party-tool', '-arg1'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env={})
感谢您的帮助,nullUser; 您的解决方案是对我的问题的简洁而正确的答案。
但是,当我尝试它时,我的第三方工具现在由于其他(未知)原因而失败。 可能还有一些我不知道的其他环境变量会随着新 shell 丢失。 幸运的是,我找到了一个替代解决方案,我将与其他任何挣扎的人分享。
据我所知,进入虚拟环境对我的环境的唯一区别是向我的 PATH 变量添加一个新路径,并添加变量 VIRTUAL_ENV。
我可以通过创建我的环境的副本来复制外部虚拟环境行为:
my_script.py
实现我的解决方案:
#!/usr/bin/env python
import subprocess, os, sys
env = os.environ.copy()
if hasattr(sys,'real_prefix'):
# If in virtual environment, gotta forge a copy of the environment, where we:
# Delete the VIRTUAL_ENV variable.
del(env['VIRTUAL_ENV'])
# Delete the "/home/me/.python_venv/main/bin:" from the front of my PATH variable.
orig_path = env['PATH']
virtual_env_prefix = sys.prefix + '/bin:'
env['PATH'] = orig_path.replace(virtual_env_prefix, '')
# Pass the environment into the third party tool, modified if and when required.
subprocess.run(['./third-party-tool'], shell=False, env=env)
third-party-tool
被third-party-tool
为一个脚本,告诉您它是否在虚拟环境中并打印出环境变量。 在这个例子中, third-party-tool
是一个 Python 脚本,但通常它可能不是。
#!/usr/bin/env python
# third-party-tool
import sys, os
in_venv = hasattr(sys, 'real_prefix')
print('This is third-party Tool and you {} in a virtual environment.'.format("ARE" if in_venv else "ARE NOT"))
os.system('env')
现在我尝试从外部虚拟环境、内部虚拟环境和虚拟环境中的 python 脚本调用第三方工具,捕获输出。
[me@host ~]$ ./third-party-tool > without_venv.txt
# Now I activate virtual environment
(main) [me@host ~]$ ./third-party-tool > within_venv.txt
(main) [me@host ~]$ ./my_script.py > within_venv_from_python.txt
注意:输出如下所示:这是第三方工具,您不在虚拟环境中。 (继续打印出 KEY=VALUE 环境变量的列表)
我使用我最喜欢的 diff 工具并比较输出。 within_venv_from_python.txt
是相同的without_venv.txt
,这是一个好现象(在这两种情况下, third-party-tool
用同样的环境变量运行,并表明它不是生活在矩阵)。 实施此解决方案后,我的实际第三方工具似乎可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.