师傅你而 发表于 2017-4-25 07:48:09

python文件类型及运行方式

  Ubantu中文件类型主要有三种:

1.源代码

源代码可以不经过编译,由python解释器解释执行
  1.1一般ubantu系统中都默认集成了python的环境,所以在命令行敲入python命令,即可运行python程序,敲入exit()可退出python环境
  1.2.新建python文件
  >>>vim helloworld.py
  然后在helloworld.py中键入
  

#!/usr/bin/python
print "Hello world"


>>>python helloworld.py  1.3.
vim helloworld.py
然后在helloworld.py中键入


#!/usr/bin/pythonprint "hellowold"


然后敲入命令./helloworld直接执行,这时提示权限不够>>>ls -l。发现确实权限不够,这时增加可执行权限
>>>chmod u+x helloworld.py
  OK,敲入命令./helloworld.py。成功执行

2.字节代码

可以把python文件编译成二进制文件,编译后的执行效率更高

需要在源文件中加上

import py_compile

py_compile("**.py")

新建一个test.py文件

>>> vim test.py

import py_compile
py_compile("helloworld.py")


该文件可以编译helloworld.py文件

编译后生成***.pyc文件

>>>python **.pyc执行

3.优化代码

用命令>>>python -o -m py_compile helloworld.py

编译后生成.pyo文件

用命令

>>>python ***.pyo可以执行
  
页: [1]
查看完整版本: python文件类型及运行方式