After learning the Python dictionary for a long time, I hope this article will teach you more things. This article describes the practical operation solutions used in the practical application of executable Python.
Executable Python
This part only applies to Linux/Unix users, but Windows users may be curious about the first line of the program. First, we need to run the chmod command to give the program executable permission, and then run the program.
- $ chmod a+x helloworld.py
- $ ./helloworld.py
- Hello World
-
The chmod command is used to change the file mode and allow all users in the system to execute the source file. Then we can directly execute the program by specifying the location of the source file. We use./to indicate that the program is located in the current directory. To be more interesting, you can change your file name to helloworld and run./helloworld. In this way, the program can still work because the system knows that it must run the program using the interpreter specified in the first line of the source file.
As long as you know the exact location of the program, you can run the program now-but what if you want your program to run from all locations? In this case, you can save your program to one of the directories in the PATH environment variable. Every time you run any program, the system will find the directories listed in the PATH environment variable. Then run the program. Simply copy the source file to one of the directories listed in PATH to make your program available anywhere.
- $ echo $PATH
- /opt/mono/bin/:/usr/local/bin:/usr/bin:/bin:/usr/X11R6
/bin:/home/swaroop/bin
- $ cp helloworld.py /home/swaroop/bin/helloworld
- $ helloworld
- Hello World
-
We can use the echo command to display the PATH variable, and use $ to prefix the variable name to show the value of this variable to shell. We can see that/home/swaroop/bin is one of the directories in the PATH variable. Swaroop is the user name used in my system. Generally, there is a similar directory in your system. You can also add the directory you selected to the PATH variable -- this can be done by running PATH = $ PATH:/home/swaroop/mydir, where
- “/home/swaroop/mydir”
-
Is the directory that I want to add to the PATH variable.
This method is useful when you want to run your program anytime and anywhere. It is like creating your own commands, like cd or other Linux terminal or DOS prompt commands. Tip: For Python, programs, scripts, or software refer to the same thing.