我正在尝试运行使用 Python 解释器直接从 Github 原始 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
Run Code Online (Sandbox Code Playgroud)
如何将多行代码块传递给 Python 解释器,而不必编写另一个 .py 文件(这会破坏本练习的目的)?
您需要将从 cURL 获得的代码通过管道传输到 Python 解释器,例如:
curl https://github.url/raw/path-to-code | python -
Run Code Online (Sandbox Code Playgroud)
更新:cURL 将下载统计信息打印到 STDERR,如果你想让它静音,你可以-s在调用它时使用修饰符:
curl -s https://github.url/raw/path-to-code | python -
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
440 次 |
| 最近记录: |