解决python错误:-bash: ./fa.py: /usr/bin/python^M: bad interpreter: No such file or directory

在windows上写好的python上传到linux下,给予执行权限后报错;是因为windows和linux的回车机制(换行)不一致导致的:

[root@CactiEZ ~]# python fa.py 
你好!
[root@CactiEZ ~]# chmod +x fa.py 
[root@CactiEZ ~]# ./fa.py 
-bash: ./fa.py: /usr/bin/python^M: bad interpreter: No such file or directory

修改方法:

[root@CactiEZ ~]# vim fa.py 

输入“:”+“set ff=unix”

#!/usr/bin/python
# -*- coding: UTF-8 -*-
print ("你好!")
                                                                                      
......                                                                         
~                                                                                          
~                                                                                          
:set ff=unix

回车,然后保存退出

#!/usr/bin/python
# -*- coding: UTF-8 -*-
print ("你好!")
......                                                                          
~                                                                                          
~                                                                                          
:wq

执行:

[root@CactiEZ ~]# ./fa.py 
你好!

 

你可能感兴趣的:(Python,Linux)