What command does the #/bin/python run?

Source: Internet
Author: User
What command does the #/bin/python run? To ask Daniel's explanation ...
Beginner python, practice when encountered this, be despised, whining ~~~~~~~~~~~~~~ hate posture, beg rise ~ ~

Reply content:

First of all, landlord, you have missed a character, the right is

#!/bin/python

To understand what this line is for, you must first understand the concept of magic number.

Each file, at the beginning of the file, has a sequence of bytes identifying the file type, identifying what type the file is. This byte sequence is called "magic number".

Like a script under Unix, this magic number is "#!". The English name of this magic number "Shebang".


When loading a file, the operating system's file interpreter will first determine what type of file it is, and then load a specific file interpreter to interpret the file. The operating system's file interpreter is judged on the basis of this magic number.

So, when you see "#!" , the operating system's file interpreter knows that this is a "script".

What kind of script interpreter do you use exactly? It continues to read.

The results showed that/bin/python.

That is to use an interpreter called "Python" under the/bin/python path to interpret the file.

The operating system's file interpreter then loads the Python interpreter. Then the operating system's file interpreter task is completed.

When the Python interpreter is loaded and run, it interprets the file itself.

At this point, it sees the first line: #!/bin/python

It recognizes that the beginning of the # is the mark that marks the beginning of the comment line, so it thinks this is a line of comments, skip directly.
Then start executing the actual Python instructions that follow.

In conclusion, this line is interpreted in different forms by two-level interpreters. This is a line of comments!
Add it behind the #! is the interpreter definition. This is when you run a script under the Linux/unix system and tell the system what interpreter to use to run the script.
Like you execute a script
$ ./script
#!/bin/python is not a command, it is the location where the Python interpreter is defined. is to tell the system where to find the Python interpreter for this script.
For example, #!/bin/python3 is defined as the Python3 location.
I usually write that, #!/use/bin/env python.
This allows the system to assist in finding the Python interpreter, which can be common across multiple systems. What kind of textbook do you use? Let's not explain this book without looking at it. Reminds the UNIX system to use/bin/python to explain the execution of this script.


  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.