Run python file in Linux system

Run python file in Linux system

[Applicable to both Linux system and Linux server]

Under Linux, you can run .py files directly like .exe files by adding any of the following lines to the first line of the .py file:

#!/usr/bin/python
#!/usr/bin/env python

The difference between the two is:

  • #!/usr/bin/python tells the operating system to call the python interpreter in the /usr/bin directory when calling the script, and the path of the python interpreter is clearly given.
  • #!/usr/bin/env python is to prevent users from not installing python in the default /usr/bin path. When the system sees this line, it will first find the python installation path in the env settings, and then call the interpreter program under the corresponding path to complete the operation. #!/usr/bin/env python will go to the environment settings to find the python directory, usually the second way of writing is recommended.

It should be emphasized that the above analysis path should be placed in the first line of the Python script.

After doing the above, use the chmod command (used to modify the access permissions of a directory or file) in the terminal to add executable permissions to the file:

chmod +x filename

[Filename--->For example: test.py, add the suffix name]

Then the target file can be executed without the python command, and the ./filename can be executed.

Such as:

./test.py

 

Guess you like

Origin blog.csdn.net/weixin_44799217/article/details/114047071