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
.