python - Python 3.9.1(或 3.9.2)中 "subprocess"模块的问题 - "subprocess.Popen"不起作用

标签 python tkinter subprocess

当我的脚本有 .pyw扩展,函数 subprocess.Popen不起作用,但如果我使用 .py扩展,它的工作原理。其实扩展并不是那么重要,关键是我是否使用终端来运行脚本,如果我不使用它我有问题,否则没有问题。
这种奇怪的行为只发生在我的带有 Python 3.9.1 或 3.9.2(当前可用的最后一个版本)的 PC 上。但是,我有另一台装有 Python 3.9.0 的 PC,问题不存在。
我给你举个例子。这里我有两个脚本,下面是第一个名为 main_script.pyw :

from tkinter import *
from tkinter import ttk
import sys, subprocess

def MyFunction():
    subprocess.Popen(["start", sys.executable, "script.py"], shell=True)

parent=Tk()
parent.geometry("300x250+370+100")
parent.title("Test")

TestButton=ttk.Button(parent, text="start", width=16, command=MyFunction)
TestButton.place(x=10, y=10)

parent.mainloop()
这是第二个名为 script.py :
a=input("give me a number: ")
它们都放在同一个目录中。用户可以使用函数 subprocess.Popen 启动第二个脚本。仅当主脚本的扩展名是 .py ,否则他不能。我该如何解决这个问题?是BUG吗?
我也附上了一个GIF:
enter image description here
更新! 1
我尝试在虚拟机中安装 Python 3.9.0,并在那里发现了与之前描述的相同的问题!所以,我不明白为什么在我的电脑上,相同版本的脚本可以工作。如果我输入 python --version在我的终端中,输出是 Python 3.9.0 .这是另一个 GIF,向您展示它的工作原理:
enter image description here
在安装路径(C:\Users\USER_NAME\AppData\Local\Programs\Python)中,我看到了两个文件夹,Python39Python38-32 .可能第二个是旧安装的垃圾,我不知道,但也许它有助于使脚本工​​作。你怎么看?
我只想通过 Tkinter 运行 CLI 脚本,就像您在上一个 GIF 中看到的那样(当然不使用终端)。我怎样才能达到我的目标?
更新! 2
我不确定,但可能是 Windows 问题。我尝试使用 subprocess.Popen(["start", sys.executable, "script.py"], shell=True) 运行 Google Chrome说明(在您必须在 Chrome 安装文件夹 C:\Program Files\Google\Chrome\Application 中打开终端之前),它只在我的 PC 上工作,我从来没有遇到过问题,但另一台 Chrome 没有启动,在终端中我得到了这个输出(我使用 ConEmu 作为终端):
>>> subprocess.Popen(["start", sys.executable, "chrome.exe"], shell=True) 
<Popen: returncode: None args: ['start', 'C:\\Users\\aquer\\AppData\\Local\\...>

SyntaxError: Non-UTF-8 code starting with '\x83' in file C:\Program Files\Google\Chrome\Application\chrome.exe on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Current directory:
C:\Program Files\Google\Chrome\Application

Command to be executed:
"C:\Users\USER_NAME\AppData\Local\Programs\Python\Python39\python.exe"   chrome.exe

ConEmuC: Root process was alive less than 10 sec, ExitCode=1.
Press Enter or Esc to close console...
我试图以管理员模式打开终端,但在这种情况下,谷歌浏览器也没有启动。从您的角度来看,问题在哪里?

最佳答案

我认为这是因为 .py Windows 使用的文件 python.exe对于 .pyw Windows 使用的文件 pythonw.exe .问题是 sys.executable具有用于启动主要 python 文件的程序的文件位置(当扩展名为 python.exe 时为 .py,当扩展名为 pythonw.exe 时为 .pyw)。所以问题是你试图启动 .py使用用于 .pyw 的 python 可执行文件的程序文件扩展名。
在命令中:

>>> import sys
>>> sys.executable
'C:\\Program Files\\Python37\\python.exe'
在 IDLE 中(没有终端窗口):
>>> import sys
>>> sys.executable
'C:\\Program Files\\Python37\\pythonw.exe'
因此,您必须决定要使用哪个程序( python.exepythonw.exe )。您可以根据文件扩展名进行操作。

关于python - Python 3.9.1(或 3.9.2)中 "subprocess"模块的问题 - "subprocess.Popen"不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66521759/

相关文章:

python - 为 tkinter 刻度创建自动刻度标签

python - 是否可以使用并发.futures 在事件发生后执行 tkinter 类内的函数/方法?如果是,怎么办?

python - 如何将基于 tkinter 的应用程序与自定义图标捆绑在一起?

python - 将 pandas 列中除第一个之外的重复数字替换为 NAN 值

python - 用我自己的数据进行tensorflow对象检测,你能帮我吗?

python - 在 Python 中,如何将 float 列表转换为具有特定格式的字符串?

python - 在 python 脚本中将子进程与 openssl 一起使用

python - 在kill()之后从子进程中检索一个值

python - 如何加速与子进程的通信

python - 在离线环境中分发 Python 应用程序和解释器的最佳方式是什么?