Where to put images folder in python exe? - python

I have converted a python game I designed into an exe. Running the exe itself causes it to flash and then close, meaning an error has occured. Running it from the Command Prompt causes the error as well, but documents it:
Cannot load image: Playfield.png
Couldn't open images\Playfield.png
This is telling me that the load_image block is failing. I have encountered this before when I did not have an images directory.
I attempted to move the images folder to the dist directory. This is the error that shows up:
Traceback (most recent call last):
File "Table_Wars.py", line 728, in <module>
File "Table_Wars.py", line 51, in main
File "Table_Wars.py", line 236, in __init__
File "pygame\__init__.pyc", line 70, in __getattr__
NotImplementedError: font module not available
(ImportError: DLL load failed: The specified module could not be found.)
This is my first time with py2exe, so I'm not really sure what is happening. The raw python file itself, Table_Wars.py, runs as expected.
If it helps, the location for the entire Table_Wars folder is inside a folder called Games, located on my Desktop (C:\Users\Oventoaster\Desktop\Games\Table_Wars). I am running Windows 7 32 bit OS.
On request, here is the output.txt I have generated:
Folder PATH listing for volume OS
Volume serial number is 7659-4C9C
C:\USERS\OVENTOASTER\DESKTOP\GAMES\TABLE_WARS
build
bdist.win32
winexe
bundle-2.7
collect-2.7
ctypes
distutils
email
mime
encodings
logging
multiprocessing
dummy
pygame
threads
unittest
xml
parsers
temp
dist
images
Here is the setup.py I used to convert the file:
from distutils.core import setup
import py2exe
setup(console=['Table_Wars.py'])
EDIT: I have attempted to use the full py2exe example. This will create the exe, but gives the same Cannot load image error. Attempting to put the images folder in the same folder as the exe creates a Runtime Error: The application requested the runtime to terminate it in an unusual way.
The shortened form of the code Slace Diamond suggested prevents py2exe from finding Table_Wars.py:
from cmd:
running py2exe
*** searching for required modules ***
error: Table_Wars.py: No such file or directory.
setup and Table_Wars are in the same directory. If it help, I input the full path to python.exe and setup.py.
EDIT: I seem to be getting closer. I put the images directory within self.extra_datas, and now I am getting this:
Fatal Python error: (segmentation fault)
This application has requested the runtime to terminate it in an unusual way. Please contact the application's suppourt team for more information

When you build a distributable package with py2exe (and py2app for that matter), part of the package environment is to point to a local resource location for files. In your plain unpackaged version, you are referring to a relative "images/" location. For the packaged version, you need to configure your setup.py to include the resources in its own location.
Refer to this doc for very specific info about how to set the data_files option of your package: http://www.py2exe.org/index.cgi/data_files
That page has multiple examples to show both very simple paths, and also a helper function for finding the data and building the data_files list for you.
Here is an example of the simple snippet:
from distutils.core import setup
import py2exe
Mydata_files = [('images', ['c:/path/to/image/image.png'])]
setup(
console=['trypyglet.py.py']
data_files = Mydata_files
options={
"py2exe":{
"unbuffered": True,
"optimize": 2,
"excludes": ["email"]
}
}
)
This closely matches what you are trying to achieve. It is saying that the "image.png" source file should be placed into the "images" directory at the root of the resources location inside the package. This resource root will be your current directory from your python scripts, so you can continue to refer to it as a relative sub directory.

It looks like you've already fixed the image problem by moving the folder into dist. The missing font module, on the other hand, is a known problem between pygame and py2exe. Py2exe doesn't copy some necessary DLLs, so you have to override py2exe's isSystemDLL method, forcing it to include audio and font related DLLs.
If Table_Wars.py is the only module in your project, try running this script with python setup.py py2exe:
from os.path import basename
from distutils.core import setup
import py2exe
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
if basename(pathname).lower() in ("libogg-0.dll", "sdl_ttf.dll"):
return 0
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL
setup(windows=[{"script": "Table_Wars.py"}],
options={"py2exe": {"dist_dir": "dist"}})
You could also try the example py2exe setup file on the pygame wiki. If neither of them are working, please add the error messages to your question.
I tried running py2exe on a sample project, and it also breaks for me when I use the default pygame font. If you're using the default font, try putting a ttf file in the root of your project and also in the dist folder. You'll have to change the call to pygame.Font in your script as well:
font = pygame.font.Font("SomeFont.ttf", 28)

Related

Use my own python libraries in visual studio code on raspberry pi

