Simple Pong game will not run as an .exe - python

I am new to this forum and have limited python experience. Since this quarantine I decided to get back into learning python. I have over the past few days made some working games using both curses and pygame. These games function properly on my PC however I would like to get them into an .exe format. I have used pyinstaller in order to create the .exe however my pong game (which uses pygame) crashes once you try to open the .exe file. I am assuming it may have something to do with the import of pygame. Any help would be much appreciated.
Here is a link to my code on GitHub:
https://github.com/nick-cheshire/PythonGames-/blob/master/Pong.py
Thanks to #TheBigKahuna I was able to create a .bat file and run it and here are the errors I recieved :
``Traceback (most recent call last):
File "Pong.py", line 137, in <module>
pong()
File "Pong.py", line 90, in pong
game_font = pygame.font.Font("freesansbold.ttf", 32)
File "site-packages\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 1472, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
[18912] Failed to execute script Pong``

I have managed to make it work. The problem was with font. Change your line
game_font = pygame.font.Font("freesansbold.ttf", 32)
with
game_font = pygame.font.SysFont("Arial", 20)
I assume that the problem is with locating the file. Please correct if I am wrong.
Now when making .exe with pyinstaller, make sure your file is named main.py and since you are building a GUI you will want -w (without a console) parameter. So, I suggest running powershell as administrator, navigate to your main.py and:
pyinstaller --onefile -w main.py
Hope this works.

Related

Problem making python program into an exe

I have been working on a program that shows the weather using python and thought I would try setting it up as an exe for easy installation on other devices. However every time I put in a location to search for using the exe GUI, I get this in the terminal window that opens with the program window:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1892, in __call__
File "getWeather.py", line 132, in <lambda>
File "getWeather.py", line 45, in open_weather
File "requests\api.py", line 75, in get
File "requests\api.py", line 61, in request
File "requests\sessions.py", line 529, in request
File "requests\sessions.py", line 645, in send
File "requests\adapters.py", line 417, in send
File "requests\adapters.py", line 228, in cert_verify
OSError: Could not find a suitable TLS CA certificate bundle, invalid path: C:\Users\pcusername\AppData\Local\Temp\_MEI118802\certifi\cacert.pem
I have been trying to figure out what the problem is, but haven't been able to find anything to get it working. If anyone can help figure this out I would appreciate it. Also, let me know if any more context/code is needed or if I did anything wrong regarding the post, as it is my first time having to post on stack overflow.
Note, this is using:
windows 11,
python 3.9,
pycharm 2021.2.2,
pyinstaller for exe creation
Ok ended up figuring it out:
This method does work: (The top answer) python requests can't find a folder with a certificate when converted to .exe
I was however missing the cacert.pem file still, which gave the error when I tried this fix initially. What ended up working is finding the cacert.pem in the mypycharmproject\venv\Lib\site-packages\certifi folder and then copying it to the dist folder where the exe is located.
Thank you for your help everyone!

How do I convert my simple game into an executable from a .py file?

