Executing python files in Linux terminals (terminal) __linux

Source: Internet
Author: User
Tags python script

executing python files in Linux terminals (terminal) Interactive Environment

Run Python in the Linux command-line mode, go into the python interactive environment, write the program directly and output the results.

In Python's interactive command-line writing program, the advantage is that you can get results, the disadvantage is not saved, the next time you want to run, you have to knock again. To generate a. py file using the editor

Writes print "Hello World" to helloworld.py and uses Python helloworld.py to output Hello World (must find the correct location for the target file). If you want to view the contents of the file before the output, you can use the Cat command to obtain and display it in the terminal.


By learning that you can run a. py file directly under Linux as if you were running an. exe file by adding any of the following lines to the first line of the. py file:

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

The difference is:
#!/usr/bin/python is to tell the operating system to invoke the Python interpreter in the/usr/bin directory when invoking the script, and the path to the Python interpreter is explicitly given.
#!/usr/bin/env Python is designed to prevent users from not putting Python in the default/usr/bin path. When the system sees this line, it first looks in the env settings to find the Python installation path, and then calls the interpreter program in the corresponding path to complete the operation. #!/usr/bin/env Python will go to the environment setting to look for the Python directory and usually recommend the second way.
Again, the parse path above should be placed in the first line of the Python script.

Then use the chmod command in terminal to modify the access rights for the directory or file, and the following modify the target file helloworld.py permissions to allow all users to access and execute.

You can look at the permissions of the target file, and it does satisfy all user access and can be performed.

The target file can then be executed without a python command.

Also, if you write #!/usr/bin/env python in the first line of the destination file, you do not need to set the file to the. py format or execute directly, because you have specified the Python interpreter that executes the file as the corresponding directory. The following example:

Friendly reminder:./represents the current directory in Linux.
The following for reference blog and Link:

http://blog.csdn.net/boriscoding/article/details/21714923
Http://www.thinksaas.cn/topics/0/507/507003.html
http://blog.csdn.net/huangfei711/article/details/51051633

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.