JDS
JDS

Reputation: 16988

From Python script how to run other Python script?

In my main Python script, I want to call another python script to run, as follows:

python2 ~/script_location/my_side_script.py \ --input-dir folder1/in_folder \ --output-dir folder1/out_folder/ \ --image-ext jpg \

From inside my Python script, how exactly can I do this?

I will be using both Windows and Ubuntu, but primarily the latter. Ideally would like to be able to do on both.

Upvotes: 0

Views: 112

Answers (1)

kennyvh
kennyvh

Reputation: 2854

You could import the script in your main file.

Suppose you have two files: myscript.py and main.py

# myscript.py
print('this is my script!')
# main.py
print('this is my main file')
import myscript
print('end')

The output if you run main.py would be:

this is my main file
this is my script
end

EDIT: If you literally just want to call python2 my_side_script.py --options asdf, you could use the subprocess python module:

import subprocess

stdout = subprocess.check_output(['python2', 'my_side_script.py', '--options', 'asdf'])

print(stdout)      # will print any output from your sidescript

Upvotes: 3

Related Questions