Can open a program in IDLE but not in Terminal,Atom etc - python

Hi I'm new to python and pygame but I am using Atom but I can't solve this problem. When I open my script in IDLE it works but I can't make it work on Atom (specifically terminal-plus) and Terminal. My error message is
Traceback (most recent call last):
File "cargame.py", line 32, in <module>
carImg = pygame.image.load('racecar.png')
pygame.error: Failed loading libpng.dylib: dlopen(libpng.dylib, 2): image not found
so can anyone help?

Perhaps the reason why pygame can't load the image is because it has no context of where the module is being loaded from. Make sure the module (essentially the file where your source code exists), and image file exist in the same environment on disk (the same folder). Otherwise pass an absolute path to the load function and see if the image can be located.

Related

pyinstaller executable has errors but is completely fine when opening .py file on terminal

I'm trying to make an executable file for a project so my friends don't need to install python, using pyinstaller
When using pyinstaller with "-w"
The executable shows an Attribute error
Traceback (most recent call last):
File "main.py", line 7, in <module>
display = calendar_object.make_calendar()
AttributeError: 'Calendar' object has no attribute 'make_calendar'
but when I open main.py in the terminal it is completely fine.
When using pyinstaller without "-w"
It opens and closes immediately, and yes I have a while loop and an input inside so I'm guessing there's again, something wrong with my file
Temporary Solution:
Inserting all the separate files into one file.
Code Sample:
main.py
calendar.py
You seem to be using a name for your class that is mimicking the python internal calendar class.
Rename your class and you should be fine

File not found in working directory pygame

I am attempting to make a simple pygame game, and I need to load in some images. When I run my code, or:
bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()
It gives me an error which says:
Traceback (most recent call last):
File "C:\Users\victo\Desktop\Assets - Pulga\mainpulga.py", line 12, in <module>
bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()
FileNotFoundError: No file 'assets/Fondotiff.tif(1).png' found in working directory 'C:\Users\victo\Desktop\Assets - Pulga'.
***Repl Closed***
I have double checked if the image is inside the folder on my desktop and it is there, but for some reason it doesn't find it.
How can I fix this?
The asset file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.
e.g.:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

Error with fonts when using pyinstaller to convert a pygame app to executable

I created a pygame app that has one main python file and an assets folder. I have used pyinstaller to convert the app to an executable file, but every time I receive errors - when I resolve them, different errors come up. I suspect the errors I have been getting recently have to do with the text in my pygame file.
This is the pygame font line I used:
font = pygame.font.SysFont("Arial", 32)
I have tried using .ttf files and copying them into the same folder as my main.py file, but that didn't work either. I also make sure to copy my assets folder into the same folder as the main.app file, but that has not helped either.
I am not sure what this error message is telling me, and there seems to be no other question with this same message (there is no line in my code that is referenced in this error message for me to check).
Fatal Python error: (pygame parachute) Segmentation Fault
Traceback (most recent call last):
File "pygame/pkgdata.py", line 50, in getResource
File "site-packages/pkg_resources/__init__.py", line 1134, in resource_exists
File "site-packages/pkg_resources/__init__.py", line 1404, in has_resource
File "site-packages/pkg_resources/__init__.py", line 1473, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
Any help would be amazing, thank you!
If you use ttf-files, then you need to import them directly instead of using SysFont. So as an example:
size = 12
myfont = pygame.font.Font("Arial.ttf",size)
Arial.ttf also needs to be placed in the same folder where your executable is, or if you have a deeper folder structure, use relative paths (and the folders need to be next to your executable as well):
size = 12
myfont = pygame.font.Font("./myfolder/Arial.ttf",size)
For testing purposes it might also help to use the following command to see from which folder you start, to set your relative path and to know where you are looking for the ttf file:
import os
os.getcwd()

Trouble playing an MP3 file in python via Pyglet

Summary: (Trying to play MP3 in python via pyglet library, AVbin not detected, Mp3 not played)
So i'm trying to to play an MP3 file in python with pyglet. i installed pyglet with the command prompt and all that good stuff, and I went to the AVbin website and downloaded and ran their .exe file. Im not sure if just running the .exe file actually installs AVbin, but that's all ive done with it so far. So my problem is im trying to play an mp3 that i have located on my desktop, but Pyglet still doesnt recognize AVbin? Here's the code as well as the error message:
Code:
import pyglet
music = pyglet.media.load(r"C:\Users\Doug\Desktop\01 Gem Shards - 4A.mp3")
music.play()
pyglet.app.run()
Error:
Traceback (most recent call last):
File "C:/Python34/test.py", line 3, in <module>
music = pyglet.media.load(r"C:\Users\Doug\Desktop\01 Gem Shards - 4A.mp3")
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\__init__.py", line 1429, in load
source = get_source_loader().load(filename, file)
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\__init__.py", line 1410, in load
return riff.WaveSource(filename, file)
File "C:\Python34\lib\site-packages\pyglet-1.2.3a1-py3.4.egg\pyglet\media\riff.py", line 201, in __init__
'AVbin is required to decode compressed media')
pyglet.media.riff.WAVEFormatException: AVbin is required to decode compressed media
Add AvBin to your PATH enviroment variable:
you can do this with the folowwing command
setx path "%path%;c:\pathToAvBin"
plase note that you need to change "c:\pathToAvBin" for your real path to av bin :)
BE CAREFULL TO NOT CLEAR YOUR PATH

JPype won't compile properly

So I am having trouble compiling a very simple python script using JPype.
My code goes like:
from jpype import *
startJVM(getDefaultJVMPath(), "-ea")
java.lang.System.out.println("hello world")
shutdownJVM()
and when I run it I receive an error saying:
Traceback (most recent call last): File "test.py", line 2, in
<module>
startJVM(getDefaultJVMPath(), "-ea") File "/usr/lib/pymodules/python2.7/jpype/_core.py", line 44, in startJVM
_jpype.startup(jvm, tuple(args), True) RuntimeError: Unable to load DLL [/usr/java/jre1.5.0_05/lib/i386/client/libjvm.so], error =
/usr/java/jre1.5.0_05/lib/i386/client/libjvm.so: cannot open shared
object file: No such file or directory at
src/native/common/include/jp_platform_linux.h:45
I'm stuck and I really need help. Thanks!
I had the same problem
RuntimeError: Unable to load DLL [/usr/java/jre1.5.0_05/lib/i386/client/libjvm.so], error = /usr/java/jre1.5.0_05/lib/i386/client/libjvm.so: cannot open shared object file: No such file or directory at src/native/common/include/jp_platform_linux.h:45
In my case wrong JAVA_HOME path was set
/profile/etc
export JAVA_HOME
JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64
PATH="$JAVA_HOME/bin:$PATH"
export PATH
The work around is to define the full path directly in the call to the JVM:
from jpype import *
startJVM('/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/MacOS/libjli.dylib', "-ea", "-Djava.class.path=/tmp/Jpype/sample")
java.lang.System.out.println("Hello World!!")
shutdownJVM()
Original text:
Similar issues when trying to run JPype on MacOS El Capitan. I could
not figure out how to coax the _darwin.py code finding the correct JVM
location, despite the JAVA_HOME system variable being set properly.
Caveat cursor, trying to run the above code in the Spyder IPython console did not produce any output, but the normal Console would.

Categories

Resources