2

I have a python script that resides on a remote server, under version control, and I'd like to execute it from my local shell.

I know that curl https://remote.path/script.py | python will work (as confirmed here) when there are no additional parameters.

The problem is, I can't figure out how to pass in additional command line arguments, e.g. python script.py arg1 arg2 arg3?

I recognize this is may not be the most secure practice, but the script is pretty benign.

1
  • are you using argparse or optparse? Commented Sep 27, 2013 at 6:01

3 Answers 3

2

If you check a manual page you will see that the python command takes either a script or the character -. The - argument instead of a script is used to tell the Python command that the script should be read from standard input. So using that all other arguments are know to be arguments to the script.

Used like

$ curl https://remote.path/script.py | python - arg1 arg2 arg3
Sign up to request clarification or add additional context in comments.

Comments

2

man python would have answered your question:

   python [ -B ] [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ]
          [ -O ] [ -OO ] [ -R ] [ -Q argument ] [ -s ] [ -S ] [ -t ] [  -u
   ]
          [ -v ] [ -V ] [ -W argument ] [ -x ] [ -3 ] [ -?  ]
          [ -c command | script | - ] [ arguments ]

Say:

curl https://remote.path/script.py | python - arg1 arg2 arg3

Example:

$ cat s
import sys
print  sys.argv[1:]
$ cat s | python - arg1 arg2 arg3
['arg1', 'arg2', 'arg3']

Comments

0

Very easily (and not recommended) you can just catch them in python using sys.argv

In file import sys

print sys.argv[1]

In terminal

python myfile.py foo

foo

If you do

print sys.argv[1:]

As another response suggested you'll get

["foo"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.