I've been trying for a long time to convert my .py file which is 100% working into a .exe file so that I can click it and run it without going into the python editor. I followed these steps: https://stackoverflow.com/a/44433442/12309053 but nothing has been working. I'm having issues when I try to run the python setup.py build line, I don't get an error it just doesn't run and outputs another line for me to input a command again. I believe to have successfully installed cx_freeze as well as pyinstaller(not sure if relevant).
I'm struggling to figure out why I can't run this command.
I did run a command prior to trying the steps listed in the other forum, which lead me to actually create build folders etc. The problem was there was no .exe file to run and there was error in the command prompt. I apologize as I forget what I ran before.
Recieve the output when running pyinstaller:
'''
File "c:\users\jamal\appdata\local\programs\python\python38-32\lib\site- packages\PyInstaller\building\utils.py", line 653, in <genexpr>
strip_paths_in_code(const_co, new_filename)
File "c:\users\jamal\appdata\local\programs\python\python38-32\lib\site- packages\PyInstaller\building\utils.py", line 660, in strip_paths_in_code
return code_func(co.co_argcount, co.co_kwonlyargcount, co.co_nlocals,
co.co_stacksize,
TypeError: an integer is required (got type bytes)

Pyinstaller builds file but chokes on certain .dlls

When I run my py program it works the way I intended it to. If I am on a Linux box and build an executable using Pyinstaller, it builds without issue and executes without issue. I have scoured the Pyinstaller docs, git, etc. none of the posted fixes helped
I am still very new at python and feel like it might be a simple fix and might be over thinking the issue
Why can I no build a functional .exe on a windows based system using pyinstaller?
Windows 10 system
Pyinstaller version 3.2
Python version 3.5.2
This is a GUI program using appJar which is also up to date.
The file does build, but errors "Could not execute script"
EDIT
Not sure if this is best to edit in line like this but...
So studying the output and making adjustments, the issue seems to be appJar.py. For some reason it is missing assets, I am looking into it. The trouble is that I am still not used to looking at this kind of output and am not sure where to start.
C:\Users\_User_>C:\temp\fileCreatorGUI\fileCreatorGUI.exe
Traceback (most recent call last):
File "F:\Users\_User_\python_working\fileCreatorGUI.py", line 73, in <module>
app = gui()
File "C:\Users\_User_\AppData\Local\Programs\Python\Python35\lib\site-packages\appJar\appjar.py", line 509, in __init__
self.topLevel.wm_iconbitmap(self.appJarIcon)
File "C:\Users\_User_\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1716, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "C:\temp\fileCreatorGUI\appJar\resources\icons\favicon.ico" not defined
Failed to execute script fileCreatorGUI
Edit 2
See answer below, but I was barking up the wrong tree on this one,
The Pyinstaller output chokes on the .dll's:
api-ms-win-core-console-l1-1-0.dll
api-ms-win-core-datetime-l1-1-0.dll
(There are like ~40 of these)
I added those .dll's to the python path, I declared them in the bianaries in the .spec file.
here is a truncated log:
2414 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-stdio-l1-1-0.dll
Traceback (most recent call last):
File "C:\Users\_USER_NAME\AppData\Local\Programs\Python\Python35-32\lib\site-pa
ckages\PyInstaller\depend\bindepend.py", line 695, in getImports
return _getImports_pe(pth)
File "C:\Users\_USER_NAME\AppData\Local\Programs\Python\Python35-32\lib\site-pa
ckages\PyInstaller\depend\bindepend.py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2423 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-heap-l1-1-0.dll
I tried the fix listed here:
https://github.com/pyinstaller/pyinstaller/pull/1981
but it did not seem to make a difference.
Someone recommended adding the sys.path.insert() route but it did not make a difference either way
I also tried this in a VM with windows 7, clean install, no change. My next step is to try to use Wine in Debian, but I don't really want to go that route. Any help would be appreciated. Thank you
Turns out this was an appJar/packaging issue, the pyinstaller was not looking in the correct directory for the assets. per the dev of appJar, I commented out two lines of code in the appJar.py, lines 508-509:
if self.platform == self.WINDOWS:
self.topLevel.wm_iconbitmap(self.appJarIcon)
More on the specifics here: https://github.com/jarvisteach/appJar/issues/84
I probably can fix this by using the --path argument with pyinstaller but for the moment, the issue is fully resolved

Python: PIL - [Errno 32] Broken pipe when saving .png

What I'm trying to do here is save the contents of a Tkinter Canvas as a .png image using PIL.
This is my save function ('graph' is the canvas).
def SaveAs():
filename = tkFileDialog.asksaveasfilename(initialfile="Untitled Graph", parent=master)
graph.postscript(file=filename+".eps")
img = Image.open(filename+".eps")
img.save(filename+".png", "png")
But it's getting this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Users\Adam\Desktop\Graphing Calculator\Graphing Calculator.py", line 352, in SaveAs
img.save(filename+".png", "png")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1406, in save
self.load()
File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 283, in load
self.im = Ghostscript(self.tile, self.size, self.fp)
File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 72, in Ghostscript
gs.write(s)
IOError: [Errno 32] Broken pipe
I'm running this on Windows 7, Python 2.7.1.
How do I make this work?
oh I just get the same error. I have solve it now
just do the following after installing PIL and Ghostscript
1) Open C:\Python27\Lib\site-packages\PIL\EpsImagePlugin.py
2) Change code near line 50 so that it looks like this:
Build ghostscript command
command = ["gswin32c",
"-q", # quite mode
"-g%dx%d" % size, # set output geometry (pixels)
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
"-sDEVICE=ppmraw", # ppm driver
"-sOutputFile=%s" % file,# output file
"-"
]
Make sure that gswin32c.exe is in the PATH
good luck
It looks like the Ghostscript executable is erroring out and then closing the connection. Others have had this same problem on different OSes.
So, first I would recommend that you confirm that PIL is installed correctly--see the FAQ page for hints. Next, ensure that Ghostscript is installed and working. Lastly, ensure that Python can find Ghostscript, for example by running a PIL script that works elsewhere.
Oh, also--here are some tips on catching the broken pipe error so your program can be more resilient, recognize the problem, and warn the end-user. Hope that helps!
I have realized that while Python 2.7 has this EPEImagePulgin.py, Anaconda also has it. And unfortunately Anaconda's file is an older version. And unfortunately again, when you run your from Spyder environment it was picking up the epsimageplugin.py file from anaconda folder.
So I was getting similar broken pipe error.
When I went into python 2.7 directory and opened python console and then ran my code, it ran just fine.
Because the lates epsimageplugin.py file takes into consideration the windows environment and appropriate ghostscript exe files. Hope this helps.

Can't create games with pygame

I make a game with python 2.5 and pygame.
but,I can't complete make.
because this errors occured.
C:\Python26\TypeType\src\dist\Main.exe:8: RuntimeWarning: use font: MemoryLoadLibrary failed loading pygame\font.pyd
Traceback (most recent call last):
File "Main.py", line 8, in <module>
File "pygame\__init__.pyo", line 70, in __getattr__
NotImplementedError: font module not available
Traceback (most recent call last):
File "Main.py", line 8, in <module>
File "pygame\sysfont.pyo", line 253, in SysFont
RuntimeError: default font not found 'freesansbold.ttf'
Perhaps I think that the reason is because it used an object called sysfont. (Because I had the programs that did not use sysfont on an execute file and was able to start with the PC which was not installed Python in)
What's wrong .
sorry I'm begginer.
EDIT
I can find Python2.5\Lib\site-packages\pygame\freesansbold.ttf
but same error occured..
Where may I copy freesansbold.ttf
A simple solution would be to simply load the font directly instead of using sysfont. Just use the pygame.font.Font class and directly load a ttf file. This will also make it easier to use py2exe, and you can choose exactly the font you want.
It looks like you don't have the freesansbold.ttf file on your computer in an accessible fonts folder. This font should have come with Pygame in the lib directory.
Check the installation folder for your copy of Pygame, and if it's there, modify your Python installation's font path to include that directory. If not, you'll need to find a copy or download a newer version of Pygame.
You probably found a solution but i want to give one for other.
Create you game with pygame. Then, unzip the librairy file.
Go to
PythonX.X\Lib\site-packages\pygame and copy freesansbold.ttf
Back to your library file that you unzipped and go to
library\pygame
Put freesansbold.ttf in and zip library.
Now, it's work ! :)

Categories