Python subproccess Popen python executable not found

1.7k Views Asked by At
from datetime import datetime
from shutil import copyfile
import shutil
import subprocess
import os
import sys

import os
from distutils.dir_util import copy_tree
from datetime import datetime

# Getting the current date and time
dt = datetime.now()

# getting the timestamp
ts = datetime.timestamp(dt)

folders = os.listdir("../..")

os.mkdir("../../backup/"+str(ts))


for folder in folders:
    if folder.lower() != "backup":
        copy_tree("../../"+folder, "../../backup/"+str(ts)+"/"+folder)

#shutil.make_archive("../../backup/"+str(ts), 'zip', "../../backup/"+str(ts))
#os.remove("../../backup/"+str(ts))

ui_main_dir = os.path.abspath("../../ui")
py_from_ui_dir = os.path.abspath("../compiled_ui")

#2. Compile all ui files and qrc file with pyuic5 command.
def parse_directory_2(path):
    path_contents = os.listdir(path)
    if os.path.exists(path.replace(ui_main_dir,py_from_ui_dir))==False:
        os.mkdir(path.replace(ui_main_dir,py_from_ui_dir))
    for path_content in path_contents:
        if os.path.isdir(path+"/"+path_content):
            parse_directory_2(path+"/"+path_content)
        else:
            original_path = path+"/"+path_content
            extension = original_path.split(".")[-1].lower()
            if extension == "ui":
                saved_path = original_path.replace(".ui",".py").replace(ui_main_dir,py_from_ui_dir)
                process = subprocess.Popen("python -m PyQt5.uic.pyuic -x \""+original_path+"\" -o \""+saved_path+"\"", shell=False)   
                process.wait()
                folder_depth = saved_path.count("/")-1
                folders_back = ""
                for i in range(0,folder_depth):
                    folders_back +="../"
                py_file = open(saved_path,"r+",encoding="utf-8")
                contents = py_file.read()
                contents = contents.replace("import Εικόνες προγράμματος_rc","import sys\nimport importlib\nsys.path.append(\""+folders_back+"\")\nicons = importlib.import_module(\"Κεντρικό παράθυρο προγράμματος και αρχείο qrc (Main window and qrc).Εικόνες προγράμματος\")")
                py_file.seek(0)
                py_file.write(contents)
                #py_file.flush()
                py_file.close()
            elif extension=="qrc":
                saved_path = original_path.replace(".qrc",".py").replace(ui_main_dir,py_from_ui_dir)
                subprocess.Popen("python -m PyQt5.pyrcc_main \""+original_path+"\" -o \""+saved_path+"\"", shell=False)

parse_directory_2(ui_main_dir)

At the second part (#2) of the above script (function: parse_directory_2) i call pyuic5 to compile PyQt5 ui files (maked with Qt Designer) to .py files.

The error is:

chris@chris-Inspiron-3847:~/Documents/Projects/papinhio-player/src/python$ python compile-and-backup.py 
Traceback (most recent call last):
  File "/home/chris/Documents/Projects/papinhio-player/src/python/compile-and-backup.py", line 64, in <module>
    parse_directory_2(ui_main_dir)
  File "/home/chris/Documents/Projects/papinhio-player/src/python/compile-and-backup.py", line 40, in parse_directory_2
    parse_directory_2(path+"/"+path_content)
  File "/home/chris/Documents/Projects/papinhio-player/src/python/compile-and-backup.py", line 46, in parse_directory_2
    process = subprocess.Popen("/usr/bin/python3.9 -m PyQt5.uic.pyuic -x \""+original_path+"\" -o \""+saved_path+"\"", shell=False)
  File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.9/subprocess.py", line 1821, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'python -m PyQt5.uic.pyuic -x "/home/chris/Documents/Projects/papinhio-player/ui/main-window/wrong-url-address.ui" -o "/home/chris/Documents/Projects/papinhio-player/src/compiled_ui/main-window/wrong-url-address.py"'

but after that if i manually run python -m PyQt5.uic.pyuic -x "/home/chris/Documents/Projects/papinhio-player/ui/main-window/wrong-url-address.ui" -o "/home/chris/Documents/Projects/papinhio-player/src/compiled_ui/main-window/wrong-url-address.py" no error oquered and the py file is maked.

So, i think is python path error.

I tried with /usr/bin/python3.9 (alias python) but the same error is displaied.

Any thoughts?

1

There are 1 best solutions below

1
Zhenhir On BEST ANSWER

You've made a very common subprocess mistake. You have passed a string to Popen, when you should always pass a list of commands.

For example:

import subprocess

subprocess.Popen(["python3", "-c", "print('Hello world')"])

Rather than what you have:

import subprocess

subprocess.Popen(["python3 -c print('Hello world')"])

Which gives me a very similar error:

Traceback (most recent call last):
  File "/Users/x/x.py", line 4, in <module>
    subprocess.Popen(["python3 -c print('Hello world')"])
  File "/opt/homebrew/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/opt/homebrew/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1821, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: "python3 -c print('Hello world')"

I am using this example instead of your code for simplicity and also because your code depends on local files.. but the principle applies.