Python基礎:第一個Python程序(2)

1.Python Shell

1.1 Windows命令

  (1)【開始】|【運行】,輸入cmd回車,進入Windows命令界面。python

  (2)輸入python,回車,進入Python Shell。優化

1.2 IDLE

  Python開始菜單中點擊IDLE,啓動Python Shell。編碼

2.Python文件

2.1 py源碼文件

  hello_world.py:spa

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

  其中,代碼行3d

    Line 1:Linux代碼移植code

    Line 2:文件編碼,可避免出現中文亂碼狀況blog

2.2 pyc字節碼文件

  將源碼py文件編譯成字節碼pyc文件:utf-8

   (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:\Projects\hello_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選項生成的優化後的字節碼文件中的文檔字符串

相關文章
相關標籤/搜索