0

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)?

6
  • Your problem doesn't seem to have anything to do with the fact that the code has multiple lines. It looks like you tried to open a curl command line as if it were a file. Commented Apr 3, 2018 at 23:27
  • 1
    are you trying to do this from bash using the python interpreter, or from within the python interpreter? Commented Apr 3, 2018 at 23:28
  • Thanks for your response @user2357112 . I am pretty new to python. Do you have any suggestions how to make the python interpreter run the output of the curl command (and not process it as a file as you correctly noticed) Commented Apr 3, 2018 at 23:34
  • @Hamms I am tying this on the bash command prompt "using" the python command Commented Apr 3, 2018 at 23:35
  • 1
    then @zwer's answer is what you want Commented Apr 3, 2018 at 23:36

2 Answers 2

5

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 - 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response @zwer, I tried piping and the output I get looks like download stats. I did not use the - after the python interpreter cal in my command . I will try that.
@user3896026 - cURL prints its stats to the STDERR, but your script will still be executed by Python. Check the update if you want it completely silent.
Thanks @zwerI I used the -s option for cURL and it worked like a charm.
Please keep in mind that using pipe does not work as expected if the script is interactively ask for user prompt
1

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 

4 Comments

That's not true. You can pipe the STDOUT from cURL directly to the Python interpreter without storing the file locally.
@zwer Sorry, I don't know curl very well. I will remove that part in any case.
Thanks everyone for your responses. it seems that my code does not make for a good test case as it exits when there is no error (its a URL monitor). I will write a simple hello world to test this.
@zwer thanks for the suggestions. I was able to use curl -s (silent mode) and pipe it to "python -" and it works. Thanks everyone else for your contribution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.