How Python is executed
1. Execute with python command
(1) Create a file hello.py file under Linux and enter
Print ("Hello world!")
(2) then execute the command: Python hello.py, output result
[Email protected] ~]# vim hello.py
[email protected] ~]# cat hello.py
Print ("Hello world!")
[email protected] ~]# python hello.py
Hello world!
[Email protected] ~]#
2. Specify interpreter execution
(1) When we execute hello.py in the first step, we explicitly point out that the Python interpreter executes the
(2) If we want to execute a python script like executing a shell script, we need to specify the interpreter in the script:./hello.py
[Email protected] ~]# vim hello.py
[email protected] ~]# cat hello.py
#!/usr/bin/env Pythn
Print ("Hello world!")
[Email protected] ~]# chmod +x hello.py #赋予脚本执行权限
[Email protected] ~]# vim hello.py
[Email protected] ~]#./hello.py
Hello world!
[Email protected] ~]#
3. Execute in the interactive device
(1) We can directly invoke Python's own interactive runtime code
[[email protected] ~]# python
Python 2.6.6 (r266:84292, 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "Help", "copyright", "credits" or "license" for more information.
>>> print ("Hello world!")
Hello world!
>>> exit ()
[Email protected] ~]#
Python executes "Hello World" program-003