无法通过subprocess运行第二个Python文件。

huangapple go评论67阅读模式
英文:

Can't run second python file via subprocess

问题

I need to run send_mess.py from my main application main.py so that the two applications run in parallel to each other.

def mailing_launch(self):
    global p
    p = subprocess.Popen(['python', 'send_mess.py'])
    self.cancel_button.configure(state=customtkinter.NORMAL)
    self.start_button.configure(state=customtkinter.DISABLED)

This code worked fine, but when I moved the project to another computer, it refuses to run:

FileNotFoundError: [Errno 2] No such file or directory: 'python'

If I use

p = subprocess.Popen(['send_mess.py'])

it I get an error:

FileNotFoundError: [Errno 2] No such file or directory: 'send_mess.py'

All files are in the same shared project folder.

无法通过subprocess运行第二个Python文件。

英文:

I need to run send_mess.py from my main application main.py so that the two applications run in parallel to each other.

def mailing_launch(self):
    global p
    p = subprocess.Popen(['python', 'send_mess.py'])
    self.cancel_button.configure(state=customtkinter.NORMAL)
    self.start_button.configure(state=customtkinter.DISABLED)

This code worked fine, but when I moved the project to another computer, it refuses to run:

FileNotFoundError: [Errno 2] No such file or directory: 'python'

If I use

p = subprocess.Popen(['send_mess.py'])

it I get an error:

FileNotFoundError: [Errno 2] No such file or directory: 'send_mess.py'

All files are in the same shared project folder.

无法通过subprocess运行第二个Python文件。

答案1

得分: 1

Here's the translation of the provided text:

我的系统没有python,所以我认为python不一定可用。可以确定使用sys.executable代替,它指向Python解释器的位置,因此请将您的一行代码替换为:

p = subprocess.Popen([sys.executable, 'send_mess.py'])
英文:

My system does not have python, so I assume that python is not always available. The sure thing to use instead if sys.executable which points to where the Python interpreter is, so replace that one line you have with:

    p = subprocess.Popen([sys.executable, 'send_mess.py'])

答案2

得分: 1

You can get the path to the current interpreter with sys.executable. Also, if you are running the script from a different working directory, you might have to parse the path to your other script as well, I did this with pathlib, assuming both files are in the same folder

import subprocess
import sys
from pathlib import Path

python_path = sys.executable
script_path = Path(__file__).parent.joinpath("send_message.py")

p = subprocess.Popen([sys.executable, script_path])
print("do other stuff")
p.wait()
英文:

You can get the path to the current interpreter with sys.executable. Also, if you are running the script from a different working directory, you might have to parse the path to your other script as well, I did this with pathlib, assuming both files are in the same folder

import subprocess
import sys
from pathlib import Path

python_path = sys.executable
script_path = Path(__file__).parent.joinpath("send_message.py")

p = subprocess.Popen([sys.executable, script_path])
print("do other stuff")
p.wait()

huangapple
  • 本文由 发表于 2023年5月17日 22:34:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定