python中检测当前shell是否为powershell [英] Detect whether current shell is powershell in python

查看:37
本文介绍了python中检测当前shell是否为powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查我的python程序在哪个shell中运行的推荐方法是什么?我知道我可以查询 os.environ.get('SHELL') 以获得 shell 名称,但是我可以用什么来确定 powershell?

What's the recommended way to check which shell is my python program running inside? I know I can query os.environ.get('SHELL') for the shell name, but what can I use to determine powershell?

推荐答案

一般来说,环境变量SHELL不会告诉你是哪个shell调用了你的脚本,它只会告诉你当前用户的默认 shell(在类 Unix 平台上)的二进制路径,如 chepner笔记.

Generally, environment variable SHELL does not tell you what shell invoked your script, it only tells you the binary path of the current user's default shell (on Unix-like platforms), as chepner notes.

要检测哪个 shell 调用了您的脚本,您必须检查脚本的父进程.

以下适用于安装的 psutil 包(它是v2 和 v3 兼容):

The following works with the psutil package installed (it is both v2- and v3-compatible):

import os, psutil, re

# Get the parent process name.
pprocName = psutil.Process(os.getppid()).name()

# See if it is Windows PowerShell (powershell.exe) or PowerShell Core (pwsh[.exe]):
isPowerShell = bool(re.fullmatch('pwsh|pwsh.exe|powershell.exe', pprocName))


如果安装包不是一种选择,您可以使用以下解决方法来专门检测 PowerShell,但请注意约束:


If installing a package is not an option, you can use the following workaround to detect PowerShell specifically, but note the constraints:

  • 假定标准PowerShell安装,特别是关于环境变量PSModulePath:即PSModulePath 必须:

  • It presumes a standard PowerShell installation, specifically with respect to environment variable PSModulePath: that is, PSModulePath must either:

  • 在 PowerShell(Unix 类平台)之外根本没有预定义
  • 或者必须只有 一个两个 PowerShell 之外的条目(Windows,通过注册表预定义)[1].
  • not be predefined at all outside of PowerShell (Unix-like platforms)
  • or must have just one or two entries outside of PowerShell (Windows, predefined via the registry)[1].

假定脚本不是通过嵌套 shell 调用来调用的:

It presumes that the script wasn't invoked via nested shell invocations:

  • 如果您从 PowerShell 启动了不同的 shell,然后启动了您的脚本,以下解决方案仍会表明您的脚本是由 PowerShell 启动的.

  • If you launched a different shell from PowerShell, which then launched your script, the solution below would still indicate that your script was launched by PowerShell.

相反,如果您启动了 wsl.exe 或 WSL 的 bash.exe 或 MSYS 的 bash.exe/sh.exe,然后启动您的脚本,下面的解决方案将表明脚本是由 PowerShell(间接)启动的,因为这些可执行文件不继承调用者的环境变量;相比之下,Git 自带的 bash.exe/sh.exe 和 Cygwin 自带的都可以.

Conversely, if you launched wsl.exe or WSL's bash.exe or MSYS' bash.exe / sh.exe from PowerShell, which then launched your script, the solution below would not indicate that the script was (indirectly) launched by PowerShell, because these executables do not inherit the caller's environment variables; by contrast, the bash.exe / sh.exe that comes with Git and the ones that comes with Cygwin do.

import os

isPowerShell = len(os.getenv('PSModulePath', '').split(os.pathsep)) >= 3 

请注意,当 PowerShell 启动时,它确保在所有平台上的 PSModulePath 中至少存在 3 个位置(收件箱/系统模块位置、所有用户位置和当前用户位置).如前所述,在 PowerShell 之外,该变量在 Unix 上根本没有预定义,而在 Windows 上,它最多预定义了 2 个位置.

Note that when PowerShell starts up it ensures that at least 3 locations are present in PSModulePath, on all platforms (the in-box/system-modules location, the all-users location, and the current-user location). As stated, outside of PowerShell the variable isn't predefined at all on Unix, and on Windows it is predefined with at most 2 locations.

[1] 较旧的 Windows 10 版本仅预定义了 system-modules 位置,$env:SystemRoot\System32\WindowsPowerShell\v1.0\Modules,而较新的版本另外预定义了all-users位置,$env:ProgramFiles\WindowsPowerShell\Modules.

[1] Older Windows 10 versions predefined just the system-modules location, $env:SystemRoot\System32\WindowsPowerShell\v1.0\Modules, whereas more recent versions additionally predefine the all-users location, $env:ProgramFiles\WindowsPowerShell\Modules.

这篇关于python中检测当前shell是否为powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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