Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
直接从Python解释器运行github原始代码_Python_Github - Fatal编程技术网

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

直接从Python解释器运行github原始代码,python,github,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.ur

我正在尝试运行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文件(这将破坏本练习的目的)?

您需要将从cURL获得的代码通过管道传递到Python解释器,类似于:

curl https://github.url/raw/path-to-code | python -
更新:cURL将下载统计数据打印到STDERR,如果您想让它静音,可以在调用它时使用
-s
修饰符:

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

您需要将从cURL获得的代码通过管道传输到Python解释器,例如:

curl https://github.url/raw/path-to-code | python -
更新:cURL将下载统计数据打印到STDERR,如果您想让它静音,可以在调用它时使用
-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

通过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


您的问题似乎与代码有多行这一事实无关。看起来您试图像打开文件一样打开
curl
命令行。您是使用python解释器从bash执行此操作,还是在python解释器中执行此操作?感谢@user2357112的回复。我对python还很陌生。您对如何让python解释器运行curl命令的输出(而不是像您正确注意到的那样将其作为文件处理)有什么建议吗@Hamms我将此绑定在bash命令提示符“using”上python命令Then@zwer的答案是您想要的。您的问题似乎与代码有多行这一事实无关。看起来您试图像打开文件一样打开
curl
命令行。您是使用python解释器从bash执行此操作,还是在python解释器中执行此操作?感谢@user2357112的回复。我对python还很陌生。您对如何让python解释器运行curl命令的输出(而不是像您正确注意到的那样将其作为文件进行处理)有什么建议吗@Hamms我将此绑定在bash命令提示符上“使用”python命令然后@zwer的答案是您想要的那不是真的。您可以将标准输出从cURL直接传输到Python解释器,而无需将文件存储在本地。@zwer抱歉,我不太清楚
cURL
。无论如何,我都会删除那个部分。谢谢大家的回复。我的代码似乎不是一个好的测试用例,因为它在没有错误时退出(它是一个URL监视器)。我将编写一个简单的hello world来测试它。@zwer感谢您的建议。我能够使用curl-s(静默模式)并将其传输到“python-”中,并且可以正常工作。谢谢大家的贡献。那不是真的。您可以将标准输出从cURL直接传输到Python解释器,而无需将文件存储在本地。@zwer抱歉,我不太清楚
cURL
。无论如何,我都会删除那个部分。谢谢大家的回复。我的代码似乎不是一个好的测试用例,因为它在没有错误时退出(它是一个URL监视器)。我将编写一个简单的hello world来测试它。@zwer感谢您的建议。我能够使用curl-s(静默模式)并将其传输到“python-”中,并且可以正常工作。谢谢大家的贡献。谢谢你的回复@zwer,我尝试了管道,我得到的输出看起来像下载统计数据。我没有在命令中使用-after-python解释器cal。我会试试。@user3896026-cURL将其统计数据打印到STDERR,但您的脚本仍将由Python执行。如果你想让它完全安静,请检查更新。谢谢@zwerI我使用-s选项来cURL,它的工作很有魅力。谢谢你的响应@zwer,我尝试了管道,我得到的输出看起来像下载统计。我没有在命令中使用-after-python解释器cal。我会试试。@user3896026-cURL将其统计数据打印到STDERR,但您的脚本仍将由Python执行。如果你想让它完全静音,请检查更新。谢谢@zwerI,我使用了-s选项进行卷发,效果非常好。