user3896026
user3896026

Reputation: 13

Running github raw code directly from Python interpreter

I am trying to run python code that I pull directly from Github raw URL using the Python interpreter. The goal is never having to keep the code stored on file system and run it directly from github.

SO far I am able to get the raw code from github using the curl command but since it is a multi-line code, I get the error that python cannot find the file.

 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

How do I pass a multi-line code block to the Python interpreter without having to write another .py file (which would defeat the purpose of this exercise)?

Upvotes: 0

Views: 1465

Answers (2)

zwer
zwer

Reputation: 25809

You need to pipe the code you get from cURL to the Python interpreter, something like:

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

UPDATE: cURL prints download stats to STDERR, if you want it silenced you can use the -s modifier when calling it:

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

Upvotes: 5

Xantium
Xantium

Reputation: 11603

There is no way to do this via Python interpreter, without first retrieving the script then passing it to the interpreter.

The currant Python command line arguments can be accessed with --help argument:

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:]

If you want it all on one line then use | to set multiple commands

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

Upvotes: 1

Related Questions