为什么在导入pygame时会出现消息,它会打印版本和欢迎消息。消息显示
"pygame 1.9.4 Hello from the pygame community.
https://www.pygame.org/contribute.html"
如何禁用此消息?
我没有看到一种自然的方式来做到这一点(你的是我能找到的唯一的谷歌结果),但我确实通过在导入pygame时暂时禁用stdout来实现同样的目的。
import os, sys
with open(os.devnull, 'w') as f:
# disable stdout
oldstdout = sys.stdout
sys.stdout = f
import pygame
# enable stdout
sys.stdout = oldstdout
这是@Mad Physicist建议的替代方案:
import contextlib
with contextlib.redirect_stdout(None):
import pygame
这个对我有用:
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
源代码包含一个保护此消息打印的条件:
if 'PYGAME_HIDE_SUPPORT_PROMPT' not in os.environ:
print('pygame %s' % ver)
print('Hello from the pygame community. https://www.pygame.org/contribute.html')
这是在最近(2018年10月)添加的,到目前为止1.9.4在此之前发布。一旦下一个版本> 1.9.4发布,您应该只需使用PYGAME_HIDE_SUPPORT_PROMPT= ./my_code.py
运行代码来隐藏消息。
您可以导航到pygame库文件夹,类似于3.6 32位版本:
Python36-32\Lib\site-packages\pygame
并编辑__init__.py文件并删除最后一行以删除此消息。
f = pygame.__file__
进入pygame的__init__.py
文件,进入该文件的底部,并注释掉这两个打印功能 -
print('pygame %s' % ver)
print('Hello from the pygame community. https://www.pygame.org/contribute.html')
但是,我不这样做,因为pygame社区是一个开源社区,他们希望尽可能多的人为pygame做贡献,这就是为什么他们最后有这个打印功能。如果我是你,我不会评论它。
# remove pygame installed with "pip install..."
python pip uninstall pygame
# remove all folder with pygame
sudo apt-get update -y; sudo apt-get upgrade -y
sudo apt-get install python-pyame
与最后一行一起安装的版本无需通知其名称即可使用。