0

How to execute Python3 code located remotely - e.g. github?

script_url = "https://gist.githubusercontent.com/geotheory/c874b88e712006802114a50d08c15c46/raw/1f218c0f4aa26b0596d9ef3b67005f7d4a9c8e99/python-test.py" exec(open(script_url).read()) FileNotFoundError: [Errno 2] No such file or directory: 'https://gist.githubusercontent.com/geotheory/c874b88e712006802114a50d08c15c46/raw/1f218c0f4aa26b0596d9ef3b67005f7d4a9c8e99/python-test.py' 

If there's a question already on this I'll happily delete this one, but I've not found anything.

5
  • 2
    open only applies to files that exists on your system's filesystem. The easiest way is to download the file and then execute it within Python. Commented Feb 4, 2019 at 10:06
  • 1
    this is insecure to do such things, to be honest Commented Feb 4, 2019 at 10:07
  • 1
    Ah cracked it import urllib.request then exec(urllib.request.urlopen(script_url).read()) Commented Feb 4, 2019 at 10:07
  • since script_url is not a valid filesystem path, it will give that error. Commented Feb 4, 2019 at 10:09
  • @AzatIbrakov Assuming the URL points to safe code, is the risk any different to downloading the script and executing it locally? Commented Feb 4, 2019 at 10:12

1 Answer 1

2

You can do like below

import urllib script_url = "https://gist.githubusercontent.com/geotheory/c874b88e712006802114a50d08c15c46/raw/1f218c0f4aa26b0596d9ef3b67005f7d4a9c8e99/python-test.py" f = urllib.urlopen(script_url) data = f.read() exec(data) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.