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

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)

Related

Compiling a non-standard module into an executable file with PyInstaller

Background: I have had success in the past installing and using Pyinstaller to transform my python projects into one-file executables. I don't think it is an issue with my source code or pyinstaller files.
Problem: I used a free, open-source library/module called easygui imported into my source code to build an application. The application works perfectly run natively or through the Python IDE. I am pretty sure the problem is that Pyinstaller is not finding the EasyGUI module to import (it automatically includes and compiles any libraries you import in the script).
Actions: My python folder is not in the C:\ drive, it is in the E:\ drive. I'm able to access the pyinstaller path in "E:\program files\python" but it is not reading the easygui library, I don't think. I installed pyinstaller and easygui using pip.
Reading a LOT of pyinstaller's documentation i tried to run it to include a paths dir like:
E:\Program Files Hard Disk\Python\Scripts>pyinstaller --paths
DIR "E:\Program Files Hard Disk\Python" --onefile "E:\Program Files\Python"
It does output the single executable in the build file but does not launch correctly. From what I can see in the console window the brief moment it's up, it looks like an easygui issue. Here is the result of attempting to launch the executable from the command-line:
Here is the compiling in the command window:
Please help

Terminal says "PyObjCTools" doesn't exist when it does

I am attempting to build a Python application on OSX 10.10, I have used MacPorts to install all dependencies and have used PyCharm to write the application. When I run it from PyCharm everything works fine, but when I try to run the build script, when it comes to the py2app part it tells me that the directory ".../PyObjC/PyObjCTools" isn't there, but I can navigate to it in Finder and it's very clearly there. Does anyone have any idea what my problem could be? I've been running around in circles with this for a week now.
If it's access to the PyObjCTools functionality you want, you should import it the Python way, for example:
from PyObjCTools import AppHelper
If you need to access actual files, it's possible to force py2app to add files using the setup data_files options. For example:
from setuptools import setup
setup(
app=['main.py'],
name="App Name",
data_files=['/path/to/folder'],
setup_requires=['py2app'],
options=dict(py2app=dict(iconfile='en.lproj/icon.icns',
includes=['includename'],
packages=['packagename']
)
)
)
After a rebuild the files are located inside the Cocoa bundle Resources folder, which you can find using the resourcePath() function:
NSBundle.mainBundle().resourcePath()

python 3.4 py2exe is not working or doing anything

I am trying to run a python file that I create as an executable or .exe in windows. I want to run this code in another computer that does not have nor need python, or pyserial or PIL or any other software, My code creates a Gui that do stuff
I am using a method to do this but it is not working
first I create a new python file, called setup.py, inside I put this code
from distutils.core import setup
import py2exe
setup(console=['mycode.py'])
and mycode.py is where my code creates a GUI that do stuff
of course I have downloaded the py2exe application by typing in the command prompt
pip install py2exe
then in the command prompt I change the directory where my setup.py and mycode.py are and run this line
python setup.py py2exe
and effectively it do creates 2 folders, one with pycache (with one archive mycode.cpython-34.pyc) and another one with a lot of archives and mycode.exe
then I take these to folders from my computer to another computer where python, pyserial, PIL are not installed, and I try to run mycode.exe and it's not working. Could someone explain why?
I have found the problem, there is no problem with py2exe but with mycode
in mycode I use some images from my computer so when I create the .exe it takes those images from the directory in my computer and runs
but when I take the .exe from my computer to another computer to run it, I do not take those images and therefore the .exe does not run
now the problem is how to call these images, which is another question

Bundle python tool in Cocoa app with easy_install

My Cocoa app requires a Python command line tool to be installed on the user's system using easy_install. Ideally, I'd want to bundle one bash file with my app which I can then run. But as far as I know this is not possible, because packages are installed in the "site-packages" directory of Python.
Is there a way to create a "package" of those files?
If not, how should I run the easy_install installation? I wanted to bundle a .pkg file with my app which I can then open if necessary, but I wasn't able to let this installation package run a script only.
If you have ideas on how to fix this, I'd be glad.
Kind regards,
Fabian
If you can ship the command line tool with your application, and if only your application will be using it (instead of the tool being used directly by the user), you can directly include and use the command line tool with your application like so:
Store the command line tool somewhere in your application directory.
Set the Python module search path so that it looks for the module you need.
Setting the Python module search path relative to a Python program can be done with
import sys
import os.path as path
sys.path.append(path.join(path.dirname(__file__),
'<relative path between this program and the command line tool module>'))
import <command line tool module>
The relative path can be written with the .. parent directory convention: this works both on Unix (including Mac OS X) and Windows.
PS: If many programs need to access the command line tool module, you can:
Either put the above code in each of your programs,
or, if you want something easier to maintain, you can create your own module my_runner and use it as a proxy: your module my_runner would import all the original runner functions like so:
import sys
import os.path as path
sys.path.append(path.join(path.dirname(__file__),
'<relative path between this program and the original ino module>'))
from ino.runner import *
You can then use the original runner module in all your programs through your proxy, by simply doing "normal" imports of your own my_runnermodule:
from my_runner import main
PPS: If you want a more robust path that works even if the working directory later changes, it is possible to convert a potentially local path to an absolute path with: path.join(path.abspath(path.dirname(__file__)),….
Thanks to EOL I got it working now. The solution involved several steps listed below.
Remove dependencies by packaging the .egg files and referencing them like this:
import sys
import os.path as path
sys.path.append(path.join(path.dirname(__file__), '<relative path to .egg file>'))
from dependency import something
Build a single .egg file from the now independent python module using setup.py
Reference the resulting .egg file in the command line tool like in 1.
Bundle the single .egg file and the command line tool in the Cocoa app
I hope this will be useful to someone.

py2exe problems

c:\python26\setup.py py2exe
Trying to run py2exe and when I get to command prompt I run the line above. However as opposed to converting my file it try's to open it. What am I doing wrong?
You must create your own setup.py and then run it with py2exe:
c:\my_python_scripts>python setup.py py2exe
In your setup.py you import distutils, py2exe and show names of your scripts to compile. There is template for it. Then I usually create .bat file which compiles my scripts.
Have you read py2exe tutorial?

Categories

Resources