Reputation: 879
Like we have source()
function to execute a R
program in another R
program in R studio, how do I execute a python program in another python program?
Upvotes: 23
Views: 10233
Reputation: 1886
If you're used to sourcing directly from GitHub, you can use the requests package to download the raw *.py file with an http get, and then execute the file.
import requests
exec(requests.get('http://github.myorg.net/raw/repo/directory/file.py').text)
Disclaimer: I'm an R user learning Python, so this might be violating some Python best practices
Upvotes: 1
Reputation: 15320
Given 2 python scripts: first.py
and second.py
, the usual way to execute the first from the second is something in the lines of:
first.py:
def func1():
print 'inside func1 in first.py'
if __name__ == '__main__':
# first.py executed as a script
func1()
second.py:
import first
def second_func():
print 'inside second_func in second.py'
if __name__ == '__main__':
# second.py executed as a script
second_func()
first.func1() # executing a function from first.py
Edits:
execfile("second.py")
if you wish (although it is only within the calling namespace).os.system
like so:os.system("second.py")
.Upvotes: 9