代码之家  ›  专栏  ›  技术社区  ›  geotheory

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

  •  0
  • geotheory  · 技术社区  · 7 年前

    如何远程执行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 回复  |  直到 7 年前
        1
  •  2
  •   Azat Ibrakov    7 年前

    你可以像下面这样做

    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)