python - 如何获得完整的解释器启动横幅
问题描述
我正在尝试在 Python 中获取启动横幅信息,而不仅仅是:
f'''Python {sys.version} on {sys.platform}
Type "help", "copyright", "credits", or "license" for more information.'''
这导致:
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.
但完整的启动横幅包括任何错误和分布,例如:
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.
python
我正在考虑运行subprocess.Popen
然后终止它,但我无法捕获启动横幅输出。
解决方案
我终于能够弄清楚这个问题。原来subprocess.Popen
是正确的答案。有趣的是,标题被打印到stderr
而不是stdout
. 对我有用的代码:
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 '>>>'
推荐阅读
- java - 对图像应用过滤器 CameraPreview 图像馈送
- regex - 用正则表达式减去 JSON Web Token (JWT)
- r - 如何使用 renv 更新项目中使用的 R 版本
- python - python用pyodbc打开一个access数据库
- python - Python如何将capnproto转换为json?
- shopware - 使用 FTP 更改某些商店软件主题文件不会影响网站
- javascript - 无法理解错误“td-dialog”不是已知元素
- sql - SQL 用前一个非 NULL 行填充 NULL 行
- javascript - 用于在 Log Work 中编写工作描述的 Jira Tampermonkey 脚本
- c# - AutoMapper:更新现有集合