#!/usr/bin/Python
指定用什么解释器运行脚本以及解释器所在的位置

# -*- coding: utf-8 -*-
用来指定文件编码为utf-8的

估计有不少人注意过一些python脚本开头有这么行东东:
#!/usr/bin/python
它是用来干嘛的?貌似没有它对脚本功能也没啥影响。它是用来指定用什么解释器运行脚本以及解释器所在的位置。

以test.py为例,脚本内容如下:

def test():
        print 'hello, world'

if __name__ == "__main__":
        test()

运行脚本:
python test.py
输出:
hello, world

换一种方法运行:
./test.py
会提示出错,文件无可执行权限:
-bash: ./test.py: Permission denied

将文件设为可执行:
chmod +x test.py
继续运行:
./test.py
提示:
./test.py: line 1: syntax error near unexpected token `('
./test.py: line 1: `def test():'
那是因为系统默认该脚本是shell脚本,把它当shell语句执行,当然失败了。

在前面加上
#!/usr/bin/python
申明l这是个python脚本,要用python解释器来运行:
./test.py
输出:
hello, world

这个东东常用在cgi脚本中,apache启动cgi脚本时就靠它来知道这是个python脚本,执行它需要的python解释器路径在哪里。
有时候写 #!/usr/bin/python 还是不行,很简单,因为python解释器没有装在/usr/bin/目录,改成其所在目录就行了,或者更通用的方法是:
#!/usr/bin/env python

Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用的更多相关文章

  1. Python脚本开头两行:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用

    转于:https://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ 出处:在路上 一.基本功能 1)#!/us ...

  2. 关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    #!/usr/bin/python指定用什么解释器运行脚本以及解释器所在的位置 # -*- coding: utf-8 -*-用来指定文件编码为utf-8的PEP 0263 -- Defining P ...

  3. 【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    原文网址:http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ #!/usr/bin/python 是用 ...

  4. 【转载】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    1.#!/usr/bin/python 是用来说明脚本语言是 python 的 是要用 /usr/bin下面的程序(工具)python,这个解释器,来解释 python 脚本,来运行 python 脚 ...

  5. (二)Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用

    #!usr/bin/env python # -*- coding: utf-8 -*- def test(): print('hello, world') if __name__ == " ...

  6. 关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 转

    #!/usr/bin/python 是用来说明脚本语言是python的 是要用/usr/bin下面的程序(工具)python,这个解释器,来解释python脚本,来运行python脚本的. # -*- ...

  7. 关于python脚本头部设置#!/usr/bin/python

    今天又是贼几把菜的一天0.0 读别人程序的时候看到在python文件头部设置签名,感觉贼几把酷,自己也试着在文件前段设置了一下. 设置还是蛮简单的,设置过程如图所示. 设置后如图所示: 当然你也可能看 ...

  8. #!/usr/bin/env python与#!/usr/bin/python的区别

    [摘自:http://blog.csdn.net/wh_19910525/article/details/8040494] 一般的python文件的开头都有#!/usr/bin/python.这是什么 ...

  9. Python程序 #!/usr/bin/python 的解释

    关于脚本第一行的 #!/usr/bin/python 的解释,相信很多不熟悉 Linux 系统的同学需要普及这个知识,脚本语言的第一行,只对 Linux/Unix 用户适用,用来指定本脚本用什么解释器 ...

随机推荐

  1. fopen w c

    http://php.net/manual/en/function.fopen.php

  2. WPF部署问题 解决:The application requires that the assembly...be installed in the GAC

    vs-->引用-->找到问题类库-->邮件属性--->特定版本-->false done

  3. C# xml压缩包不解压的情况下解析xml内容

    string sourceFilePath = @"E:\文件拷贝\xx\3773\3773.zip"; FileInfo fileInfo = new FileInfo(sour ...

  4. For,Function,Lazy

    package com.dtgroup.study import scala.io.Source object ForFunctionLazy { def main(args: Array[Strin ...

  5. Leetcode: Repeated Substring Pattern

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  6. Learning How To Learn

    1.Practice 2.memory every week for from working memory to long tern memory 3.sleep 4.running promote ...

  7. MVC返回JSON数据格式书写方式

    返回json数据格式,多个返回值加,隔开 [Route("api/users/web")] //如果不加这个路由请这样调用:/api/users/web?schoolname=十五 ...

  8. js 格式化数字保留2位小数

    function toDecimal2(x) { var f = parseFloat(x); if (isNaN(f)) { return false; } var f = Math.round(x ...

  9. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数007, match,图像匹配

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数007, match,图像匹配 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换 ...

  10. mysql时间格式DATE_FORMAT()

    1.以下是mysql查询中用到的时间格式的转化形式例子:SELECT DATE_FORMAT(NOW(),'%Y-%m-%d') -- 2015-10-28 SELECT DATE_FORMAT(NO ...