直接从Python解释器运行github原始代码

时间:2018-04-03 23:21:41

标签: python github

我正在尝试运行python代码,我使用Python解释器直接从Github原始URL中提取。目标是永远不必将代码保存在文件系统上并直接从github运行。

到目前为止,我能够使用curl命令从github获取原始代码,但由于它是一个多行代码,我得到python无法找到该文件的错误。

 python 'curl https://github.url/raw/path-to-code'
 python: can't open file 'curl https://github.url/raw/path-to-code': [Errno 
 2] No such file or directory

如何将多行代码块传递给Python解释器而无需编写另一个.py文件(这会破坏本练习的目的)?

2 个答案:

答案 0 :(得分:4)

您需要将从cURL获得的代码传递给Python解释器,例如:

curl https://github.url/raw/path-to-code | python -

更新:cURL将下载统计信息打印到STDERR,如果您希望它静音,则可以在调用它时使用-s修饰符:

curl -s https://github.url/raw/path-to-code | python -

答案 1 :(得分:1)

没有办法通过Python解释器执行此操作,无需先检索脚本然后将其传递给解释器。

可以使用--help参数访问currant Python命令行参数:

usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-b     : issue warnings about str(bytes_instance), str(bytearray_instance)
     and comparing bytes/bytearray with str. (-bb: issue errors)
-B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
-i     : inspect interactively after running script; forces a prompt even
     if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-I     : isolate Python from the user's environment (implies -E and -s)
-m mod : run library module as a script (terminates option list)
-O     : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x
-OO    : remove doc-strings in addition to the -O optimizations
-q     : don't print version and copyright messages on interactive startup
-s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-S     : don't imply 'import site' on initialization
-u     : force the binary I/O layers of stdout and stderr to be unbuffered;
     stdin is always buffered; text I/O layer will be line-buffered;
     also PYTHONUNBUFFERED=x
-v     : verbose (trace import statements); also PYTHONVERBOSE=x
     can be supplied multiple times to increase verbosity
-V     : print the Python version number and exit (also --version)
     when given twice, print more information about the build
-W arg : warning control; arg is action:message:category:module:lineno
     also PYTHONWARNINGS=arg
-x     : skip first line of source, allowing use of non-Unix forms of #!cmd
-X opt : set implementation-specific option 
file   : program read from script file
-      : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]

如果您想要一行,请使用|设置多个命令

curl https://github.url/raw/path-to-code --output some.file|python some.file