python cx-freeze executable and original python file look different - python

I made a python file, it uses the module tkinter.messagebox, I made it an executable file using cx-freeze , but the messagebox from the executable looks different from the messagebox made with the python code.
messagebox looks different
how do I make the executable's messagebox look like the messagebox made using the python code?
here is my cx-freeze script:
from cx_Freeze import setup, Executable
setup( name = "script",
version = "1.1",
description = "messages",
options = {"build_exe": {"packages": ["os"]}},
executables = [Executable("msg.py", base="win32gui")])
to make the exe, I typed python setup.py build in the command line.
also, please don't ask me to use pyinstaller because there's problems with it for me.

I used pyinstaller to make the executable instead, it worked finally.
I think it's something to do with the win32gui base. in pyinstaller, when i used the -w parameter, the messagebox also looks like the ugly one.

Related

How to make a Python exe file automatically install dependancies?

I have made an application using python using some libraries installed as needed on the go. Now I want to make it usable for person who doesn't know how to install dependencies and which ones are needed.
What to do to transform it into an easy to use application and probably make it able to run on Mac too??
Please suggest some resources that might help me know more.
PyInstaller might be something you are looking for, which can create .exe files from python scripts.
Here's the documentation. Be aware that the __import__() function with variable data isn't detected by PyInstaller, so you should just use import ..., and then PyInstaller will add the dependencies so that they are used in the .exe file.
As Wouter K mentioned, you should install Pyinstaller (pip install pyinstaller for pip) and then cd to the directory you want and type pyinstaller --onefile file.py on the terminal. If you cded in the directory your file is, you just type the name and the extension (.py) of your file. Else, you will have to specify the full path of the file. Also, you can't make an mac os executable from a non mac os pc. You will need to do what I mentioned above on a mac.
When making the executable you should declare the modules with it, you can then just copy the library files to the same location as the programme.
I use cx_freeze when making exe's and an example setup.py files looks like this:
from cx_Freeze import setup, Executable
base = None
executables = [Executable("projectname.py", base=base)]
packages = ["idna","os","shutil","csv","time","whatever else you like"]
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "Some Project",
options = options,
version = "1.2.3",
description = 'Generic Description',
executables = executables
)
Packages are where the modules are declared.
If you have a quick search online it'll give you complete guides for it, all you'd need to do then is copy the lot across to your friend's computer.
Hope this helps!

How can I convert pygame to exe?

