Search code examples
windowspy2exepython

Use the python interpreter packaged with py2exe


I have a python script I want to distribute to Windows, where people might not have python installed. So I use py2exe. The problem is in the script I run other python scripts by using subprocess, which requires python interpreter as the program to execute. As I don't have python interpreter installed on Windows, is there any way I could ignore the interpreter and work around the problem? Is there any way I could call the python interpreter pakcaged by py2exe?


Solution

  • It's probably more simple than you think: Instead of starting sub-processes, use the built-in eval() command to execute the scripts.

    [EDIT] To redirect stdio, replace sys.stdout/sys.stderr with buffers or something else that supports "write()".

    To restore the original values, the sys module offers __stdout__, etc.

    [EDIT2] I haven't tried this but it might work: Add "python.exe" to the set of files which py2exe creates.

    From the main code, copy all files that py2exe created + the python.exe into a temporary directory. Then add all your scripts.

    Now start the new python interpreter with a small script that adds the temp folder and library.zip to the sys.path

    Note: Python doesn't have to be "installed" like a Windows application. In fact, you can simply copy all the files to a new place. As long as the search path is correct, this works.