Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bundle .py files launched with execfile() on py2exe?

I'm developing a small tool on Python which basically launches a set of scripts on a folder. I need to package this on a stand-alone binary and I'm using py2exe for it.

My current code use os.path.listdir() to get all the .py files on a folder, and then launch some of them using execfile() funcion based on user input on a PyQT interface.

My code works as expected if executed through the main Python file, but fails when compiled with py2exe. The exception is:

IOError: [Errno 2] No such file or directory

for the python files launched with execfile().

I'm currently bundling with "bundle_files": 1 and zipfile = None. I tried to include these files messing with includes and packages but without luck. Can you help me to configure py2exe properly?

This is my current setup.py:

from distutils.core import setup
import py2exe
import os

#Python modules excluded from binary file
mod_excludes = [
    "Tkinter",
    "doctest",
    "unittest",
    "pydoc",
    "pygments",
    "pdb",
    "email",
    "_ssl",
    "difflib",
    "inspect"
]

#Avoid adding this dependencies
dll_excludes = [
    "MSVCP90.dll",
    "w9xpopen.exe"
]

#Force to exe
mod_includes = [
    "sip"
]

package_includes = [
    "app.payloads"
]



py2exe_options = {
    "optimize": 2,  # 0 (None), 1 (-O), 2 (-OO)
    "includes": mod_includes,
    "excludes": mod_excludes,
    "dll_excludes": dll_excludes,
    "packages": package_includes,
    #"xref": False,
    "bundle_files": 1,
    "compressed": True
    #"dist_dir": dist_dir
}

#TODO generar automaticamente la interfaz

setup(
    windows=[{"script": "app.py",
        "icon_resources": [(1, "app/gui/Res/app.ico")],
        "uac_info": "requireAdministrator"}],
    data_files=exe_files,
    options={"py2exe": py2exe_options},
    zipfile=None
    )

And I'm getting the following traceback:

Traceback (most recent call last):
  File "app\gui\ui.pyo", line 22, in call_report
  File "app\core\core.pyo", line 32, in generate_report
  File "app\core\core.pyo", line 18, in launch_payload
IOError: [Errno 2] No such file or directory: 'C:\\Users\\my_user\\path\\to\\app\\dist\\app.exe\\app\\payloads\\autoruns.py'
like image 801
lithiium Avatar asked Oct 21 '22 10:10

lithiium


1 Answers

Py2exe only includes *.pyc files (or .pyo files if you use "optimize" greater than 0 as you do). Since you error message mentions a non existing *.py file:

IOError: [Errno 2] No such file or directory: 'C:\Users\my_user\path\to\app\dist\app.exe\app\payloads\autoruns.py'

, this might be the reason.

In general, I would recommend not to use execfile(). Rather write your own package. Py2exe will include this package automatically if you import it somewhere in your application code. This package should contain the files you want to load dynamically. You can use this code:

my_module = __import__('my_package.module_name')

The string 'module_name' can come from the user input through the GUI.

like image 128
Mike Müller Avatar answered Oct 24 '22 03:10

Mike Müller