15 Python scripts into one executable?

20,972

You can really use py2exe, it behaves the way you want.

See answer to the mentioned question: How would I combine multiple .py files into one .exe with Py2Exe

Usually, py2exe bundles your main script to exe file and all your dependent scripts (it parses your imports and finds all nescessary python files) to library zip file (pyc files only). Also it collects dependent DLL libraries and copies them to distribution directory so you can distribute whole directory and user can run exe file from this directory. The benefit is that you can have a large number of scripts - smaller exe files - to use one large library zip file and DLLs.

Alternatively, you can configure py2exe to bundle all your scripts and requirements to 1 standalone exe file. Exe file consists of main script, dependent python files and all DLLs. I am using these options in setup.py to accomplish this:

setup( 
  ...
  options = {         
    'py2exe' : {
        'compressed': 2, 
        'optimize': 2,
        'bundle_files': 1,
        'excludes': excludes}
        },                   
  zipfile=None, 
  console = ["your_main_script.py"],
  ...
)

Working code:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')
setup( 
  options = {         
    'py2exe' : {
        'compressed': 1, 
        'optimize': 2,
        'bundle_files': 3, #Options 1 & 2 do not work on a 64bit system
        'dist_dir': 'dist',  # Put .exe in dist/
        'xref': False,
        'skip_archive': False,
        'ascii': False,
        }
        },                   
  zipfile=None, 
  console = ['thisProject.py'],
)
Share:
20,972
Nefariis
Author by

Nefariis

Pragmatic developer and data enthusiast - I specialize in all things automation. I write in Python, Kotlin, JAVA, Typescript, Regex, and SQL to create custom applications that support the teams around me. For fun I wrestle alligators, battle rap in NY subways, and Netflix and chill with Tony Danza. Achievements: I've seen the internet in its entirety, my record is 14 marshmallows in chubby bunny, and - I know a girl.

Updated on December 14, 2020

Comments

  • Nefariis
    Nefariis over 3 years

    Ive been tinkering around all day with solutions from here and here:

    How would I combine multiple .py files into one .exe with Py2Exe

    Packaging multiple scripts in PyInstaller

    but Its not quite working the way I thought it might.

    I have a program that Ive been working on for the last 6 months and I just sourced out one of its features to another developer who did his work in Python.

    What I would like to do is use his scripts without making the user have to download and install python.

    The problem as I see it is that 1 python script calls the other 14 python scripts for various tasks.

    So what I'm asking is whats the best way to go about this?

    Is it possible to package 15 scripts and all their dependencies into 1 exe that I can call normally? or is there another way that I can package the initial script into an exe and that exe can call the .py scripts normally? or should I just say f' it and include a python installer with my setup file?

    This is for Python 2.7.6 btw

    And this is how the initial script calls the other scripts.

    import printSub as ps
    import arrayWorker as aw
    import arrayBuilder as ab
    import rootWorker as rw
    import validateData as vd
    etc...
    

    If this was you trying to incorporate these scripts, how would you go about it?

    Thanks

  • Frerich Raabe
    Frerich Raabe over 7 years
    On the other hand, the py2exe news page says that the last release was about eight years ago. Are you sure that PyInstaller won't cut it, since py2exe seems to be more or less dead.
  • Jesh Kundem
    Jesh Kundem over 7 years
    @Jiri For some reason your code is not working. I am usina wxpython module. Is there something I should be doing different?
  • Jiri
    Jiri over 7 years
    @JeshKundem Hard to say without more information. You can open new question with details and I'll try to answer it :)
  • picklepirate28
    picklepirate28 over 3 years
    I get IndexError: tuple index out of range. Maybe you know what could cause this?