I have 2 .py files: test.py and test2.py
test.py:
import test2 print("This is test.py") test2.py:
print("This is test2.py") When I run test.py, I get:
This is test.py How can I print "This is test2.py" by running test2.py from test.py?
Solution found:
test.py:
print("This is test.py") exec(open("test2.py").read()) test2.py:
if __name__ == '__main__': print("This is test2.py") Output:
This is test.py This is test2.py
importdoes exactly what you want.