Error, pygame.image.load, unable to resolve - pygame

To start, this seems to be a common issue for folks but the resolves which have helped them have been little help to myself. Preliminaries, I am running Raspbian w/Pixel on the RaspberryPi3B. I have read all relevant pygame documentation on the matter along with other resources. Here is the link to the doc; https://www.pygame.org/docs/ref/image.html#pygame.image.load .
I have re-installed everything using the shell:
$ sudo apt-get install mercurial
$ hg clone https://bitbucket.org/pygame/pygame
$ cd pygame
$ sudo apt-get install libsdl-dev libsdl-image1.2-dev libsdl-
mixer1.2-dev libsdl-ttf2.0-dev
$ sudo apt-get install libsmpeg-dev libportmidi-dev libavformat-dev libswscale-dev
$ sudo apt-get install python3-dev python3-numpy
$ python3 setup.py build
$ sudo python3 setup.py install
So now starts the fun..
I am getting the following error:
Traceback (most recent call last): File
"/home/pi/Python_Code/Revenge of the Morning Wood.py", line 19, in
lumberJack = pygame.image.load('/home/pi/Python_Code/Revenge_of_the_Morning_wood/lumberJack.png')
pygame.error: Couldn't open
/home/pi/Python_Code/Revenge_of_the_Morning_wood/lumberJack.png
Code up to the point of error:
import pygame
import time
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Revenge of the Morning Wood")
clock = pygame.time.Clock()
lumberJack = pygame.image.load('/home/pi/Python_Code/Revenge_of_the_Morning_wood/lumberJack.png'
def lumberJack(x,y):
gameDisplay.blit(lumberJack,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
It won't even show an uncompressed .bmp file. I've tried all file extensions and all combination of file locations in relation to source code/image/pygame_module.
pygame.image.get_extended() initially popped an error in python shell but after the installation detailed above now returns a value of "1".

The long awaited resolve! I removed the image extension entirely. Ensuring the image was in the pygame directory:
lumber_Jack = pygame.image.load('/home/pi/pygame/lumberJack')
This was actual mentioned in the documentation I included earlier but I overlooked it. https://www.pygame.org/docs/ref/image.html#pygame.image.load :
Pygame will automatically determine the image type (e.g., GIF or bitmap) and create a new Surface object from the data. In some cases it will need to know the file extension (e.g., GIF images should end in ”.gif”).
For those of you wondering why I changed the name of my variable. I also noticed an issue with my variable lumberjack sharing the same name as my lumberjack function but this did not contribute to the issue. For anyone else having this issue just leave off the .PNG, .BMP, JPG, etc.

Related

How can I resolve the "pygame.error: No available video device" error in pygame? [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"

AttributeError: module 'pygame' has no attribute 'init' [duplicate]

I just downloaded Pygame but is running into an error: AttributeError: module 'pygame' has no attribute 'init'. I was looking around on stackoverflow and someone fixed the error by renaming the files (which I don't know how to do). The directory is the following
Python -> Lib -> site-packages ->pygame
The confusing part for me is inside the pygame folder
pygame > Include, Lib
Include > pygame > (many hh files)
Lib > SitePackages > pygame > init (I know that it has the attribute init)
Since there is '3' pygame folder, is this causing the error for my program?
import pygame, sys
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Just rename your file from pygame.py to myTestGame.py for example; because you're importing the same file when you use import pygame !
It looks as though you have "downloaded" a source distribution of pygame, and then copied the content into site-packages. that works fine for most pure-python libraries, but not for ones with c-extensions, like pygame. You are probably best served by using an installer, as pygame is a bit tedious to build from source.
Visit http://www.pygame.org/download.shtml and choose an installer for your OS. note that the first two links are source downloads, and not what you need.
The most current, best way to install pygame is available at: https://www.pygame.org/wiki/GettingStarted
Currently, for windows, it says to install it like this in a cmd window:
py -m pip install pygame --user
Many windows users have difficulty installing Python, getting the path vars right and then adding in additional modules. A convenient solution I find for many is to use a portable version they can put on USB (Or Desktop) and run anywhere, easily.
Download and install this: Portable Python Pack
Install to USB or Desktop
From the installed folder, open PyScripter (or another if you prefer)
Open and run your PyGame or other Python code.
NOTE: This is Python 2.7, but it has PyGame and a range of other modules packed in- check it out before download and try if suited to your needs!
You have to install pygame. If you did please try it with pip. In terminal or command line type pip install pygame and if that does not work do python -m pip install pygame and if that does not work do py -m pip install pygame
i recommend y'all to use this code
import pygame, sys
from pygame.locals import *
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("Pygame Title")
def close():
sys.exit()
pygame.quit()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
1 first uninstall your IDE
2 reinstall IDE in another folder
3 goto IDE terminal and install Pygame using pip version 19.0.3
4 Then your problem is solved
it's a common mistake which is not still fixed by now if you are running your code on a IDE like , "VSC" or "pycharm" and such a kind. you will get this error.
MAKE SURE that you have installed pygame correctly using pip and try to run the code using Git Bash (by typing python filename.py) or the python's interpreter.
you can also seek info at www.pygame.org
try:
python -m pip uninstall pygame
python -m pip install pygame
This should reinstall pygame.

NameError: name 'base' is not defined OpenAI Gym

[Note that I am using xvfb-run -s "-screen 0 1400x900x24" jupyter notebook]
I try to run a basic set of commands in OpenAI Gym
import gym
env = gym.make("CartPole-v0")
obs = env.reset()
env.render()
but I get the following error:
...
~/Downloads/yes/lib/python3.7/site-packages/pyglet/gl/__init__.py in <module>()
225 else:
226 from .carbon import CarbonConfig as Config
--> 227 del base
228
229 # XXX remove
NameError: name 'base' is not defined
What can I do to fix this?
Solving your issue required getting the right combination of system dependencies and python dependencies. Paste this code into a cell in Colab and run it to install all of the dependencies (taking note of the specific version numbers used).
%%bash
# install required system dependencies
apt-get install -y xvfb x11-utils
# install required python dependencies (might need to install additional gym extras depending)
pip install gym[box2d]==0.17.* pyvirtualdisplay==0.2.* PyOpenGL==3.1.* PyOpenGL-accelerate==3.1.*
The final step is to run the following block of code to properly initialize the virtual display. The code in the below creates a virtual display in the background that your Gym Envs can connect to for rendering. You can adjust the size of the virtual buffer as you like but you must set visible=False when working with Xvfb.
This code only needs to be run once per session to start the display.
import pyvirtualdisplay
_display = pyvirtualdisplay.Display(visible=False, # use False with Xvfb
size=(1400, 900))
_ = _display.start()
For additional details check out the following blog post.
It's work for me. (And I just met the same problem)
git clone https://github.com/openai/gym.git
cd gym
pip install -e .
You can also have a try,
conda install -c conda-forge pyglet
# pyglet==1.2.4?
Before that, I installed gym with pip, maybe this is the problem.
change the code as follow
import gym
print(gym.__version__)# for me: 0.15.4
env = gym.make("CartPole-v0")
obs = env.reset()
for i in range(1000):# it's changable
env.step(env.action_space.sample())
env.render()# won't work in Google Colab
env.close()
This might work for you:
Uninstall the packages with these commands:
pip uninstall pyglet
pip uninstall gym
Then install the packages using these commands:
conda install -c conda-forge pyglet
conda install -c conda-forge gym
u can try update glfw to glfw 3.3+
You could run the algorithm from the command line:
python -m spinup.run ppo --exp_name CartPole --env CartPole-v0
Here, ppo is the proximal policy optimization algorithm, but you can run any of the algorithms you want.

Error getting large content from Chrome with Selenium WebDriver

I implemented a Selenium WebDriver test case to read the content of a JavaScript variable using Google Chrome as browser. The content of this variable can be very large since it's a Base64 String obtained from a Blob object. WebDriver stops working logging this trace:
[1080,058][SEVERE]: Too large write data is pending: size=118624364, max_buffer_size=104857600
Does anyone knows how to increase this limit (max_buffer_size) in a Chrome browser managed by WebDriver?
We got around this by editing this line in Chromium to be minorly greater than the size of the raw text in bytes (string/char types used in C++) and compiling for Chromedriver target!
// Replace x with just under your max size.
const int kBufferSize = x * 1024 * 1024; // x MB
We followed these steps for compiling.
# Install Chromium build dependencies.
echo "deb http://archive.ubuntu.com/ubuntu trusty multiverse" >> /etc/apt/sources.list # && dpkg --add-architecture i386
sudo apt-get update && apt-get install -qy git build-essential clang curl
# Install Chromium's depot_tools.
export DEPOT_TOOLS=/usr/bin/depot_tools
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git $DEPOT_TOOLS
export PATH=$PATH:$DEPOT_TOOLS
echo -e "\n# Add Chromium's depot_tools to the PATH." >> .bashrc
echo "export PATH=\"\$PATH:$DEPOT_TOOLS\"" >> .bashrc
git config --global https.postBuffer 1048576000
# Download Chromium sources.
fetch --nohooks --no-history chromium
cd <directory-installed>
gclient runhooks
cd src
build/install-build-deps.sh --no-prompt
gn gen out/Release --args="is_debug=false"
ninja -C out/Release chromedriver
cp out/Release/chromedriver /usr/bin/chromedriver
cd <wherever>
I guess the proper solution would be to have Chromium http server properly chunk this data to the buffer but I need to look into this a bit further.
It seems it is not possible to change that value (max_buffer_size) in a running instance of Chrome. I got that answer in the Chromium forum.

how to install pygobject for python3 in fedora18

i try to install pygobject for python 3 in fedora 18
i read this doc https://python-gtk-3-tutorial.readthedocs.org/en/latest/install.html and i try to install jhbuild after that when i type $ jhbuild sanitycheck
get this error : bash: jhbuild: command not found...
i try to install with yum , i install "pygobject3-devel" but this not work with python3
in fedora 18 default python is python2.7.3, i install python3.3.0 with yum and test a first demo code :
#!/usr/bin/python
from gi.repository import Gtk
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
after give chmod +x for this python file can run with this command "./test.py"
and if i try to run with python (with out use #! ....) and type this command "$python test.py" that run and work without any problem but when i type this command "$python3 test.py" i get this error :
Traceback (most recent call last):
File "test.py", line 2, in <module>
from gi.repository import Gtk
ImportError: No module named 'gi'
have any idea ?
thanks
Try installing pygobject3 instead of pygobject3-dev (as a matter of fact, it seems that pygobject3 is already installed in a vainilla installation of Fedora).
After installing that package, you should be able to open your interpreter (python2.7) and do:
from gi.repository import Gtk
If you want to use python3 (I didn't understand if you meant that you tried to use python3 or you want to use python3), it seems that the gnome instrospection bindings are not yet available, according to this page that summarizes the state of different packages python3-related that are being on the works.
EDIT: I just tried installing python3-gobject (which should be installed when installing pygobject3, but it doesn't for some reason) and it seems to be working aswell on python3.