Equivalent to source () R in Python

How do we have a source() function to execute an R program in another R program in R studio, how do I execute a python program in another python program?

+5
source share
2 answers

Given 2 python scripts: first.py and second.py , the usual way to execute the first of the second is something in the following lines:

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 

Editing :

  • You can also go for a simple execfile("second.py") if you want (although it is only in the namespace).
  • And the last option uses os.system like this:
    os.system("second.py") .
+4
source

If you use the source directly from GitHub, you can use the request package to download the raw * .py file using http get, and then execute the file.

 import requests exec(requests.get('http://github.myorg.net/raw/repo/directory/file.py').text) 

Disclaimer: I am an R user learning Python, so this may violate some Python best practices.

0
source

Source: https://habr.com/ru/post/1242848/


All Articles