98

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

Why is this printed? How can I disable this message?

wovano
  • 4,543
  • 5
  • 22
  • 49
Aaron
  • 1,109
  • 1
  • 7
  • 4
  • 92
    It is rude of a library to self-advertise. – fche Jan 05 '19 at 15:13
  • 1
    Relevant pygame issue: https://github.com/pygame/pygame/issues/1468 – Cas Dec 13 '19 at 17:38
  • OH great... Another env variable (and appears to not work if importing mixer from) -- Time for a new library – Angry 84 Jun 15 '22 at 10:37
  • 1
    @fche it is even more rude to censor opinions by locking the correspinding GitHub issue github.com/pygame/pygame/issues/1468. This is a plain an clear ad spam in a software. I opened another issue here: https://github.com/pygame/pygame/issues/3933. Luckly stackoverflow has a better report filter policy than GitHub. – Eduardo Pignatelli Jun 21 '23 at 09:56

13 Answers13

133

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.

wovano
  • 4,543
  • 5
  • 22
  • 49
Eduardo Dalapicola
  • 1,347
  • 1
  • 8
  • 2
44

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
tsbertalan
  • 1,453
  • 1
  • 21
  • 30
  • 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
17

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,413
  • 1
  • 15
  • 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
  • 6,304
  • 3
  • 24
  • 46
Kalif
  • 109
  • 2
4
  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
  • 589
  • 10
  • 11
1

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,372
  • 22
  • 22
1

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

I hope this helps anyone having the same issue.

Pyzard
  • 451
  • 3
  • 14
  • This is one of the cases where I'd rather disable a checker (or put a suppression in the code if possible) then to sacrifice good coding practices. Another work-around is to put the `import os; os.environ = ...` in a file `_manifest.py` and then do `import _manifest` inside the main script. This way you achieve the same goal while still following the PEP8 conventions. – wovano Oct 22 '22 at 12:54
0

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,422
  • 16
  • 26
0

Here's how I accomplished this in my module named DialogX:

import sys

sys.stdout = open(os.devnull, "w")
import pygame
sys.stdout = sys.__stdout__
del globals()["sys"]
RixTheTyrunt
  • 185
  • 3
  • 11
-2

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
  • 511
  • 5
  • 7
-2

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
  • 14,916
  • 3
  • 26
  • 46
-2

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
  • 6,304
  • 3
  • 24
  • 46
Duffy
  • 123
  • 7
-4
# 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
  • 36,443
  • 4
  • 48
  • 76
web33
  • 5