-1
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)

在上述脚本(函数:parse_directory_2)的第二部分(#2)中,我调用 pyuic5 将 PyQt5 ui 文件(使用 Qt Designer 制作)编译为 .py 文件。

错误是:

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"'

但在那之后,如果我手动运行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"没有错误,并且创建了 py 文件。

所以,我认为是python路径错误。

我尝试使用 /usr/bin/python3.9 (别名 python),但显示相同的错误。

有什么想法吗?

4

1 回答 1

2

您犯了一个非常常见的子流程错误。你已经向 Popen 传递了一个字符串,此时你应该总是传递一个命令列表。

例如:

import subprocess

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

而不是你所拥有的:

import subprocess

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

这给了我一个非常相似的错误:

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/python@3.9/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/python@3.9/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')"

为简单起见,我使用此示例而不是您的代码,也因为您的代码依赖于本地文件。但原则适用。

于 2022-01-24T05:14:33.790 回答