0

I have 3 Python scripts, script1, script2 and script3 in a folder. I want to run script2 and script3 using script1. How can I do this?

2
  • Take a look at docs.python.org/3/library/subprocess.html Commented May 15, 2015 at 10:02
  • 1
    import script2 script2.main() (if you defined a main() method) Commented May 15, 2015 at 10:07

3 Answers 3

1

In script1 you need to import script2 and script3:

At the top of script1:

import script2
import script3

To run a function from script2 for example:

script2.function()

You may also need to add a blank file called __init__.py in the same directory as the scripts, so that python can see that the directory is a library.

3
  • I tried import script2 but am getting an error that the module is not found. Commented May 15, 2015 at 10:51
  • In my original post the _ was missing from the init file name. Hopefully that will help. Commented May 15, 2015 at 10:56
  • The file should be called __init__.py with 2 underscores before and after init. Commented May 21, 2015 at 15:59
1

You can use

    execfile("script2.py")
    execfile("script3.py")

or

    subprocess.call("script2.py")
    subprocess.call("script3.py")
0

You can also use os.system:

os.system("script2.py")
os.system("script3.py")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.