Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A single executable file with Py2Exe

I have been trying to make a single executable file and I am getting close. Please do not recommend that I use PyInstaller -- I have tried that route, asked on SO here, and have put in tickets. It is close but not quite working. I am now trying py2exe and am also very close. In pyinstaller, I am able to create resource files (which builds the executable with the files included -- I can then access these in the temporary folder).

I want to do the same for py2exe. I have a single executable, but five extra folders (maps, mpl-data, data, pics and tcl). I have seen this question but can't seem to understand it, nor get it to work. In my main py file, I am using PersistentDict(filepath) which is where I need the path to the file.

My question is two parts: 1. How do I get the files (data files below) packaged into the executable. 2. How do I access these files in my code and return their path (as a string) such as /temp/file1.jpg.

Here is my code for my py2exe setup file -- note that I have matplotlib and must include the mpl-data correctly in my executable. Thanks!

from distutils.core import setup 
import py2exe 
import shutil
import glob 
import matplotlib,six

opts = {'py2exe': { "includes" : ["matplotlib.backends",
                                  "matplotlib.backends.backend_qt4agg",
                                  "matplotlib.figure","numpy",
                                  "six",
                                  "mpl_toolkits.basemap", 
                                  "matplotlib.backends.backend_tkagg"], 
                     'excludes': ['_gtkagg', '_tkagg','_agg2','_cairo',
                                  '_cocoaagg', '_fltkagg', '_gtk', '_gtkcairo', 'tcl' ], 
                 'dll_excludes': ['libgdk-win32-2.0-0.dll','w9xpopen.exe', 
                                 'libgobject-2.0-0.dll'],
                    'bundle_files': 1,
                    'dist_dir': "Dist Folder",
                    'compressed': True,
              } 
       }

data_files = [(r'mpl-data', glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\*.*')),
              (r'mpl-data', [r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']), 
              (r'mpl-data\images',glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\images\*.*')), 
              (r'mpl-data\fonts',glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts\*.*')),
              (r'mpl-data\data', glob.glob(r'C:\Python27\Lib\site-packages\matplotlib\mpl-data\data\*.*')),
              ('data', ['C:\\Users\\Me\\Documents\\Example_Json_File.json']),
              ('pics', ['C:\\Users\\Me\\Documents\\Example_Icon.ico',
                        'C:\\Users\\Me\\Documents\\Example_Jpg.jpg',
                        ])]


setup(windows=[{"script" : "MyMainScript.py",
                "data_files" : data_files,
                "icon_resources": [(1, 'C:\\Users\\Me\\Documents\\Example_Icon.ico')]}, ],

                version = "1.0", 
                options=opts,
                data_files=data_files,
                zipfile = None,
      ) 
like image 812
mcfly Avatar asked Nov 08 '13 18:11

mcfly


People also ask

What is a single executable file?

An executable file (EXE file) is a computer file that contains an encoded sequence of instructions that the system can execute directly when the user clicks the file icon.

Which is better py2exe or PyInstaller?

In PyInstaller it is easy to create one exe, By default both create a bunch of exes & dlls. In py2exe its easier to embed manifest file in exe, useful for run as administrator mode in windows vista and beyond. Pyinstaller is modular and has a feature of hooks to include files in the build that you like.

Can you create an EXE with Python?

Underneath the GUI is PyInstaller, a terminal based application to create Python executables for Windows, Mac and Linux. Veteran Pythonistas will be familiar with how PyInstaller works, but with auto-py-to-exe any user can easily create a single Python executable for their system.


1 Answers

Guy here explains how to package to one file with py2exe. He's setup doesn't package resources inside the executable either.

When I package my apps, I don't use one executable option

options = {"py2exe": {'bundle_files': 1, 'compressed': True}},

not even bothered to put them in library.zip via

options = {"py2exe": {"skip_archive":0}}

Just have a number of pyc's, data files, dlls etc in one dir. Then create an installer using NSIS or Inno setup. As some of my apps have to run as services, Inno was taking care of that. The biggest plus of that approach, you don't have to deal with "frozen" paths to your files, that are different from your original paths.

Otherwise you might need to alter your code to detect frozen paths, e.g. http://www.py2exe.org/index.cgi/WhereAmI

like image 122
Dmitriy Avatar answered Sep 21 '22 13:09

Dmitriy