Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The detailed process of compiling pyc files by python

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "python compilation pyc file detailed process", the explanation in the article is simple and clear, easy to learn and understand, please follow the small series of ideas slowly in-depth, together to study and learn "python compilation pyc file detailed process" bar!

What is pyc file?

Pyc is a binary file, is compiled from py file, generated file, is a byte code, py file into pyc file, loading speed has been improved, and pyc is a cross-platform byte code, is executed by python virtual machine, this is similar to JAVA or. NET virtual machine concept. Pyc content, is related to the version of python, different versions of pyc compiled files are different, 2.5 compiled pyc files, 2.4 version of python is not executable.

What is a pyo file?

pyo is an optimized compiled program python -O source file can be compiled into a pyo file

What is a pyd file?

Pyd is Python's dynamic link library.

Why do I need pyc files?

This requirement is too obvious, because py file can see the source code directly, if you are developing commercial software, it is impossible to leak the source code, right? So you need to compile it into pyc and then publish it. Of course, pyc files can also be decompiled, different versions of the pyc file compiled is different, according to the python source code provided in the opcode, you can decompile pyc files according to the source code, you can find a decompiler python2.3 version of pyc files online tools, but the tool from python2.4 to start charging, if you need to decompile a new version of pyc files, you need to do it yourself (I don't have this ability yet-), but you can modify the opcode file in python source code yourself and recompile python to prevent illegal cracking.

Generate a single pyc file

Python is a good thing, it provides a built-in library to compile py files into pyc files, this module is py_compile module.

The method is very simple, as shown below, directly in idle, you can compile a py file into a pyc file. (Assuming in Windows environment)

The IDLE operation is as follows

##Single file test>> import py_compile>> py_compile.compile(r 'D:\hello.py')'D:\\_pycache__\hello.cpython-39.pyc'>>>#Single file import py_compile.compile(r'script. py')#Multiple files import compileall.compile_dir(r'script file directory')

- Command line operations are as follows

#single file python -m py_compile script.py #multiple files python -m compileall script file directory

As shown in the following figure is compiling multiple scripts (actually forgot to add the path, directly compiled the current directory, awkward…,)

Run pyc

python script.py

_pycache__folder

The interpreter first compiles it into bytecode (which is oversimplification) and stores it in the__pycache__folder. If you look there you'll find a bunch of files sharing.py file names in the project folder, only they have the extension.pyc. They are bytecode compiled versions of program files.

Although compiled files are generated, they are not in the same directory as the source code, which may cause invocation problems. Therefore,.pyc files in sibling directories are often generated as follows:

python -m compileall -b . #Run in the directory where the script is located

Publishing Python Programs

Compile pyc file, suggest adding-O optimization item

python3 -O -m compileall -b .

Delete py file

find . -name "*.py"|xargs rm -rf

Delete__pycache__directory

find . -name "__pycache__" |xargs rm -rf

tar pack

cd ..

tar -cjvf xxx.1.1.0.0.tar.bz2 xxx

Thank you for reading, the above is the "python pyc file compilation process in detail" content, after the study of this article, I believe that we have a deeper understanding of the python pyc file compilation process in detail, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 235

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report