widnows下写的python放到linux系统跑的问题

windows下写的python脚本,直接放到linux一般是可以执行的,但需要加上python命令:

python test.py

如果直接执行

./test.py

会显示错误:

root@root-VirtualBox:~/mywork/# ./test.py 
' @ error/constitute.c/WriteImage/1037.
' @ error/constitute.c/WriteImage/1037.
' @ error/constitute.c/WriteImage/1037.
' @ error/constitute.c/WriteImage/1037.
' @ error/constitute.c/WriteImage/1037.
' @ error/constitute.c/WriteImage/1037.ck
./test.py: 行 7: $'\r': 未找到命令
./test.py: 行 8: class: 未找到命令
./test.py: 行 9: 未预期的符号 `(' 附近有语法错误
'/test.py: 行 9: `    def __init__(self):

想直接./test.py运行python脚本,需要在test.py的首行加上:

#!/usr/bin/env python

如果出现以下错误,说明文件格式是不对的:

root@root-VirtualBox:~/mywork/# ./test.py 
/usr/bin/env: "python\r": 没有那个文件或目录

英文:
/usr/bin/env: ‘python\r’: No such file or directory

原因:

#!/usr/bin/env python  

在ubuntu会变成

#!/usr/bin/env python\r  

而\r 会被shell 当成参数,所以出现上面的错误。

解决办法:

vi test.py

输入

:set ff=unix

:wq

再执行./test.py就可以正常运行了。

 

原因:

unix换行为0A:

dos换行为0D0A:

利用vi,在vi上打:set ff?,可以显示当前的类型。打:set ff=unix,就可以转换行符号为unix类型;打:set ff=dos那就是dos类型,ff也就是fileformat的意思,把ff写全名fileformat也是可以的。


 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值