75

Why is there a message when I import pygame, it prints the version and welcome message. The message reads

"pygame 1.9.4 Hello from the pygame community.
 https://www.pygame.org/contribute.html" 

How can I disable this message?

Mad Physicist
  • 95,415
  • 23
  • 151
  • 231
Aaron
  • 779
  • 1
  • 5
  • 4

13 Answers13

106

It works for me:

import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
Eduardo Dalapicola
  • 1,077
  • 1
  • 7
  • 2
37

I didn't see a natural way to do it (yours is the only Google result for this that I could find), but I did achieve the same thing 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

Here's the alternative suggested by @Mad Physicist:

import contextlib
with contextlib.redirect_stdout(None):
    import pygame
tsbertalan
  • 1,341
  • 1
  • 20
  • 29
  • 13
    That's perfectly natural. Try `contextlib.redirect_stdout(None)` for a preference. Fewer imports and lines in general. Also probably more robust. – Mad Physicist Jul 22 '18 at 23:36
  • 2
    btw, contextlib.redirect_stdout is a python3+ thing, not in python2 – fche Jan 05 '19 at 15:15
  • With contextlib.redirect_stdout(None), my SSH cursor disappears. For me this worked: https://stackoverflow.com/a/8391735/8474284 – DaWe Sep 01 '19 at 11:08
16

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')

See this commit

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.

Will
  • 1,183
  • 1
  • 12
  • 13
10

You can navigate to the pygame library folder, something like this for 3.6 32 bit version:

Python36-32\Lib\site-packages\pygame

and edit the __init__.py file and remove the last line to get rid of this message.

Alex Waygood
  • 4,796
  • 3
  • 14
  • 41
Kalif
  • 109
  • 2
5
  1. import pygame
  2. Get the location of the init file: f = pygame.__file__
  3. Open f and comment out the print on the last two lines of the file
Hunaphu
  • 385
  • 6
  • 7
1

For me, only this worked in python 3:

import sys, os

# Disable print
def blockPrint():
    sys.stdout = open(os.devnull, 'w')

# Enable print
def enablePrint():
    sys.stdout = sys.__stdout__


blockPrint()
import pygame
enablePrint()

(thanks for Brigand!)

DaWe
  • 1,011
  • 12
  • 19
  • 1
    At least in more recent versions of PyGame, you can simply set an environment variable instead, which is a lot more neat :) See Eduardos answer. – runemoennike Oct 24 '19 at 19:06
  • When you `from moviepy.editor import VideoFileClip`, Eduardos answer won't work. – DaWe Mar 10 '20 at 10:59
0

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.

Gugalcrom123
  • 1
  • 2
  • 1
0

alt click or ctrl click in pygame in your code and ctrl + f and then paste'pygame 1.9.4 Hello from the pygame community. https://www.pygame.org/contribute.html'... you have to find were that message form that python file so you ctrl + f and type the message or just type https://www.pygame.org .... is this clear

thenoober
  • 69
  • 9
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 07 '22 at 20:01
0

This works fine for me:

from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'

import pygame

You just have to make sure your imports don't get rearranged.

And it has come before any import of anything that imports pygame, not just before your import of pygame.

Darrel Lee
  • 2,154
  • 19
  • 22
-1

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.

Tushar
  • 413
  • 5
  • 6
-1

This is one time process to disable it!

Step 1:

  • Run a dummy.py containing following code:
import pygame 
    
pygame.__file__

Step 2:

  • Copy the path of the pygame source code excluding __init__.py

Example:

C:\\Users\\dell\\AppData\\Roaming\\Python\\Python37\\site-packages\\pygame\\

Step 3:

  • 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')
  • Now save the file and you are good to go!
MrBean Bremen
  • 12,145
  • 3
  • 19
  • 36
-1

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.

Alex Waygood
  • 4,796
  • 3
  • 14
  • 41
Duffy
  • 95
  • 7
-3
# 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-pygame

The version installed with the last line will work without announcing its name.

Davis Herring
  • 30,864
  • 3
  • 33
  • 65
web33
  • 7