Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include command line options in the python shebang?

I have the canonical shebang at the top of my python scripts.

#!/usr/bin/env python

However, I still often want to export unbuffered output to a log file when I run my scripts, so I end up calling:

$ python -u myscript.py &> myscript.out &

Can I embed the -u option in the shebang like so...

#!/usr/bin/env python -u

and only call:

$ ./myscript.py &> myscript.out &

...to still get the unbuffering? I suspect that won't work, and want to check before trying. Is there something that would accomplish this?

like image 304
Mittenchops Avatar asked May 14 '13 17:05

Mittenchops


People also ask

What do you put in a shebang Python?

A shebang refers to a set of unique characters included at the beginning of a script file. A shebang defines the type and path of the program that should execute the script. The shebang begins with the characters #! followed by the path to the program used to execute the script.

How do you write a shebang line in Python?

#!/usr/bin/env python """ The first line in this file is the "shebang" line. When you execute a file from the shell, the shell tries to run the file using the command specified on the shebang line. The ! is called the "bang".

What is the use of command line option in Python?

Python exposes a mechanism to capture and extract your Python command line arguments. These values can be used to modify the behavior of a program. For example, if your program processes data read from a file, then you can pass the name of the file to your program, rather than hard-coding the value in your source code.

Do you need a shebang line Python?

Whether using the shebang for running scripts in Python is necessary or not, greatly depends on your way of executing scripts. The shebang decides if you'll be able to run the script as a standalone executable without typing the python command. So, if you want to invoke the script directly – use the shebang.


2 Answers

In new versions of env since coreutils 8.30 there is option -S for this. Citation from man env:

   The -S option allows specifing multiple parameters in a script.  Running a script named 1.pl containing the follow‐
  ing first line:

         #!/usr/bin/env -S perl -w -T

  Will execute perl -w -T 1.pl .

  Without the '-S' parameter the script will likely fail with:

         /usr/bin/env: 'perl -w -T': No such file or directory
like image 193
user3132194 Avatar answered Nov 07 '22 11:11

user3132194


You can have arguments on the shebang line, but most operating systems have a very small limit on the number of arguments. POSIX only requires that one argument be supported, and this is common, including Linux.

Since you're using the /usr/bin/env command, you're already using up that one argument with python, so you can't add another argument -u. If you want to use python -u, you'll need to hard-code the absolute path to python instead of using /usr/bin/env, e.g.

#!/usr/bin/python -u

See this related question: How to use multiple arguments with a shebang (i.e. #!)?

like image 27
Barmar Avatar answered Nov 07 '22 10:11

Barmar