Categories
Python Answers

How to call a script from another script with Python?

Spread the love

Sometimes, we want to call a script from another script with Python.

In this article, we’ll look at how to call a script from another script with Python.

How to call a script from another script with Python?

To call a script from another script with Python, we can import the script with import.

For instance, we write

foo.py

def some_func():
    print('hello')

if __name__ == '__main__':
    some_func()

Then we run some_func in bar.py by writing

import foo

def service_func():
    print('func')

if __name__ == '__main__':
    service_func()
    foo.some_func()

We import foo.py with import foo.

And then we call foo.some_func to call the some_func function in foo.py.

Conclusion

To call a script from another script with Python, we can import the script with import.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *