linux下直接运行.py文件


在Linux命令行下运行Python,可以直接输出hello world

[python]  view plain  copy
  1. jackywgw@jackywgw-A8F:~/python_learning$ python  
  2. Python 3.3.6 (default, Apr 17 201500:20:01)   
  3. [GCC 4.9.2] on linux  
  4. Type "help""copyright""credits" or "license" for more information.  
  5. >>> print('hello world')  
  6. hello world  
  7. >>>   
  1. jackywgw@jackywgw-A8F:~/python_learning$ python
  2. Python 3.3 .6 (default, Apr 17 2015, 00: 20: 01)
  3. [GCC 4.9 .2] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> print( 'hello world')
  6. hello world
  7. >>>

如果将print ('hello world')写入first_helloworld.py中,通过python first_helloworld.py可以输出hello world.

如果想直接运行./first_helloworld.py,则先要chmod u+x first_helloworld,然后还要在first_helloworld.py最前面加入#!/usr/bin/python3

[python]  view plain  copy
  1. #!/usr/bin/python3  
  2. print('hello world')  
  1. #!/usr/bin/python3
  2. print( 'hello world')

#!/usr/bin/python3 这个为python的可执行路径,可以通过which python3/ which python来获取。

[plain]  view plain  copy
  1. jackywgw@jackywgw-A8F:~/python_learning$ which python3  
  2. /usr/bin/python3  
  3. jackywgw@jackywgw-A8F:~/python_learning$ which python  
  4. /usr/bin/python  
  5. jackywgw@jackywgw-A8F:~/python_learning$   
jackywgw@jackywgw-A8F:~/python_learning$ which python3
/usr/bin/python3
jackywgw@jackywgw-A8F:~/python_learning$ which python
/usr/bin/python
jackywgw@jackywgw-A8F:~/python_learning$ 

这样就可以直接运行./first_helloworld.py输出hello world了。

转载自:http://blog.csdn.net/jackywgw/article/details/4884718

你可能感兴趣的:(【Python】)