Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้างไฟล์ Byte Code ใน Python


โปรแกรม Python ทุกโปรแกรมคอมไพล์ซอร์สโค้ดของคุณเป็น compiled code หรือเรียกว่า byte code โดยอัตโนมัติ ก่อนรันมัน

ทุกครั้งที่เรานำเข้าโมดูลครั้งแรก หรือเมื่อไฟล์ซอร์สของคุณเป็นไฟล์ใหม่ หรือเรามีไฟล์ที่อัปเดตแล้ว แล้วไฟล์ที่คอมไพล์ล่าสุด ไฟล์ .pyc จะถูกสร้างขึ้นระหว่างการคอมไพล์ไฟล์ในไดเรกทอรีเดียวกับไฟล์ .py (ตั้งแต่ Python 3 - คุณอาจเห็นไฟล์ .pyc อยู่ใน subdirectory ที่ชื่อ __pycache__ แทนที่จะอยู่ในไดเรกทอรีเดียวกับไฟล์ .py ของคุณ) นี่เป็นกลไกประหยัดเวลา เพราะมันป้องกันไม่ให้ Python ข้ามขั้นตอนการคอมไพล์เมื่อคุณรันโปรแกรมครั้งต่อไป

ไม่มีไฟล์ .pyc ถูกสร้างหากคุณรันสคริปต์ด้วย import (อีกไฟล์หนึ่ง) ตัวอย่างเช่น หากคุณมีสคริปต์ (file1.py) ที่ import อีกไฟล์ (file2.py)

วิธีที่ง่ายที่สุดในการสร้างไฟล์ PYC คือการ import มัน สมมติว่าคุณมีโมดูลชื่อ MainP.py แค่ทำ −

>>> import MainP >>>

อย่างไรก็ตาม หากคุณต้องการสร้างไฟล์ .pyc สำหรับโมดูลที่ไม่ได้ถูก import เราต้องใช้ชุดโมดูลที่ชื่อ py_compile และ compileall เพื่อทำภารกิจนั้น

โมดูล py_compile สามารถคอมไพล์โมดูลใดๆ ด้วยตนเอง เราสามารถทำให้โมดูล py_compile ทำงานแบบ interactive ด้วยการใช้ฟังก์ชัน py_compile.compile

>>> import py_compile >>> py_compile.compile('test.py') '__pycache__\\test.cpython-36.pyc' >>>

เมื่อเรารันคำสั่งข้างต้นใน Python shell เราจะเห็นไฟล์ .pyc ถูกสร้างในโฟลเดอร์ __pycache__ (Python 3) มิฉะนั้นจะถูกสร้างในตำแหน่งเดียวกับไฟล์ test.py ของคุณ

วิธีสร้างไฟล์ Byte Code ใน Python

ในกรณีที่คุณต้องการคอมไพล์หลายไฟล์พร้อมกัน คุณสามารถใช้ฟังก์ชัน py_compile.main() ได้ เช่นนี้ −

>>> # คอมไพล์หลายไฟล์พร้อมกัน >>> py_compile.main(['test1.py', 'test2.py', 'test_sample1.py', 'test_sample2.py']) 0

เราเห็นว่ามีไฟล์คอมไพล์ 4 ไฟล์ถูกสร้าง −

วิธีสร้างไฟล์ Byte Code ใน Python

อย่างไรก็ตาม ในกรณีที่คุณต้องการคอมไพล์ทุกไฟล์ภายในโฟลเดอร์ คุณสามารถใช้ฟังก์ชัน compileall.compile_dir() ได้

>>> # คอมไพล์ทุกไฟล์ .py จากโฟลเดอร์เฉพาะ >>> import compileall >>> compileall.compile_dir('gmplot') Listing 'gmplot'... Listing 'gmplot\.git'... Listing 'gmplot\.git\hooks'... Listing 'gmplot\.git\info'... Listing 'gmplot\.git\logs'... Listing 'gmplot\.git\logs\refs'... Listing 'gmplot\.git\logs\refs\heads'... Listing 'gmplot\.git\logs\refs\remotes'... Listing 'gmplot\.git\objects'... Listing 'gmplot\.git\objects\info'... Listing 'gmplot\.git\objects\pack'... Listing 'gmplot\.git\refs'... Listing 'gmplot\.git\refs\heads'... Listing 'gmplot\.git\refs\remotes'... Listing 'gmplot\.git\refs\remotes\origin'... Listing 'gmplot\.git\refs\tags'... Compiling 'gmplot\__init__.py'... Compiling 'gmplot\color_dicts.py'... Listing 'gmplot\gmplot'... Listing 'gmplot\gmplot\markers'... Compiling 'gmplot\gmplot.py'... Compiling 'gmplot\google_maps_templates.py'... Compiling 'gmplot\setup.py'... Listing 'gmplot\tests'... True

ตอนนี้เราเห็นไฟล์ .pyc ถูกสร้างภายในตำแหน่ง 'folder_name\__pycache__'

วิธีสร้างไฟล์ Byte Code ใน Python

ในกรณีที่คุณต้องการคอมไพล์ทุกไฟล์ภายในไดเรกทอรีหรือไดเรกทอรีหลายๆ คุณสามารถทำได้ด้วยฟังก์ชัน compile

C:\Users\rajesh>python -m compileall Skipping current directory Listing 'C:\\Python\\Python361\\python36.zip'... Can't list 'C:\\Python\\Python361\\python36.zip' Listing 'C:\\Python\\Python361\\DLLs'... Listing 'C:\\Python\\Python361\\lib'... Listing 'C:\\Python\\Python361'... Compiling 'C:\\Python\\Python361\\BeautifulSoup_script1.py'... Compiling 'C:\\Python\\Python361\\EDA_python1.py'... Compiling 'C:\\Python\\Python361\\MainP.py'... Compiling 'C:\\Python\\Python361\\NegativeAgeException.py'... Compiling 'C:\\Python\\Python361\\NegativeNumberException.py'... Compiling 'C:\\Python\\Python361\\OtherP.py'... Compiling 'C:\\Python\\Python361\\__init__ Constructor.py'... Compiling 'C:\\Python\\Python361\\attribute_access.py'... ...... Compiling 'C:\\Python\\Python361\\variable_arguments_list.py'... Compiling 'C:\\Python\\Python361\\variable_arguments_list1.py'... Compiling 'C:\\Python\\Python361\\winquality1.py'...

และเราเห็นว่าไฟล์ .pyc ถูกสร้างสำหรับทุกไฟล์ภายในไดเรกทอรี __pycache__

วิธีสร้างไฟล์ Byte Code ใน Python