linux 下直接运行.py文件

在linux命令行下运行python,可以直接输出hello world

jackywgw@jackywgw-A8F:~/python_learning$ python
Python 3.3.6 (default, Apr 17 2015, 00:20:01) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello world')
hello world
>>> 

如果将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

#!/usr/bin/python3
print('hello world')

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

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了。

你可能感兴趣的:(linux 下直接运行.py文件)