执行远程源代码时“没有这样的文件或目录”

时间:2019-02-04 10:03:07

标签: python python-3.x

如何执行位于远程的Python3代码-例如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'

如果对此已经有疑问,我会很乐意删除此问题,但是我什么都没找到。

1 个答案:

答案 0 :(得分:2)

您可以像下面这样

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)