0
votes

I am struggling with my implementation of subprocess when created exe with pyInstaller.

I have a program that has a gui (tkinter) where multiple buttons have their subprocess.popen(). there is a command over ssh sent to a network device which needs some time to finish.

In pyCharm this works smooth. When I create the exe with pyInstaller, I get the following error:

Exception in Tkinter callback
Traceback (most recent call last): 
  File "tkinter\__init__.py", line 1699, in __call__
  File "gui.py" in line 197 in wrapper
  File "subprocess.py", line 707, in __init__
  File "subprocess.py", line 990, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
key not found: process1

The corresponding code snipped of gui.py line 197 is:

proc = subprocess.Popen(['python', r'commands/pec_cmd.py', 
       str(cmd), pec_ip, '1', '0', '0', '0', '0'], 
       startupinfo=subprocess.STARTUPINFO(), env=os.environ, 
       stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

later on I store the process in a dict, which is initialized earlier with subproccesses = {}:

subprocesses['process' + str(i)] = proc

Any idea or help why I get this FileNotFoundError? When run from PyCharm it works, but as soon as the exe is created (--onedir and --onefile tried), it fails

2

2 Answers

0
votes

You need to identify which file isn't found python.exe or commands/pec_cmd.py.

You could try to run the subprocess within the current shell: subprocess.Popen(... shell=True) or use full paths for subprocess

subprocess.Popen(['c:/python/python.exe', r'c:/whateverPath/commands/pec_cmd.py'])

But you still depend on a pre-installed Python interpreter:
Imagine you want to run your application/exe another computer, which hasn't Python installed the subprocesses will fail or you will always need to install Python.

1
votes

Thanks @Maurice Meyer. I was not afraid of the fact, that then still python needs to be installed. So I changed it to call a exe: Instead of calling subprocess.popen("python", commands/pec_cmd.py", ...) it is now working when calling subprocess.popen("pec_cmd.exe", ...) directly with the exe instead of the python with the script pec_cmd.py.