Search code examples
pythonsubprocessexecutable

Problem in running an executable file in Python


I have a simple Python code (3.12 version). It just computes the sum of the ASCII's of a name entered by the user:

import sys
args = sys.argv
name=args[1]
print(f"Hello, {name}!")
print("Here is some intelligence that will return a number you will NEVER understand how it is 
 computed :-D \n")
name = name.lower() 
ascii_sum = 0

for char in name:
    if char.isalpha():
        ascii_value = ord(char) 
        ascii_sum += ascii_value

print(ascii_sum)

that I have transformed in an executable called dummy_fct.exe. Because for some reason I need to be able to call a (Python) executable from an ipynb notebook.

Now, I am trying to call this executable from a new notebook:

import subprocess
import sys
name = input("Quel est ton nom ? ")
subprocess.run(["dummy_fct.exe", name], capture_output=True, text=True)

But got the following error:

CompletedProcess(args=['dummy_fct.exe', 'PDH'], returncode=1, stdout='', stderr='[4004] Module object for pyimod02_importers is NULL!\nTraceback (most recent call last):\n File "PyInstaller\loader\pyimod02_importers.py", line 22, in \n File "pathlib.py", line 14, in \n File "urllib\parse.py", line 40, in \nModuleNotFoundError: No module named 'ipaddress'\nTraceback (most recent call last):\n File "PyInstaller\loader\pyiboot01_bootstrap.py", line 17, in \nModuleNotFoundError: No module named 'pyimod02_importers'\n[4004] Failed to execute script 'pyiboot01_bootstrap' due to unhandled exception!\n')

I do say that I do not understand at all this error message... Can you help me ?

EDIT: The executable was created running these two commands:

jupyter nbconvert --to script dummy_fct.ipynb
pyinstaller --onefile dummy_fct.py

Solution

  • This seems to be bug with pyinstaller(affecting Python >=3.11.4) which is reported here and it has fixed in v5.12. So the solution is to upgrade the pyinstaller version to >=5.12.

    pip install "pyinstaller>=5.12"