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?
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.
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')
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.
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.
import pygame
Get the location of the init file: f = pygame.__file__
Open f and comment out the print on the last two lines of the file
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.
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!)
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.
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.
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.
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!
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.
# 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.
Related
I am trying to use OpenGL with python. I have Pyton 3.7.4, on a Windows 10 machine.
I downloaded PyOpenGL and PyOpenGL_accelerate from .whl files, and I have downloaded the freeglut.dll file separately and placed it in the same directory as the script I am running.
My initial scripts is just:
import OpenGL
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
glutInit()
which gives the following error message:
freeglut (foo): fgPlatformInitialize: CreateDC failed, Screen size
info may be i ncorrect This is quite likely caused by a bad '-display'
parameter
(Without the freeglut.dll file, it gives a NameError complaint about glutInit() being undefined).
I have seen error on this question but a) they were doing it with C/C++ and b) the answer did not say where one had to make the change.
Any ideas on what I am supposed to do?
UPDATE
The problem may be this:
import os
os.getenv('DISPLAY')
# 'needs-to-be-defined'
What should I call this environment variable?
I just added this piece of code at the beginning of my script and it worked:
import os
try:
del os.environ['DISPLAY']
except:
pass
Ideally I would delete the DISPLAY environment variable for all processes but I did not manage.
glutInit(["-display","0"]) - as an idea based on :
https://www.opengl.org/resources/libraries/glut/spec3/node10.html
When I import pyBullet, it immediately prints a line with the time when it was build:
In [1]: import pybullet
pybullet build time: Jun 19 2020 04:01:58
Is there a way to prevent this?
Change the source code. Jokes aside, I think there is no obivious way to prevent this from happening. Since it happens when you import the module, there is not much you can do in configuration of pybullet. Because it is literally the first thing you do with it.
Maybe you can reroute stdout during the import of the module. Which is a duplicate question already answered here: Stop Python Module from Printing
Check these solutions on a similar problem with pygame.
Why when import pygame, it prints the version and welcome message. How delete it?
Based on this, I suggest to try:
import contextlib
with contextlib.redirect_stdout(None):
import pybullet
full credit to MadPhysicist
I have a small script in python2.7 that I want to convert into Windows executable. I use pyinstaller for this.
The script:
import sys
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def get_inputs():
coor = raw_input(">>>top x left: ").replace(" ", "")
top, left = coor.split("x")
top = int(top.strip())
left = int(left.strip())
return top, left
def plot_location(top, left):
img= mpimg.imread('nbahalfcourt.jpg')
plt.imshow(img)
plt.scatter(left, top)
plt.grid()
plt.show()
def main():
top, left = get_inputs()
plot_location(top, left)
if __name__ == '__main__':
print "Input top x left coordinates (no space) eg: 44x232"
run = True
while run:
main()
Basically, the script just plots a point on a grid.
The converting process finishes successfully. When I run the .exe however I've got the ImportError (see below) even though I have no reference to Tkinter anywhere.
What could went wrong here?
I have a feeling that matplotlib uses the Tkinter module internally, but imports it in a non-standard way. Then pyinstaller doesn't notice Tkinter is needed, and subsequently doesn't bundle it into the executable.
Try explicitly putting import Tkinter at the top of your script.
I had the same issue and none of the solutions here worked. I was using Pyinstaller 3.2 (the latest version at the time) and everything was fixed (and no import statements needed) when I upgraded to the latest developer version using
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
This seems to indicate that at the times of writing this that all the kinks to this issue are still being worked out
EDIT: As of January 15 2017 Pyinstaller version 3.2.1 was released. I now use this and it solves this issue along with others like this and this that I could previously only solve by using the developer version. So I highly recommend upgrading to the latest version if you haven't already.
... could someone explain the difference?
What I type in the command prompt:
sys.path.append('M:/PythonMods')
import qrcode
myqr = qrcode.make("randomtexxxxxxxxxt")
myqr.show()
myqr.save("M:/myqr.png")
MAKES A QR FOR THE TEXT.
The code I type:
sys.path.append('M:/PythonMods')
import scipy
from qrcode import myqr
file=open('myqr3.png',"r")
myqr.show()
file.close()
It doesn't recognise sys, do I need to import something? How come it runs in the command prompt?
Thanks in advance for any help.
add at the begining of your source file:
import sys
and while we're reviewing your code, in executable source files it is advised to do so:
import sys
sys.path.append('M:/PythonMods')
import qrcode
if __name__ == "__main__":
myqr = qrcode.make("randomtexxxxxxxxxt")
myqr.show()
myqr.save("M:/myqr.png")
so your code will run only when you execute it as a file, not when you import it. You may want to define your three lines as a function, and call your function in the if __name__ == "__main__": part, to be able to reuse it like any library!
At the top of the script, please include the following line:
import sys
sys is not a built-in, you do need to explicitly import it:
import sys
The ipython interactive shell imports a lot of modules by default; perhaps you are using that to test your code. The default Python runtime does not import sys for you.
How can I in Python (v 2.7.2) completely clear the terminal window?
This doesn't work since it just adds a bunch of new lines and the user can still scroll up and see earlier commands
import os
os.system('clear')
Found here that the correct command to use was tput reset
import os
clear = lambda : os.system('tput reset')
clear()
I've tried this and it works:
>>> import os
>>> clear = lambda : os.system('clear')
>>> clear()
BEFORE
AFTER clear()
On Windows:
import os
os.system('cls')
On Linux:
import os
os.system('clear')
for windows:
import os
def clear(): os.system('cls')
for linux:
import os
def clear(): os.system('clear')
then to clear the terminal simply call:
clear()
No need of importing any libraries.
Simply call the cls function.
cls()
I am using Linux and I found it much easier to just use
import os
in the header of the code and
os.system('clear')
in the middle of my script. No lambda or whatever was needed to get it done.
What I guess is important to pay attention to, is to have the import os in the header of the code, the very beginning where all other import commands are being written...
Adding for completeness, if you want to do this without launching an external command, do:
print('\x1bc')
Only tested on Linux.
You can not control the users terminal. That's just how it works.
Think of it like write only.