直接从Python解释器运行GitHub Raw代码



我正在尝试使用Python Instrymer直接从GitHub Raw URL中拉出Python代码。目标是永远不必将代码存储在文件系统上并直接从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文件(这会破坏本练习的目的(?

您需要将您从卷发到python解释器获得的代码,类似:

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

update :curl打印统计统计数据,如果您希望它沉默,可以在调用它时使用-s修饰符:

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

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

可以使用 --help参数访问墨西兰人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

最新更新