0

Since python 3 is using utf-8 encoding by default, the headline # -*- coding: utf-8 -*- is not needed anymore. However, what about the line #!/usr/bin/env python3 on Unix system?

To my knowledge, this line is supposed to locate the python installation to use for this script. However, the system also has a default python installation and runs python scripts fine without this line. Is it still needed for python version above 3.6?

What about this line when running on Windows? Does it have any impact on Windows runs?

Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • If you try and directly execute a script without the shebang (the `#!` line) it is unlikely to work. (That probably does not apply on Windows.) – khelwood May 25 '21 at 09:40
  • Only Windows has a registry of file name extensions mapping them to interpreters; nowhere else is there such a thing as a "default Python installation" automatically used to run all scripts with .py extensions. (Note that the above is meant to be practically, not pedantically, correct; there does exist a mechanism to set up filename-based interpreter selection on Linux, but it's rarely used and not considered good practice to rely on). – Charles Duffy May 25 '21 at 09:48
  • @CharlesDuffy Ok, but then what about a python script within a pip module, installed. Let's say I also want to make this specific script executable with a `if __name__ == '__main__':` statement at the end. How can I set the shebang in a way that works for every python installation? – Mathieu May 25 '21 at 09:53
  • @Mathieu, ...that's the whole point of `#!/usr/bin/env python3` on UNIX -- it searches the PATH to find the first directory contained with `python3` within it, so it'll use a Python 3 interpreter no matter where it's installed. If your script is also compatible with Python 2.x, you could just change it to `#!/usr/bin/env python` to be able to get the default whether or not it's version-3. – Charles Duffy May 25 '21 at 10:39
  • 1
    @Matchieu, ...one of the most common places where a shebang using `#!/usr/bin/env python` instead of `#!/usr/bin/python` is systems like MacOS, where `/usr/bin` is provided by the OS vendor and its contents can't be changed; to pick bash as an example, `#!/usr/bin/bash` on MacOS selects bash 3.2 (since that was the last version shipped under license terms Apple considers acceptable), whereas folks who care about the version will typically use Homebrew, Macports, Nix or other tools to install a modern (currently 5.x) release instead. – Charles Duffy May 25 '21 at 10:41
  • @Mathieu, that said, just as `pip install yourscript` will generate a .exe wrapper on Windows for its entrypoints, `pip install yourscript` will generate a wrapper script with a shebang pointing to _the specific interpreter that `pip` installed the library for_ on UNIXy systems, so you end up with `#!/path/to/my-virtualenv/bin/python` no matter what -- otherwise an interpreter could be used that didn't have library dependencies installed, so execution would just fail. – Charles Duffy May 25 '21 at 10:43

0 Answers0