I have a directory that contains sub directories of code that I reuse.
MyBaseDirectory
\genericcodedir1
reuse1.py
\simpleapp1
app1.py
app1.py has the following line
import reuse1
Visual studio will fail to run this since it says it can't find the library.
On windows I simply added the genericcodedir1 to the PYTHONPATH environment variable and all is well.
What should I do on the raspberry pi to allow this to run?
error message:
Exception has occurred: ModuleNotFoundError
No module named 'reuse1'
File "/home/pi/Desktop/Mybasedirectory/simpleapp1/app1.py", line 5, in <module>
import reuse1
I assume you have file structure like this and you open Test folder in VS Code As follows.
You can specify the path by adding the following code above the import statement in app.py:
import sys
sys.path.append("./genericcodedir1")
import reuse1
In addition, you can add the following configuration to the settings.json file to make vscode recognize reuse1.
{
"python.analysis.extraPaths": [
"./genericcodedir1"
]
}
code and result
so if your files looks like :
|_genericcodedir
|_reuse1.py
|_simpleapp1
|_app1.py
you need to add an empty file called __init__.py in your genericcodedir.
another note worthy thing is your working directory (the directory in which your terminal runs)
you may need to append to os path depending on where you are when launching the program

I'm using py2app to make my pygame executable but it keeps giving me an error that the file or directory isn't found [duplicate]