Recently, I made a pygame game. I want to show it to my friends. But unfortunately, I cant make an executable file, only a .py file. I have tried to use tools such as py2exe which might not support Python 3.6 and pyinstaller. I think pyinstaller may be useful, and I can also make an exe successfully but it didn't work at all. It just showed it couldn't open the sound file which is already in the same file.
I have tried two ways to load files firstly, just load the path then the exe converted shows it can't open the file which is already in the same path with the exe.
Secondly, I use the os.path which the exe showed it cant open ...../temp/... which is a long path but my computer doesn't have this folder at all.
I am a new guy to learn python. I have searched for a day and can't make it work. Can you help me?
I'm just a scripting hack, and an idiot when it comes to command line tools. But I REALLY wanted to package up my python game into a Windows executable. I wrestled with pyinstaller for days, and read all of the threads of others who are also trying to figure it out, and I also read all the pyinstaller docs several times. No one solution worked for me, but after much trial and error I finally stumbled onto a recipe for success and want to share it in case there are other pygame developers out there banging their heads against this. The solution contains bits and pieces of several threads.
How to create a single file exe of a multi-file python pygame with several asset directories using pyinstaller on Windows:
First, install pyinstaller.
Open Windows Command Prompt, type:
pip install pyinstaller
Make a python game. Say the main game file is called main_game_script.py, located in
C:\Users\USERNAME\Documents\python\game_dir
Add this function to main_game_script.py. Make sure to import os and sys. (I never would have figured out on my own that I needed to add this function to my game, but it works, so thanks to whoever posted it in some other thread)
import sys
import os
def resource_path(relative_path):
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
To load images into game, call that function, like such:
asset_url = resource_path('assets/chars/hero.png')
hero_asset = pygame.image.load(asset_url)
When your game is ready to be wrapped in an EXE, open Windows Command Prompt. Go to the main game directory and type:
pyinstaller --onefile main_game_script.py --collect-data assets/chars --collect-data assets/tiles --collect-data assets/fonts
It'll look like this:
C:\Users\USERNAME\Documents\python\game_dir>pyinstaller --onefile main_game_script.py --collect-data assets/chars --collect-data assets/tiles --collect-data assets/fonts
This will create four important items:
- a main_game_script.spec file in the game_dir
- a 'build' dir in the game_dir
- a 'dist' directory in the game_dir
- a main_game_script.exe will be created in the 'dist' directory.
Running the command spits out a ton of warnings and logs. I don't know what it all means. But as long as the final output line says success, you're probably good.
If your exe is still not working: First, you must modify the .spec file (which now exists in the game_dir) to add the needed game asset paths to the exe:
Open the main_game_script.spec file in notepad or whatever.
Replace the empty datas[] list with asset directory paths like such (using tuples!):
datas=[('assets/chars/*','assets/chars'),('assets/tiles/*.png','assets/tiles'),('assets/fonts/*','assets/fonts')],
The first value of each tuple is the actual file names you want to import.
The second value is the relative path from you main_game_script.py
The syntax and paths must be perfect, or it won't work and may not throw an error
Save and close the 'main_game_script.spec' file. Return to your windows command prompt. Type:
pyinstaller main_game_script.spec
Like this:
C:\Users\USERNAME\Documents\python\game_dir>pyinstaller main_game_script.spec
This somehow embeds the asset dir paths into the exe that you've already built
After all this (which only takes a couple minutes after you've done it 100 times), you finally have a single file EXE of your python game that includes all of the game assets. If you've done everything right, double click on the EXE to play your python game.
Note: getting it right the first time took me a couple days. I started with a simple test_game_script.py with minimal code and iterated methodically, adding more asset paths and complexity with each iteration so I could isolate what was broken and why. If your game isn't converting to EXE properly, start with a simple 5 line game script with one asset and slowly build on it until you have a pipeline that works, THEN apply that functioning asset pipeline your ACTUAL game. Otherwise isolating the problem will be impossible.
1. Add --hidden-import pygame.
Something like:
python -m PyInstaller <options> xxx.py --hidden-import pygame
2. Hack the pygame stdout.
If you used the option -w, it will fail because of the (annoying) version output of pygame.
Workaround:
import contextlib
with contextlib.redirect_stdout(None):
import pygame
I too had the same problem and spent days looking through google. Today finally I realised what was the problem.
Before trying to convert it, remember to install Pygame via the Terminal as well. Just type in "pip install pygame"
This can be done with cx_freeze
pip install cx_freeze
You will need to create a setup.py file to help cx_freeze build the executable
import cx_Freeze
# base = "Win32GUI" allows your application to open without a console window
executables = [cx_Freeze.Executable('my_example_app.py', base = "Win32GUI")]
cx_Freeze.setup(
name = "My Example Exe App",
options = {"build_exe" :
{"packages" : ["pygame"], "include_files" : ['example_folder_with_files/', 'ExampleFont.ttf', 'example_text_file.txt']}},
executables = executables
)
Be sure to add your packages, fonts, or files to the script and any additional files that your app needs to the setup.
Finally you need to run your setup.py script
python setup.py build
or if you want cx_freeze to create an installer for you
python setup.py bdist_msi
Sentdex has a nice walkthrough on his website
pythonprogramming.net
#Blue Botic answer above works like a charm! Thank you!
I added the "--noconsole" parameter in the below command to get rid of the console and only show the game's main window:
pyinstaller --onefile --noconsole main_game_script.py
With PyInstaller it's possible without editing the .spech file, using the argument --collect-data NAME_ASSET_FOLDER
pyinstaller main_game_script.py --onefile --windowed --collect-data assets/chars --collect-data assets/tiles --collect-data assets/fonts

How to convert a pygame application with multiple modules to .exe

I have a pygame application which I would like to convert to .exe format.
Pygame2exe (https://pygame.org/wiki/Pygame2exe) works nicely EXCEPT I am unable to figure out how to do this conversion with a project that has more than one custom module.
For example:
python_project/
main.py
other.py
I need to compile both modules, and merging them is not an option.
I have been working at this problem for a few weeks, and have not found any solutions.
I know it is possible, because I had it working, and then formatted the only hard drive that had a copy of the code without realizing I had not made a backup.
EDIT
Thank you Michael.
I just had to make a small change to the setup.py file you provided to make it font compatible. Here is the full setup.py file
from distutils.core import setup
import py2exe
import os
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
if os.path.basename(pathname).lower() in ["sdl_ttf.dll"]:
return 0
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL
setup(windows=['main.py'])
(taken from Pygame font not working after py2exe)
Thanks again.
if you are using python 3.3 or 3.4, you can use py2exe.
install through pip, pip install py2exe in cmd
note: if 'pip' is not recognized as an internal or external command, navigate to the directory in which python is installed,/scripts probably: C:\python34\scripts
make a setup script called setup.py,
with just the three lines of code
from distutils.core import setup
import py2exe
setup(windows=['filename.pyw'] #probably pyw in windows, will be name of main file
then open command prompt in the directory where the main file and setup.py are located, and type setup.py py2exe
DO NOT USE or filename.pyw in the setup script, use the name of the main module.
this will make a folder called dist, (you can safely get rid of '__pycache_') containing all the files your exe needs to run!
you will probably want to make an installer, I would recommend Inno Setup (http://www.jrsoftware.org/isinfo.php)

cx_Freeze app cannot find tk.tcl

I would like to distribute a tkinter app by using cx_Freeze.
I can run the myapp.py from command line.
cx_Freeze creates a folder named "exe.macosx-10.6-x86_64-3.4", and the osx exe works from here.
The osx exe from within myapp.app yields the following error:
`tkinter.TclError: Can't find a usable tk.tcl in the following directories:`
....
This probably means that tk wasn't installed properly.
Here is my stripped-down setup.py
build_exe_options = {"modules": ["tkinter", "time", "requests", "threading", "os"]}
setup(name="myapp",
option={"build_exe": build_exe_options},
executables=[Executable("myapp.py",
base=base)]
)
Edit: I attempted this with python 2.7, and it works. The line "This probably means that tk wasn't installed properly" is likely correct.
I ran into the same problem on mac Yosemite. The system python is 2.7 and I'm using python3. I found that python2.7 build successfully with tkinter but 3.4 does not.
Haven't figured out how to make python3.4 work yet.

Converting a Pygame Game into an Exe [duplicate]

Recently, I made a pygame game. I want to show it to my friends. But unfortunately, I cant make an executable file, only a .py file. I have tried to use tools such as py2exe which might not support Python 3.6 and pyinstaller. I think pyinstaller may be useful, and I can also make an exe successfully but it didn't work at all. It just showed it couldn't open the sound file which is already in the same file.
I have tried two ways to load files firstly, just load the path then the exe converted shows it can't open the file which is already in the same path with the exe.
Secondly, I use the os.path which the exe showed it cant open ...../temp/... which is a long path but my computer doesn't have this folder at all.
I am a new guy to learn python. I have searched for a day and can't make it work. Can you help me?
I'm just a scripting hack, and an idiot when it comes to command line tools. But I REALLY wanted to package up my python game into a Windows executable. I wrestled with pyinstaller for days, and read all of the threads of others who are also trying to figure it out, and I also read all the pyinstaller docs several times. No one solution worked for me, but after much trial and error I finally stumbled onto a recipe for success and want to share it in case there are other pygame developers out there banging their heads against this. The solution contains bits and pieces of several threads.
How to create a single file exe of a multi-file python pygame with several asset directories using pyinstaller on Windows:
First, install pyinstaller.
Open Windows Command Prompt, type:
pip install pyinstaller
Make a python game. Say the main game file is called main_game_script.py, located in
C:\Users\USERNAME\Documents\python\game_dir
Add this function to main_game_script.py. Make sure to import os and sys. (I never would have figured out on my own that I needed to add this function to my game, but it works, so thanks to whoever posted it in some other thread)
import sys
import os
def resource_path(relative_path):
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
To load images into game, call that function, like such:
asset_url = resource_path('assets/chars/hero.png')
hero_asset = pygame.image.load(asset_url)
When your game is ready to be wrapped in an EXE, open Windows Command Prompt. Go to the main game directory and type:
pyinstaller --onefile main_game_script.py --collect-data assets/chars --collect-data assets/tiles --collect-data assets/fonts
It'll look like this:
C:\Users\USERNAME\Documents\python\game_dir>pyinstaller --onefile main_game_script.py --collect-data assets/chars --collect-data assets/tiles --collect-data assets/fonts
This will create four important items:
- a main_game_script.spec file in the game_dir
- a 'build' dir in the game_dir
- a 'dist' directory in the game_dir
- a main_game_script.exe will be created in the 'dist' directory.
Running the command spits out a ton of warnings and logs. I don't know what it all means. But as long as the final output line says success, you're probably good.
If your exe is still not working: First, you must modify the .spec file (which now exists in the game_dir) to add the needed game asset paths to the exe:
Open the main_game_script.spec file in notepad or whatever.
Replace the empty datas[] list with asset directory paths like such (using tuples!):
datas=[('assets/chars/*','assets/chars'),('assets/tiles/*.png','assets/tiles'),('assets/fonts/*','assets/fonts')],
The first value of each tuple is the actual file names you want to import.
The second value is the relative path from you main_game_script.py
The syntax and paths must be perfect, or it won't work and may not throw an error
Save and close the 'main_game_script.spec' file. Return to your windows command prompt. Type:
pyinstaller main_game_script.spec
Like this:
C:\Users\USERNAME\Documents\python\game_dir>pyinstaller main_game_script.spec
This somehow embeds the asset dir paths into the exe that you've already built
After all this (which only takes a couple minutes after you've done it 100 times), you finally have a single file EXE of your python game that includes all of the game assets. If you've done everything right, double click on the EXE to play your python game.
Note: getting it right the first time took me a couple days. I started with a simple test_game_script.py with minimal code and iterated methodically, adding more asset paths and complexity with each iteration so I could isolate what was broken and why. If your game isn't converting to EXE properly, start with a simple 5 line game script with one asset and slowly build on it until you have a pipeline that works, THEN apply that functioning asset pipeline your ACTUAL game. Otherwise isolating the problem will be impossible.
1. Add --hidden-import pygame.
Something like:
python -m PyInstaller <options> xxx.py --hidden-import pygame
2. Hack the pygame stdout.
If you used the option -w, it will fail because of the (annoying) version output of pygame.
Workaround:
import contextlib
with contextlib.redirect_stdout(None):
import pygame
I too had the same problem and spent days looking through google. Today finally I realised what was the problem.
Before trying to convert it, remember to install Pygame via the Terminal as well. Just type in "pip install pygame"
This can be done with cx_freeze
pip install cx_freeze
You will need to create a setup.py file to help cx_freeze build the executable
import cx_Freeze
# base = "Win32GUI" allows your application to open without a console window
executables = [cx_Freeze.Executable('my_example_app.py', base = "Win32GUI")]
cx_Freeze.setup(
name = "My Example Exe App",
options = {"build_exe" :
{"packages" : ["pygame"], "include_files" : ['example_folder_with_files/', 'ExampleFont.ttf', 'example_text_file.txt']}},
executables = executables
)
Be sure to add your packages, fonts, or files to the script and any additional files that your app needs to the setup.
Finally you need to run your setup.py script
python setup.py build
or if you want cx_freeze to create an installer for you
python setup.py bdist_msi
Sentdex has a nice walkthrough on his website
pythonprogramming.net
#Blue Botic answer above works like a charm! Thank you!
I added the "--noconsole" parameter in the below command to get rid of the console and only show the game's main window:
pyinstaller --onefile --noconsole main_game_script.py
With PyInstaller it's possible without editing the .spech file, using the argument --collect-data NAME_ASSET_FOLDER
pyinstaller main_game_script.py --onefile --windowed --collect-data assets/chars --collect-data assets/tiles --collect-data assets/fonts

Categories

Resources