सभी Python प्रोग्राम स्वचालित रूप से आपके स्रोत कोड को संकलित कोड में संकलित करते हैं, जिसे बाइट कोड भी कहा जाता है, इसे निष्पादित करने से पहले।
जब भी हम पहली बार किसी मॉड्यूल को आयात करते हैं या जब आपकी स्रोत फ़ाइल एक नई फ़ाइल होती है या हमारे पास हाल ही में संकलित फ़ाइल की तुलना में एक अद्यतन फ़ाइल होती है, तो फ़ाइल को संकलित करने पर उसी डायरेक्टरी में एक .pyc फ़ाइल बनाई जाएगी जहां .py फ़ाइल है (Python 3 से - आप .pyc फ़ाइल को __pycache__ नामक सबडायरेक्टरी में देख सकते हैं, उसी डायरेक्टरी में जहां आपकी .py फ़ाइल है)। यह एक समय-बचत तंत्र है क्योंकि यह Python को अगली बार आपका प्रोग्राम चलाने पर संकलन चरण को छोड़ने से रोकता है।
यदि आप किसी अन्य फ़ाइल को आयात करके स्क्रिप्ट चला रहे हैं तो कोई .pyc फ़ाइल नहीं बनाई जाएगी। उदाहरण के लिए, यदि आपके पास एक स्क्रिप्ट (file1.py) है जो दूसरी फ़ाइल (file2.py) को आयात करती है।
PYC फ़ाइल बनाने का सबसे आसान तरीका इसे आयात करना है। मान लें कि आपके पास MainP.py नामक एक मॉड्यूल है। बस करें -
>>> import MainP >>>
हालांकि, यदि आप किसी ऐसे मॉड्यूल के लिए .pyc फ़ाइल बनाना चाहते हैं जो आयात नहीं किया गया है, तो हमें py_compile और compileall नामक मॉड्यूल के सेट का उपयोग करना होगा।
py_compile मॉड्यूल मैन्युअल रूप से किसी भी मॉड्यूल को संकलित कर सकता है। हम py_compile.compile फ़ंक्शन का उपयोग करके py_compile मॉड्यूल को इंटरैक्टिवली भी बना सकते हैं।
>>> import py_compile
>>> py_compile.compile('test.py')
'__pycache__\\test.cpython-36.pyc'
>>>
एक बार जब हम उपरोक्त कथन को Python शेल में चलाते हैं, तो हम देख सकते हैं कि एक .pyc फ़ाइल __pycache__ फ़ोल्डर (Python 3) में बनाई गई है, अन्यथा यह आपकी test.py फ़ाइल के समान स्थान पर बनाई जाएगी।

यदि आप एक साथ कई फ़ाइलों को संकलित करना चाहते हैं, तो आप py_compile.main() फ़ंक्शन का उपयोग कर सकते हैं, जैसे -
>>> # एक समय में कई फ़ाइलों को संकलित करता है >>> py_compile.main(['test1.py', 'test2.py', 'test_sample1.py', 'test_sample2.py']) 0
हम देख सकते हैं कि चार अलग-अलग संकलित फ़ाइलें उत्पन्न हुई हैं -

हालांकि, यदि आप किसी फ़ोल्डर के अंदर सभी फ़ाइलों को संकलित करना चाहते हैं, तो आप 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\logs\refs\remotes\origin'...
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__' स्थान के अंदर बनाई गई है।

यदि आप किसी डायरेक्टरी या डायरेक्टरी के अंदर सभी फ़ाइलों को संकलित करना चाहते हैं, तो आप संकलन फ़ंक्शन के साथ ऐसा कर सकते हैं।
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'...
और हम देख सकते हैं कि __pycache__ डायरेक्टरी के अंदर सभी फ़ाइलों के लिए .pyc फ़ाइल बनाई गई है।
