Purpose of #!/usr/bin/python3

前端 未结 6 727
遇见更好的自我
遇见更好的自我 2020-11-30 16:53

I have noticed this in a couple of scripting languages, but in this example, I am using python. In many tutorials, they would start with #!/usr/bin/python3 on t

6条回答
  •  无人及你
    2020-11-30 17:38

    Actually the determination of what type of file a file is very complicated, so now the operating system can't just know. It can make lots of guesses based on -

    • extension
    • UTI
    • MIME

    But the command line doesn't bother with all that, because it runs on a limited backwards compatible layer, from when that fancy nonsense didn't mean anything. If you double click it sure, a modern OS can figure that out- but if you run it from a terminal then no, because the terminal doesn't care about your fancy OS specific file typing APIs.

    Regarding the other points. It's a convenience, it's similarly possible to run

    python3 path/to/your/script

    If your python isn't in the path specified, then it won't work, but we tend to install things to make stuff like this work, not the other way around. It doesn't actually matter if you're under *nix, it's up to your shell whether to consider this line because it's a shellcode. So for example you can run bash under Windows.

    You can actually ommit this line entirely, it just mean the caller will have to specify an interpreter. Also don't put your interpreters in nonstandard locations and then try to call scripts without providing an interpreter.

提交回复
热议问题