Introduction to running helloworld in Python

Source: Internet
Author: User

If you want to know more about the Python programming language, such as how the Python programming language executes the Python program, how to get help, and other practical application skills related to the language, you can browse our article. The following is a detailed description of the article.

Executable Python Program

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.

 
 
  1. $ chmod a+x helloworld.py  
  2. $ ./helloworld.py  
  3. Hello World   
  4.  

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.

 
 
  1. $ echo $PATH  
  2. /opt/mono/bin/:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/
    bin:/home/swaroop/bin  
  3. $ cp helloworld.py /home/swaroop/bin/helloworld  
  4. $ helloworld  
  5. Hello World   
  6.  

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, its "/home/swaroop/mydir" is the directory 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.

Get help

If you need quick help for a Python function or statement, you can use the built-in help function. Especially when you use a command line with a prompt, it is very useful. For example, run help (str) -- this will display the help of the str class. Str class is used to save various text strings used by your program ). The class will be explained in detail in the next chapter of object-oriented programming.

Note: Press q to exit the help. Similarly, you can obtain information about almost everything in the Python programming language. Use help () to learn

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.