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
Plakhoy
  • 1,846
  • 1
  • 18
  • 30
  • http://stackoverflow.com/questions/4303128/how-to-use-multiple-arguments-with-a-shebang-i-e – ctn Jun 27 '13 at 23:37
  • Maybe you could try #!/usr/bin/python -tOv – Vyassa Baratham Jun 27 '13 at 23:39
  • You should look at argparse – Achrome Jun 27 '13 at 23:39
  • It works for Perl because the Perl interpreter examines the script's `#!` line. – Keith Thompson Jun 27 '13 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? – Plakhoy Jun 27 '13 at 23:47
  • @Segfault: The kernel examines the `#!` line to determine the name of the interpreter to invoke and the argument(s) to pass to it. Perl additionally examines the `#!` line, if any, after it starts running, useful because it's common to specify options like `-w` and it works even on Windows. I don't know how Python behaves, but my guess is that it just treats the `#!` lines as a comment. – Keith Thompson Jun 28 '13 at 00:04

1 Answers1

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.

mata
  • 67,110
  • 10
  • 163
  • 162
  • 3
    It depends on the system, as detailed in [this answer](http://stackoverflow.com/a/4304187/827263) to [this question](http://stackoverflow.com/questions/4303128/how-to-use-multiple-arguments-with-a-shebang-i-e) mentioned in @ctn's comment. – Keith Thompson Jun 28 '13 at 00:05