Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

파이썬에서 바이트 코드 파일을 생성하는 방법

<시간/>

모든 파이썬 프로그램은 소스 코드를 자동으로 컴파일하여 실행하기 전에 바이트 코드라고도 하는 코드를 컴파일합니다.

처음으로 모듈을 가져올 때마다 또는 소스 파일이 새 파일이거나 업데이트된 파일이 있고 최근에 컴파일된 파일이 있을 때 .py 파일과 동일한 디렉토리에 파일을 컴파일할 때 .pyc 파일이 생성됩니다. (파이썬 3에서- .pyc 파일이 .py 파일과 동일한 디렉토리 대신 __pycache__라는 하위 디렉토리에 있음을 볼 수 있습니다.) 이것은 다음에 프로그램을 실행할 때 파이썬이 컴파일 단계를 건너뛰는 것을 방지하기 때문에 시간을 절약하는 메커니즘입니다.

가져오기(다른 파일)로 스크립트를 실행하는 경우 .pyc 파일이 생성되지 않습니다. 예를 들어 다른 파일(file2.py)을 가져오는 스크립트(file1.py)가 있는 경우

PYC 파일을 만드는 가장 쉬운 방법은 파일을 가져오는 것입니다. MainP.py라는 모듈 이름이 있다고 가정해 보겠습니다. 그냥 하세요 -

>>> import MainP
>>>

그러나 가져오지 않은 모듈에 대해 .pyc 파일을 생성하려면 py_compile이라는 모듈 세트를 만들고 해당 작업을 수행하기 위해 모든 모듈을 컴파일해야 합니다.

py_compile 모듈은 모든 모듈을 수동으로 컴파일할 수 있습니다. py_compile을 사용하여 py_compile 모듈을 대화식으로 만들 수도 있습니다. 컴파일 기능.

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

파이썬 셸에서 위의 명령문을 실행하면 __pycache__ 폴더(python 3)에 .pyc 파일이 생성되고 test.py 파일과 같은 위치에 생성되는 것을 볼 수 있습니다.

파이썬에서 바이트 코드 파일을 생성하는 방법

한 번에 여러 파일을 컴파일하려는 경우 다음과 같이 py_compile.main() 함수를 사용할 수 있습니다. -

>>> #Compiles several files at a time
>>> py_compile.main(['test1.py', 'test2.py', 'test_sample1.py', 'test_sample2.py'])
0

4개의 서로 다른 컴파일된 파일이 생성되는 것을 볼 수 있습니다 -

파이썬에서 바이트 코드 파일을 생성하는 방법

하지만 폴더 안에 있는 모든 파일을 컴파일하고 싶다면 compileall.compile_dir() 함수를 사용하면 됩니다.

>>> # Compile all the .py file from a particular folder.
>>> 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 파일이 생성된 것을 볼 수 있습니다.

파이썬에서 바이트 코드 파일을 생성하는 방법