Here's the better way alternative suggested by @Mad Physicist:
import contextlib
with contextlib.redirect_stdout(None):
import pygame
Or, if your Python is older than 3.4 you can achieve the same thing without the contextlib import by temporarily disabling stdout while importing pygame.
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
The source code contains a condition guarding the printing of this message:
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')
This was added fairly recently (October 2018) and so far 1.9.4 was released prior to this. Once the next version > 1.9.4 is released you should simply by able to run your code with PYGAME_HIDE_SUPPORT_PROMPT= ./my_code.py to hide the message.
Go in pygame's __init__.py file, go at the bottom of that file, and comment out those two print function-
print('pygame %s' % ver)
print('Hello from the pygame community. https://www.pygame.org/contribute.html')
However, I would not do that since, pygame community is an open-source community, and they would want as many people as possible, to contribute to pygame, thats why they have this print function at that last. I would not comment it out, if I were you.
As can be seen in the source code, the message is not printed if the environment variable PYGAME_HIDE_SUPPORT_PROMPT is set. So the following code could be used to import pygame without printing the message:
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
Note that the value does not have to be "hide" but can be anything else as well, and the environment variable can also be set in other ways to achieve the same.
Go to the copied location by pasting it into the run dialog box or any other way
Step 4:
open __init__.py in any text editor and search for welcome
delete the following code from the file:
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')
You can go into pygame's __init__.py file and comment out the line that causes the message to be printed. It's exactly at line 355. Here is the code that does that.
# Thanks for supporting pygame. Without support now, there won't be pygame
#later.
if 'PYGAME_HIDE_SUPPORT_PROMPT' not in os.environ:
print('pygame {} (SDL {}.{}.{}, Python {}.{}.{})'.format(
ver, *get_sdl_version() + sys.version_info[0:3]
))
print('Hello from the pygame community.
https://www.pygame.org/contribute.html')
You can just go ahead and comment those lines out. I have tested it, it does not cause any problems.
But always be thankful for pygame's free and opensource library.
For me, PYGAME_HIDE_SUPPORT_PROMPT does not work.
Add this for the whole block of imports:
sys.stdout = open(os.devnull, "w")
# your other imports go here
import pygame
sys.stdout = sys.__stdout__
I kindly ask you though to only use this if the program will be launched graphically, to avoid spawning a console, or when you'll leave another message. You can print a shorter or more fitting one, or you can add it in a GUI.
Editing Pygame is not desirable if you are going to distribute your project in any way.
About Eduardo's answer, I was having problems with my formatter autopep8 and was unable to put the line to set the PYGAME_HIDE_SUPPORT_PROMPT environment variable above the line to import pygame. Thus, I had to do something like this:
import os # last import (all other imports above this one)
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = 'hide'
def import_pygame():
global pygame
import pygame
import_pygame()