简体   繁体   English

如何获得完整的解释器启动横幅

[英]How to get full interpreter startup banner

I am trying to get the startup banner info in Python, not just:我正在尝试获取 Python 中的启动横幅信息,而不仅仅是:

f'''Python {sys.version} on {sys.platform}
Type "help", "copyright", "credits", or "license" for more information.'''

which results in:这导致:

Python 3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

but the full startup banner including any errors and the distribution for example:但完整的启动横幅包括任何错误和分布,例如:

Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation.

Type "help", "copyright", "credits" or "license" for more information.

I was thinking of running python in subprocess.Popen and then terminate it, but I have not been able to capture the startup banner output.我正在考虑在subprocess.Popen中运行python然后终止它,但我无法捕获启动横幅 output。

I was finally able to figure this question out.我终于能够弄清楚这个问题。 Turns out subprocess.Popen was the right answer.原来subprocess.Popen是正确的答案。 Interestingly the header was printed to stderr instead of stdout .有趣的是 header 被打印到stderr而不是stdout Code that worked for me:对我有用的代码:

from subprocess import (Popen, PIPE)
from os import devnull
from sys import executable
from time import sleep
nump = open(devnull, 'w+') #Making a dud file so the stdin won't be the same as the main interpreter
hed = Popen(executable, shell=True, stdout=PIPE, stderr=PIPE, stdin=nump)
sleep(0.1) #Sleeping so python has time to print the header before we kill it
hed.terminate()
nump.close()
print(hed.stderr.read().decode('utf-8').strip().strip('>>>').strip()) #Removing whitespace and the '>>>'

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

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