我的 GUI 是一个 python 脚本启动器,我想创建一个新的控制台窗口来处理选定的脚本。但我无法让 subprocess.Popen 处理正确发送的参数。即使参数列表正确填充,它也会从 GUI 的 Python 环境而不是用户选择的环境运行脚本(eg. ['C:\\Python\\python.exe', '-i', 'C:\\Script\\testscript.py'].
def run_script(self):
# Create a list for arguments to be used in Popen
args = []
# Get the python path from config.ini
# Just assume this is C:\Python\python.exe
python_path = get_settings_value("Python_Path", "Default")
# Add the python path to the argument list
# Add '-i' to argument list, this will open an interactive python window
args.append(python_path)
args.append('-i')
# For every script selected, create a subprocess and run with argument list
for index in self.treeView_scripts.selectedIndexes():
# Only run if selected item is a file
if not self.tree_view_model.isDir(index):
# Get the full path to the script
script_path = os.path.abspath(self.tree_view_model.fileInfo(index))
# Append script path to the argument list, run subprocess, and remove the script path from the argument list
args.append(script_path)
subprocess.Popen(args, executable = sys.executable, creationflags = subprocess.CREATE_NEW_CONSOLE)
args.remove(script_path)