Jupyter Notebook, Pygame: No video window opens [duplicate] - python

I have this setup:
$ python -V
Python 2.7.2+
$ python -c "import pygame; print pygame.__version__"
1.9.1release
When I run a pygame script, I get this error:
Traceback (most recent call last):
File "/home/santosh/tmp/pygameHelloWorld.py", line 8, in <module>
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.error: No available video device
I am using Ubuntu and have install pygame with apt-get. Additionally I have install all dependencies mentioned on this pygame wiki page.

If you are running Pygame on a UNIX system, like a Linux server, try using a DummyVideoDriver:
import os
os.environ["SDL_VIDEODRIVER"] = "dummy"

From similar experience the most likely problem is something is wrong with one of your SDL packages. Try running the following.
import pygame
pygame.init()
pygame.display.list_modes()
If you get back an empty list, it's definitely because of the packages. Try reinstalling them.
Possibly related:
SDL init failure, reason is: No available video device
SDL init failure, reason is: No available video device in ubuntu 12.04 LTS

This problem might only affect Windows XP. Try adding one of these to your code:
import os
os.environ['SDL_VIDEODRIVER']='windib'
or
import os
os.environ['SDL_VIDEODRIVER']='windlib'
Good luck.

Wild guess - is the DISPLAY environment variable set and/or required by the library, and not available? This could be the case if you're ssh-ing into the Linux machine you're running this on without providing the -X option.

Try the following:
import pygame
from pygame.locals import *
pygame.init()

On Linux you can solve the problem using x11.
Verify that your SDL was built with x11 support, otherwise build it yourself only after that you added some libraries in this way:
sudo apt install xorg-dev libx11-dev libgl1-mesa-glx
Maybe xorg-dev already installs libx11-dev
Build SDL from source and enjoy

I solved it with the three commands below:
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install libsdl1.2debian:i386

I was getting this error while trying to render an OpenAI gym in a jupyter notebook. Following fushan's answer got it to stop erroring, but it only rendered a black image. Looking at https://wiki.libsdl.org/FAQUsingSDL, I figured out that I could use directfb as the renderer. The code ended up being:
import os
os.environ["SDL_VIDEODRIVER"] = "directfb"

Related

ModuleNotFoundError: No module named 'simpleaudio'

Basically, I recently started Python. I'm working on a project where I need audio to play. I searched up some libraries that can play audio and tried simpleaudio. I'm using Windows and sucessfull installed simpleaudio using: pip3 install simpleaudio.
However, when I tried to use simpleaudio in my project with import simpleaudio as sa, it gives me this error:
Traceback (most recent call last):
File "d:\coding\python\python projects\random tests\soundtest.py", line 1, in
import simpleaudio as sa
ModuleNotFoundError: No module named 'simpleaudio'
Any idea what is wrong?
After I installed simpleaudio, import simpleaudio as sa worked with no errors. I've run into similar errors before, and it was always because the version of pip I was using and the Python interpreter I was using didn't match. You probably need to either switch to using the same interpreter as pip3 or install the package for whatever interpreter you're using
The most probable issue is the use of differing version of python. For instance if you use pip3 it is for python3 only
Since the module was not found try
pip install simpleaudio
and then importing again
It's possible you installed the library for python2 instead of python3. In ubuntu pip is python2 and pip3 is python3- I'm not sure if that's the case on windows or not.

Using Mayavi on macOS with PyQT5

