Pygame: Font module not available - python

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.

Related

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

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"

Pillow import error from _imaging.so / libjpeg.8.dylib on OSX

I've used Pillow in the past on OSX without problems, however I now get the following error.
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 61, in <module>
from PIL import _imaging as core
ImportError: dlopen(/Library/Python/2.7/site-packages/PIL/_imaging.so, 2): Library not loaded: /usr/local/lib/libjpeg.8.dylib
Referenced from: /Library/Python/2.7/site-packages/PIL/_imaging.so
Reason: image not found
I've seen this question which appears to be a similar problem but I don't think I installed Pillow with brew.
I've also tried the solution from this question but the command
pip install PIL --allow-external PIL --allow-unverified PIL
dosent seem to work and I get an error (no such option: --allow-unverified)
Lastly, I've tried to recreate the symbolic link to libjpeg.8.dylib but that also did not make any difference.
Would anyone know how to fix this error? Do I need to do something to relink _image.so aswell as libjpeg?
Python PIL was deprecated eons ago and you should not attempt to use it anymore.
What you want is python[23] -m pip install Pillow
PS. Using pip command will be deprecated soon because of confusions regarding python interpreters, instead of calling pip ... everyone should use only python -m pip ... which assures that is calling the right interpreter.

can't install pyside and QT Mac OSX 10.9

I'm more or less new to python
I'm trying to install pyside and QT on a mac but I can't figure out how to install them.
I downloaded install files "pyside-1.1.0-qt47-py27apple.pkg" from http://qt-project.org/wiki/PySide_Binaries_MacOSX and "Qt libraries 4.8.5 for Mac" from http://qt-project.org/downloads.
double click on both files and installed. but it looks like it didn't work.
I can import PySide but nothing else.
in Python Idle when I type
import PySide
print PySide.__version__
1.1.0
then if I do:
import PySide.QtCore
I get this error.
Traceback (most recent call last): File "", line 1, in
import PySide.QtCore ImportError: dlopen(/Library/Python/2.7/site-packages/PySide/QtCore.so, 2): Library
not loaded: QtCore.framework/Versions/4/QtCore Referenced from:
/Library/Python/2.7/site-packages/PySide/QtCore.so Reason: no
suitable image found. Did find:
/Library/Frameworks/QtCore.framework/Versions/4/QtCore: mach-o, but
wrong architecture
I have python 2.7 and PyCharm. I also have installed setuptools and pip.
I looked around online and I see a lot of people had the same problem. but I couldn't find any answer that worked for me.
I hope anyone can help me out with this.
This worked for me:
sudo pyside_postinstall.py -install
Note: I'm using PySide 1.2.2, Qt 4.8.6, Mac OS X 10.9.3.
Edit: There is no more need to call the post-install script on Linux and MacOS systems since version 1.2.3 (2015-10-12).

Python finds glib, but not _glib

I try to import glib in Python (2.7) and get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/share/pyshared/glib/__init__.py", line 22, in <module>
from glib._glib import *
ImportError: No module named _glib
Seems as if glib is found, while _glib is not found.
Has anybody a hint for solving this problem? I'm running Ubuntu and installed python2.7, python-gobject, python-gda, dbus from Ubuntu's package manager.
I had this issue.
You can try moving the glib and related folders out of the python2.7 install locations e.g. on ubuntu /usr/shared/pyshared in case these have been installed wrongly.
Then remove and reinstalling the related packages with the platform package manager ...
sudo apt-get remove
followed by sudo apt-get install
for ...
python-gobject python-gtk libglib2.0-dev python-cario python-gi
since if they are present in pyshared I found that they were not probably reinstalled
I run ubuntu 10.04, with its native python 2.6.6 and GCC 4.4.5, and an "import glib" in python appears to work.
I installed "python-numpy", "python-matplotlib" and "python-pyfits" with the synaptic package manager and nothing else, in particular nothing installed from a more recent version found on the package's site because previous experience learned me this was usually a very bad idea.
But I have NO /usr/share/pyshared/glib/ folder and do not know how you get it.
Is this folder on your path or PYTHONPATH ?
If yes, try to remove it from the path and retry the "import glib", it is possible that you get some second installation of glib in that place (installed - and maybe needed - by another package).
glib is an interface to the c compiler, so it is used in several places. What is your GCC version ? GTK also makes intensive use of glib. Did you installed "GTK" and "pygtk" from the synaptic or did you did it on the hard way, with all separated little packes to be compiled ? If yes, try to install it from the synaptic.
I mostly think this comes from some installation mix of some python extension somewhere. The radical solution, of course, is to reformat the computer with a fresh O.S., but it's probably something you do not want, so I should try to uninstall all packages python uses, and re-install them from the synaptic.
Hope this helps...

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