Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyInstaller: launch another Python process with subprocess

How do I launch another Python process with subprocess from within a PyInstaller executable?

That is, I have a Python script that I compile with PyInstaller to an executable. Inside this Python script, at some point I need to launch another Python process with subprocess (I have a good reason for using subprocess instead of multiprocessing here, namely because it seems to be the only way to open the new process in a new console window, with creationflags=CREATE_NEW_CONSOLE_WINDOW on Windows):

subprocess.run([sys.executable, "-m", "some.module", ...])

However this approach doesn't work, because when the PyInstaller-compliled executable runs, sys.executable is no longer a path to the Python interpreter, but rather it's the path to the executable itself. I don't want to use just "python" either because I want to make sure it's using the exact same Python interpreter that is used to run the main program.


Another reason for using subprocess is that if I find out a way to start a Python process by explicitly calling the interpreter as above (with the argument list [sys.executable, ...]), I will be able to use the async version of subprocesses, asyncio.subprocess, which has an API very similar to subprocess. There doesn't seem to be an asyncio equivalent of multiprocessing.

like image 501
Anakhand Avatar asked Aug 13 '26 07:08

Anakhand


1 Answers

You can copy over python.exe from your local installation and then use that as the entrypoint for subprocess.run. The executable should see everything the one from PyInstaller does.

As an example this file

import subprocess
import sys
from pathlib import Path

python_exe = Path(sys.executable).with_stem("python")
subprocess.run([str(python_exe), "-m", "__hello__"])

Works as expected after the file is copied:

(.venv) PS dir> pyinstaller .\main.py
...
(.venv) PS dir> Copy-Item "$env:LOCALAPPDATA\Programs\Python\Python310\python.exe" "dist/main"
(.venv) PS dir> .\dist\main\main.exe
Hello world!
like image 114
Numerlor Avatar answered Aug 16 '26 01:08

Numerlor