I tried to setup Mayavi on macOS (currently running Sierra) using the following:
brew install --with-qt5 vtk
brew install mayavi
This should theoretically work but now when I try to use Mayavi in my code I get the following error:
Traceback (most recent call last):
File "test.py", line 32, in <module>
mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300))
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/figure.py", line 63, in figure
engine = get_engine()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 101, in get_engine
return self.new_engine()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 146, in new_engine
check_backend()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 49, in check_backend
''')
ImportError: Could not import backend for traits
_______________________________________________________________________________
Make sure that you have either the TraitsBackendWx or the TraitsBackendQt
projects installed. If you installed Mayavi with easy_install, try
easy_install <pkg_name>. easy_install Mayavi[app] will also work.
If you performed a source checkout, be sure to run 'python setup.py install'
in Traits, TraitsGUI, and the Traits backend of your choice.
Also make sure that either wxPython or PyQT is installed. wxPython:
http://www.wxpython.org/ PyQT:
http://www.riverbankcomputing.co.uk/software/pyqt/intro
I belive the error lies in the fact that Homebrew only supports Qt5 and PyQT5 while Mayavi is looking for PyQT4 in the background. Is there a way to get Mayavi to work with PyQT5 or is there a way to install PyQT4 with brew?
And of course the problem could lie elsewhere....
Thanks!
An alternative is to use wx instead of qt, see the docs here: docs.enthought.com/mayavi/mayavi/installation.html
Then, set the environment variable export ETS_TOOLKIT=wx - it is worth a try.
I would recommend against using wx; I've run into a mess of issues getting it to run Mayavi examples (not to mention incompatibility with virtual environments). And from the mayavi repo:
Mayavi itself should work with the new wxPython 4.x. However, traitsui, pyface, and other ETS packages do not yet support it so the UI will not work correctly. Older versions should work. PyQt/PySide should work largely out of the box.
Here's how to install Mayavi w/ PyQt4 on macOS Sierra+:
brew install vtk
brew tap cartr/qt4
brew tap-pin cartr/qt4
brew install cartr/qt4/qt
brew install cartr/qt4/pyqt
brew install pyside
pip install mayavi
These steps make sure you get v4, not the default v5. And you may want to do unset ETS_TOOLKIT to make sure mayavi doesn't look for a different backend.
FWIW PyQt5 is not available (at least as a PyPI package) for Python 2x -- I see you're running Python 2.7. You can do pip3 PyQt5, but mayavi dependencies will need extra setup for Python 3 -- specifically pip3 install traitsui.

Pygame "No module called pygame.base"

When I download pygame-1.9.2b8-cp36-cp36m-win32.whl from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame I turn it into zip, take the headers in the pygame-1.9.2b8.data folder into pygame34/include/pygame and I put the other pygame file from the zip in the site-packages folder together with pygame-1.9.2b8.data as any tutorial shows, I always get this error:
Traceback (most recent call last):
File "C:/Users/andsa/PycharmProjects/THIS WORKS ANDREAS/Games.py", line 1, in <module>
import pygame
File "C:\Python34\lib\site-packages\pygame\__init__.py", line 141, in <module>
from pygame.base import *
ImportError: No module named 'pygame.base'
EDIT: I use python 3.4.5, I have tried both python and pygame 64 and 32 bit but i still get this error
This error bugged me for months, what i did was:
Uninstalled the package
pip3 uninstall pygame
Purged the pip cache
pip cache purge
Finally...Reinstalled pygame using pip3
pip3 install pygame
Worked like a charm :)
Use pip3 tool to reinstall the pygame python module:
C:\Python373\Scripts>pip3 uninstall pygame
C:\Python373\Scripts>pip3 install pygame
Then install it:
C:\Python373\Scripts>pip install pygame-menu==2.0.1
You probably have python installed for 32-bit. Uninstall and the install the 64-bit version. After that,
pip3 uninstall pygame
pip3 install pygame
try usin pip3 install pygames
to install pygames
pip3 install pygame
SUBLIME TEXT SOLUTION
OBS: THE REASON MOST GUI FRAMEWORKS DO NOT WORK ON SUBLIME IS BECAUSE OF THIS ONE LITTLE DETAIL HERE
Create a new build system:
In Sublime go to >> Tools
Tools >> Build System
Build System >> New Build System
DELETE EVERYTHING CONTAINED IN THIS NEW FILE AND ADD THIS PIECE OF CODE:
{
"cmd": ["/usr/local/bin/python3.8", "-u", "$file"],
"file_regex": "^[]*File \"(...*?)\", line ([0-9]*)"
}
ATTENTION: DO NOT CHANGE THE CURRENT DIRECTORY YOU ARE ON, WHICH IS THE
USER ONE.
SAVE IT AS Python3.8.sublime-build
In Sublime go to >> Tools
Tools >> Build System
Mark the Python3.8 you have just created
Voila!
P.S I Saved mine as 3.7 but it works the same, as long as the code inside the file is 3.8, or whatever version you have on your machine currently.

Pygame: Font module not available

Running Python 2.7.2, and Pygame 1.9.2pre on OS X.
Another noobious issue: Initialising the pygame.font module produces an error that I dare not... well, attempt to solve myself. I'm guessing this is a rather generic pygame-related issue... but I haven't been able to find any solutions.
import pygame
pygame.init()
pygame.font.init()
Gives:
Desktop/font.py:4: RuntimeWarning: use font: dlopen(/Library/Python/2.7/site-packages/pygame/font.so, 2): Library not loaded: /usr/X11/lib/libfreetype.6.dylib
Referenced from: /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf
Reason: image not found
(ImportError: dlopen(/Library/Python/2.7/site-packages/pygame/font.so, 2): Library not loaded: /usr/X11/lib/libfreetype.6.dylib
Referenced from: /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf
Reason: image not found)
pygame.font.init()
Traceback (most recent call last):
File "Desktop/font.py", line 4, in <module>
pygame.font.init()
File "/Library/Python/2.7/site-packages/pygame/__init__.py", line 70, in __getattr__
raise NotImplementedError(MissingPygameModule)
NotImplementedError: font module not available
(ImportError: dlopen(/Library/Python/2.7/site-packages/pygame/font.so, 2): Library not loaded: /usr/X11/lib/libfreetype.6.dylib
Referenced from: /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf
Reason: image not found)
What exactly is wrong here? What are some possible reasons that the font module could not be available? Common causes? Common fixes?
If anyone can enlighten me about what's wrong, I'll appreciate it even if it doesn't solve the problem!
Thanks very much!
Edit: I am using 64 bit version of Python with 32 bit Pygame. (There seems to be no 64 bit Pygame available for Mac OS)
(self answer)
Problem: Using 32-bit Pygame with 64-bit Python generally isn't a good idea.
Solution: Always use Python and Pygame both in 64-bit architecture. (Or 32).
P.S. Currently there doesn't seem to be a 64-bit Pygame for OS X, so you'll have to use a 32-bit Python.
P.S.S (After installing a 32-bit Python you may need to re-install the 32-bit Pygame for it to have any effect. This is what happened to me)
I encounter the same problem, and the making-this-problem progress is as follow:
I download the pygame source file from the http://www.pygame.org/
I install the pygame with command python setup.py install
Then, I can import pygame and use basic pygame functions normally, but if I try to import other relative modules like import pygame.font, it jumps out font module not found error. I try to fix this problem by install some missing modules manually as suggested here:
$ sudo apt-get install python-dev libsdl-image1.2-dev libsdl-mixer1.2-dev
libsdl-ttf2.0-dev libsdl1.2-dev libsmpeg-dev python-numpy subversion
libportmidi-dev ffmpeg libswscale-dev libavformat-dev libavcodec-dev
However, this problem still exists.
At last, I realize this is a dependency problem, so I remove installed pygame by the command sudo python setup.py clean and remove all files in python/site-packages. I reinstall the pygame by the command:
sudo apt-get install python-pygame
and everything works fine.

Problems importing python-Xlib

I installed a new module and it appears as if one of its dependencies was not already installed. The module is called Xlib.display.
Here is the error message I received:
from Xlib.display import Display
ImportError: No module named Xlib.display
Where can I find this module that I am apparently lacking? Google yielded no leads.
"Edit: I already have that sourceforge module downloaded but I still get the same results.
Please try.
This shall install Xlib
sudo apt-get install python-xlib
Then you can check
>>from Xlib.display import Display
To install PyMouse if you want to control and capture mouse events please use:
sudo easy_install https://github.com/pepijndevos/PyMouse/zipball/master
Below worked for me!
pip install python3_xlib
I have also used pyuserinput for automation which requires this.
I was having the same problem, but the solutions above didn't work for me. Since I had installed python through the anaconda package, when I used:
sudo apt-get install python-xlib
Xlib was still undetectable by python2. The solution in my case was to use:
anaconda search -t conda python-xlib
Then find the package from the anaconda api, mine was erik/python-xlib. Install it using:
conda install --channel https://conda.anaconda.org/erik python-xlib
Then it worked.
On Debian systems install python-xlib.
On other systems there's a high probability that the package carries the same name.
I don't think the Xlib library works in Python 3.
Source:
Requirements
The Python X Library requires Python 1.5.2 or newer. It has been tested to various extents with Python 1.5.2 and 2.0 through 2.6.
I honestly cant explain why this works... but here is the command that got it working for me.
sudo apt-get install python3-xlib
Should not work because xlib apparently does not work with python 3.x, but everything installed alright, so I'm not complaining!
I was looking for the same answer, however after some more digging it seems that XCB (X protocol C-language Binding) will obsolete Xlib in general. From the XCB website:
The X protocol C-language Binding (XCB) is a replacement for Xlib featuring a small footprint, latency hiding, direct access to the protocol, improved threading support, and extensibility.
Fortunately there are python bindings available as python-xpyb in apt or xpyb on PyPi. I've not gotten that far in my project so I haven't tested if this works with Python3, but this is probably the way to go and the proper place to file any Python3 support bugs if necessary.
Scenario:
I was trying to use screenshot functionalities of pyautogui package. I was getting this error:
Traceback (most recent call last):
File "test_screenshot.py", line 1, in <module>
import pyautogui
File ".../miniconda3/envs/myenv/lib/python3.7/site-packages/pyautogui/__init__.py", line 152, in <module>
from . import _pyautogui_x11 as platformModule
File ".../miniconda3/envs/myenv/lib/python3.7/site-packages/pyautogui/_pyautogui_x11.py", line 7, in <module>
from Xlib.display import Display
ModuleNotFoundError: No module named 'Xlib'
Python code (test_screenshot.py):
import pyautogui
img = pyautogui.screenshot('test.png')
Environment:
Ubuntu 16.04 (LTS)
conda 4.5.11
Python 3.7 (Miniconda)
requirements.txt:
certifi==2019.3.9
Pillow==5.4.1
PyAutoGUI==0.9.42
PyGetWindow==0.0.4
PyMsgBox==1.0.6
PyRect==0.1.4
PyScreeze==0.1.20
PyTweening==1.0.3
Solution:
I installed python-xlib package in the conda environment using:
pip install python-xlib
Now test_screenshot.py is running without any error.
Updated requirements.txt:
certifi==2019.3.9
Pillow==5.4.1
PyAutoGUI==0.9.42
PyGetWindow==0.0.4
PyMsgBox==1.0.6
PyRect==0.1.4
PyScreeze==0.1.20
python-xlib==0.25
PyTweening==1.0.3
six==1.12.0

Categories

Resources