我搜索过,但似乎找不到已经问过这个问题的任何人,那么就由我来问一下吧。
我开始将IPython作为我的Windows 7默认 shell,我想将感叹符魔法(!cmd
)配置为使用PowerShell而不是cmd.exe来传递命令给系统shell,这样我可以利用微软所做的增强功能,但又不必更改我的 %COMSPEC%
到 PowerShell(因为我的公司仍然使用几个基于CMD的.BAT文件进行自动化,我不想破坏这些文件)。
我将我的问题放在这里,因为答案可能不需要下面的任何信息:
- 我有遗漏吗?IPython中是否有使用配置文件指定系统命令解释器的方法?
- 如果!1,则是否有我忽略的方式可以从PowerShell启动一个子进程,并具有对该进程局部的机器范围环境变量?
调查/测试
浏览IPython代码,我发现它使用os.system()
将命令发送到shell,而不是subprocess.call()
。这使事情变得更加复杂,因为os.system*只使用COMSPEC,而使用subprocess*可以指定要使用的可执行文件。
我尝试加载PowerShell,设置$env:comspec
变量,然后从该shell中启动IPython,但即使在IPython中,COMSPEC看起来已设置,CMD仍然被使用:
[PS] C:\> $env:comspec
C:\Windows\system32\cmd.exe
[PS] C:\> $env:comspec = 'powershell.exe'
[PS] C:\> $env:comspec
powershell.exe
[PS] C:\> & 'C:\Python33\Scripts\ipython3.exe'
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import os; os.getenv('comspec')
Out[1]: 'powershell.exe'
In [2]: !gci
'gci' is not recognized as an internal or external command,
operable program or batch file.
In [3]: os.system('gci')
'gci' is not recognized as an internal or external command,
operable program or batch file.
Out[3]: 1
看起来像是本地修改的COMSPEC被传递给了IPython(作为进行本地更改的PowerShell进程的子进程),但os.system似乎仍在使用持久设置。
我尝试过类似的方法,使用[Environment] :: SetEnvironmentVariable("ComSpec",'powershell.exe',[System.EnvironmentVariableTarget]::User)
,以便只更改用户范围的环境变量,但这也不起作用(与上面相同的结果 - os.getenv('ComSpec')
显示powershell,但!-ed命令被发送到CMD)。
更改机器范围的环境变量似乎符合我的要求,但出于之前提到的原因,这不是一个有效的解决方案。
[PS] C:\> $env:comspec
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
[PS] C:\> [Environment]::GetEnvironmentVariable('ComSpec','User')
[PS] C:\> [Environment]::GetEnvironmentVariable('ComSpec','Machine')
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
[PS] C:\> & 'C:\Python33\Scripts\ipython3.exe'
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: !gci env:ComSpec
Name Value
---- -----
ComSpec C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
由于这不是一个可行的持续解决方案,因此我通过调整ConEmu *中的设置进行了更多测试。在“设置>启动>ComSpec”中设置显式可执行文件和为子进程设置ComSpec环境变量为所选值*标志,并提供powershell.exe的路径,我能够做到这一点而不改变全局变量,但我不确定这是否会对使用ConEmu打开的CMD控制台产生负面影响,因为该设置是全局的(在ConEmu内)。这就是导致我问问题2的原因,因为我不知道如何在PowerShell中设置进程/子进程本地、机器范围的环境变量(如果这样的事情真的存在)。
最终,我的梦想目标是IPython通过配置文件支持指定命令行解释器,但要实现这一点,不能使用os.system()
。我计划在本地副本中使用subprocess.call()
替换它(参考Python文档),以进行测试,但如果有人已经玩过这个,或者如果当前模型与使用subprocess
相比具有足够的优势,并且我不知道,请告诉我。看起来这已经在非Windows系统上完成(GitHub),但我在这个规模上的Python是新手,不能确定如果在条件语句的Windows部分使用该更改是否会导致其他问题。
脚注
糟了!似乎必须拥有10+声望才能适当记录问题。这里是我无法在上面发布的链接:
- os.system - docs.python.org/3.3/library/os.html#os.system
- subprocess - docs.python.org/3.3/library/subprocess.html
- ConEmu - code.google.com/p/conemu-maximus5/
subprocess
做出贡献,我们一定会考虑。我认为它还没有被更改的主要原因是我们没有太多的Windows用户来测试这个更改。 - Thomas Kos.system()
是一个经过计算的选择,并且一直存在。 - jmwelch