This question already has an answer here:
py2app-installed app using pygame fails
(1 answer)
Closed 1 year ago.
Title, I just finished my first pygame project which Is a very simple game with a main menu module and a main game module, they both run perfectly when I run them from sublime.
I tried using py2app to share this game with some friends, after installing py2app this is what I did in my terminal:
py2applet --make-setup MainMenu.py # (this module imports the main game module)
then I edited my setup.py file to include all my python files like images, sounds and fonts (this is the setup.py file):
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['MainMenu.py']
DATA_FILES = ['MySprite/Shoot/1.png','MySprite/Shoot/2.png',
'MySprite/Go/1.png','MySprite/Go/2.png','MySprite/Go/3.png','MySprite/Go/4.png','MySprite/Go/5.png','MySprite/Go/6.png','MySprite/Go/7.png','MySprite/Go/8.png',
'go_1.png','go_2.png','go_3.png','go_4.png','go_5.png','go_6.png','go_7.png','go_8.png','go_9.png','go_10.png','go_1L.png','go_2L.png','go_3L.png','go_4L.png','go_5L.png','go_6L.png','go_7L.png','go_8L.png','go_9L.png','go_10L.png','HomeScreen.png','icon.png','Bullet.png','SkyNight.png','GamePlatform.png','Heart.png','Starjedi.ttf','Chernobyl.ttf',
'MainMenu.wav','Fireball.wav','background.wav','Jump.wav','Zombie.wav','Hit.wav']
OPTIONS = {}
setup(
app = APP,
data_files = DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Then back to my terminal:
rm -rf build dist
then:
python setup.py py2app -A
which gave me an error so I changed it to python3 and it created a "build" and a "dist" folders, I tried opening the app inside the "dist" folder but it gave me an error and asked me to terminate or open the console, I right clicked on it and pressed show package options --> contents --> MacOs and pressed the file which opened another terminal with the error:
screen_home = pygame.image.load('HomeScreen.png')
FileNotFoundError: No such file or directory.
Both modules run perfectly from sublime, Im out of ideas and I tried googling the problem and I can't find a solution, Im new at this so I apologize if the solution was obvious or it was a dumb question and id appreciate any help.
I don't have a real answer but a few things to try and check:
We need to review a few things in the two lines, sometimes the setup.py requires to have the full path where each file is located, it is the right way, otherwise the setup.py will not find where are those images and it won't be able to include them into the final package, Are all those files in the same folder?, even if they are, try to add the full path for each file (jbsidis should be replaced by your username):
import os
APP = ['/home/jbsidis/Escritorio/MainMenu.py']
if os.path.isfile(APP[0])==True:
print("the MainMenu.py file it does exists")
DATA_FILES = ['/home/jbsidis/Escritorio/MySprite/Shoot/1.png','/home/jbsidis/Escritorio/MySprite/Shoot/2.png','/home/jbsidis/Escritorio/MySprite/Go/1.png','/home/jbsidis/Escritorio/MySprite/Go/2.png','/home/jbsidis/Escritorio/MySprite/Go/3.png','/home/jbsidis/Escritorio/MySprite/Go/4.png','/home/jbsidis/Escritorio/MySprite/Go/5.png','/home/jbsidis/Escritorio/MySprite/Go/6.png','/home/jbsidis/Escritorio/MySprite/Go/7.png','/home/jbsidis/Escritorio/MySprite/Go/8.png','/home/jbsidis/Escritorio/go_1.png','/home/jbsidis/Escritorio/go_2.png','/home/jbsidis/Escritorio/go_3.png','/home/jbsidis/Escritorio/go_4.png','/home/jbsidis/Escritorio/go_5.png','/home/jbsidis/Escritorio/go_6.png','/home/jbsidis/Escritorio/go_7.png','/home/jbsidis/Escritorio/go_8.png','/home/jbsidis/Escritorio/go_9.png','/home/jbsidis/Escritorio/go_10.png','/home/jbsidis/Escritorio/go_1L.png','/home/jbsidis/Escritorio/go_2L.png','/home/jbsidis/Escritorio/go_3L.png','/home/jbsidis/Escritorio/go_4L.png','/home/jbsidis/Escritorio/go_5L.png','/home/jbsidis/Escritorio/go_6L.png','/home/jbsidis/Escritorio/go_7L.png','/home/jbsidis/Escritorio/go_8L.png','/home/jbsidis/Escritorio/go_9L.png','/home/jbsidis/Escritorio/go_10L.png','/home/jbsidis/Escritorio/HomeScreen.png','/home/jbsidis/Escritorio/icon.png','/home/jbsidis/Escritorio/Bullet.png','/home/jbsidis/Escritorio/SkyNight.png','/home/jbsidis/Escritorio/GamePlatform.png','/home/jbsidis/Escritorio/Heart.png','/home/jbsidis/Escritorio/Starjedi.ttf','/home/jbsidis/Escritorio/Chernobyl.ttf','/home/jbsidis/Escritorio/MainMenu.wav','/home/jbsidis/Escritorio/Fireball.wav','/home/jbsidis/Escritorio/background.wav','/home/jbsidis/Escritorio/Jump.wav','/home/jbsidis/Escritorio/Zombie.wav','/home/jbsidis/Escritorio/Hit.wav']
for x in DATA_FILES:
if os.path.isfile(x)==True:
print("the "+str(x)+" file it does exists")
else:
print("the "+str(x)+" DOES NOT Exists in that location and that's why you got the error with your setup.py")
Here is the image in my case, my folders don't have those files, so the result is:

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()

Python py2exe Problems?

I coded a python application with a GUI in Tkinter with pictures. Now that I have finished, I am trying to convert it to a .exe with py2exe. All my pictures are in the same folder as my python file and setup file, but when I try to convert my python file via the Command Prompt, I get error messages saying that my .ico file is not defined and that it can not copy it. I think the problem is due to my setup.py file. How do I allow my images to be copied into the new .exe executable file without getting errors I have never used py2exe before.
Setup file:
from distutils.core import setup
import py2exe
setup(console=['Gui.py'])
Error:
How do I fix this?
You may have just forgotten to define your icon:
cfg = {
'py2exe.icon':'icon.ico',
...
}
Try to change your setup.py like this:
from distutils.core import setup
import py2exe
data_files = [('', [r'standardicon.ico'])]
setup(
windows =['Gui.py'],
data_files = data_files
)

py2exe can't find .py

Sorry if this is a really stupid question,
How do I get the py2exe to find the module I wish to convert?
I keep getting this error:
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'dist_dir'
warnings.warn(msg)
running py2exe
creating C:\Users\David\workspace\setup\src\dist
*** searching for required modules ***
error: Pygame.py: No such file or directory
I looked up as many Pygame to .exe tutorials or problems others have had, but their problem seemed to occur AFTER they created a .exe file...
I can't even get it to create one.
I used the exact same code as the one found on http://pygame.org/wiki/Pygame2exe except that I changed:
class BuildeExe:
def __init__(self):
#Name of starting .py
self.script = "MyApps.py"
into
class BuildExe:
def __init__(self):
#Name of starting .py
self.script = "Pygame.py"
My Pygame.py exists in "C:\Users\"username"\workspace\pygame\src". Please help.
you can try putting this in a .py file:
from distutils.core import setup
import py2exe
setup(console=['hello.py'])
such as how they do in this tutorial. Then all you have to do is oppen up cmd.exe and cd to your "C:\Users\"username"\workspace\pygame\src" and type:
python setup.py py2exe
or you can changed the setup.py to whatever .py file you saved the above code in. (within the same directory as you gave)

Categories

Resources