2

I'm trying to run a python script with several parameters, the tab warnings,optimize and verbose parameters, -t, -O and -v respectively.
#!/usr/bin/python -t -O -v
This is the error that I get when I try to run it this way, ./script.py in the terminal.

Unknown option: - usage: /usr/bin/python [option] ... [-c cmd | -m mod | file | -] [arg] ... Try python -h' for more information.

The script runs well when I have a maximum of one parameter in the shebang. Is it wrong to pass more than one parameter in a python shebang?
Running the script as python -O -t -v script.py at the terminal works.

I'm guessing this is a python issue because I have a perl script that has the following shebang #!/usr/bin/perl -w -t and it runs okay.

The only workaround I came up with was creating a python_runner.sh script to invoked the python interpreter with the three parameters:

#!/bin/sh python -O -t -v $1 
6
  • stackoverflow.com/questions/4303128/… Commented Jun 27, 2013 at 23:37
  • Maybe you could try #!/usr/bin/python -tOv Commented Jun 27, 2013 at 23:39
  • You should look at argparse Commented Jun 27, 2013 at 23:39
  • It works for Perl because the Perl interpreter examines the script's #! line. Commented Jun 27, 2013 at 23:41
  • @Vyassa's answer is spot on. It works like a charm. So, why does it work differently for python. Keith, do you want to say that python doesn't? Commented Jun 27, 2013 at 23:47

1 Answer 1

2

Let's say the file is called test.py and starts with a shebang of:

#!/usr/bin/python -t -O -v 

Then calling ./test.py would be the equivalent of the command

/usr/bin/python '-t -O -V' ./test.py 

Everything after the first space is treated as one single argument, that's why you can only supply one argument in a shebang. Luckily you can chain shortopts to -tOv.

Sign up to request clarification or add additional context in comments.

1 Comment

It depends on the system, as detailed in this answer to this question mentioned in @ctn's comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.