英文:
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.
英文:
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.
答案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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论