Python基础:第一个Python程序(2)

1.Python Shell

1.1 Windows命令

  (1)【开始】|【运行】,输入cmd回车,进入Windows命令界面。

  (2)输入python,回车,进入Python Shell。

1.2 IDLE

  Python开始菜单中点击IDLE,启动Python Shell。

2.Python文件

2.1 py源码文件

  hello_world.py:

1 #! /usr/bin/python3
2 # -*- coding: utf-8 -*- 
3 
4 print('Hello World!')

  其中,代码行

    Line 1:Linux代码移植

    Line 2:文件编码,可避免出现中文乱码情况

2.2 pyc字节码文件

  将源码py文件编译成字节码pyc文件:

   (1)单个生成pyc文件

  命令方式:

python -m py_compile hello_world.py

  或

python -m compileall hello_world.py

  脚本方式:

  启动Python IDLE

>>> import py_compile
>>> if __name__=='__main__':
        py_compile.compile(r'F:Projectshello_world.py')

  (2)批量生成pyc文件

  脚本方式:

>>> import compileall
>>> if __name__=='__main__':
        compileall.compile_dir(r'F:Projects')

2.3 opt.pyc优化编译文件

python -O -m py_compile hello_world.py
python -O -m compileall hello_world.py

  优化编译后生成的文件:__pycache__/hello_world.cpython-37.opt-1.pyc

python -OO -m py_compile hello_world.py
python -OO -m compileall hello_world.py

  优化编译后生成的文件:__pycache__/hello_world.cpython-37.opt-2.pyc

  -O参数:生成更加紧凑的优化后的字节码

  -OO参数:进一步移除-O选项生成的优化后的字节码文件中的文档字符串

原文地址:https://www.cnblogs.com/libingql/p/10160082.html