0

So, all I want is opening a Python script with a Python script? I want the equivalent of 'Python script.py'. Can someone explain how can I execute a. py file. How can I use subprocess to do that? Thanks in advance

  • 1
    Import and run it. – Banana Jul 02 '20 at 08:57
  • 3
    Does this answer your question? [How can I make one python file run another?](https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another) – Anze Jul 02 '20 at 09:31

2 Answers2

0

With the subprocess module:

import subprocess

subprocess.run(['python', 'script.py'])

If you want to use the same python as the caller you can do:

import subprocess
import sys

subprocess.run([sys.executable, 'script.py'])
orestisf
  • 1,396
  • 1
  • 15
  • 30
0

There are several ways:

subprocess.call(command)
subprocess.check_call(command)
subprocess.check_output(command)
subprocess.run(command)
os.system(command)

(Each of them has it's own features and you can search in SO to find their differences. I usually use os.system(command))

Also I found THIS: os.exec*** are realy helpful in these situations

Ramin-RX7
  • 128
  • 1
  • 1
  • 13