Python--on the compilation and decompile of Python

Source: Internet
Author: User

1-python file py involved in the compilation process
源代码文件,由python.exe解释,可在控制台下运行,可用文本编辑器进行编辑;
PYc
源代码文件经过编译后生成的二进制文件,无法用文本编辑器进行编辑;执行一个.py文件后,并不会自动生成对应的.pyc文件,需要指定触发Python来创建pyc文件;- pyc是由py文件经过编译后生成的二进制字节码(byte code)文件;- pyc文件的加载速度比py文件快;- pyc文件是一种跨平台的字节码,由python的虚拟机来执行;- pyc文件的内容跟python版本相关,不同的python版本编译生成不同的pyc文件,只能在相同版本环境下执行;
Pyo
源代码文件经过优化编译后生成的文件,无法用文本编辑器进行编辑;Python3.5之后,不再使用.pyo文件名,而是使用类似“xxx.opt-n.pyc的文件名;
Pyd
是python的动态链接库;动态链接库(DLL)文件是一种可执行文件,允许程序共享执行特殊任务所必需的代码和其他资源;pyd文件虽然是作为python的动态模块,但实质上还是DLL文件,只是后缀改为pyd;一般是用C、C++、D语言按照一定的格式编写;参考信息:https://docs.python.org/3/faq/windows.html?highlight=pyd#is-a-pyd-file-the-same-as-a-dll
Pyz
从Python 3.5开始,定义了.pyz和.pyzw分别作为“Python Zip应用”和“Windows下Python Zip应用”的扩展名。新增了内置zipapp模块来进行简单的管理,可以用Zip打包Python程序到一个可执行.pyz文件。- zipapp — Manage executable python zip archives- https://docs.python.org/3/library/zipapp.html详细内容请见PEP441(https://www.python.org/dev/peps/pep-0441/)
2-Build PYc file

After executing a. py file, the corresponding. pyc file is not automatically generated, and you need to specify the trigger Python to create the PYc file.
You can use the import mechanism of Python to create a PYc file:

    • The built-in py_compile module can compile the py file into a pyc or pyo file;
    • The built-in Compileall module can compile the py file in the whole directory into a pyc or pyo file;
The process of generating a PYC file:
Python在执行import语句时(例如“import abc”),将会到已设定的path中寻找abc.pyc或abc.dll文件。如果只是发现了abc.py,那么Python会首先将abc.py编译成相应的PyCodeObject中间结果,然后创建abc.pyc文件,并将中间结果写入该文件。然后,Python会import这个abc.pyc文件,实际上也就是将abc.pyc文件中的PyCodeObject重新在内存中复制出来。
How to generate the PYc file:

Command form:

python -m py_compile file.py  # 生成单个pyc文件python -m py_compile /dir/{file1,file2}.py  # 生成多个pyc文件python -m compileall /dir/  # 生成目录下所有py文件对应的pyc文件

Script form: Compile function of compile module

import py_compile  # 相当于命令行中的“-m py_compile”py_compile.compile(‘py file path‘)

Script form: Compile_dir function of Compileall module

import compileallcompileall.compile_dir("py files dir")
Example of generating a PYc file:
[Email protected] mingw64/d/anliven/anliven-code/test$ ls-ltotal 2-rw-r--r--1 anliven 197121 50 March 7 22:55 sample.py-rw-r--r--1 anlive  N 197121 49 March 7 23:40 sample2.py[email protected] mingw64/d/anliven/anliven-code/test$ cat sample.py#-*-Coding: Utf-8-*-print ("Hello Python!") [Email protected] mingw64/d/anliven/anliven-code/test$ cat sample2.py#-*-coding:utf-8-*-print ("Hello world!") [Email protected] mingw64/d/anliven/anliven-code/test$ python-m compileall./listing ' ... Compiling './sample.py ' ... Compiling './sample2.py ' ... [Email protected] mingw64/d/anliven/anliven-code/test$ ls-ltotal 6drwxr-xr-x 1 anliven 197121 0 March 7 23:42 __pycache__/-rw-r--r--1 anl Iven 197121 50 March 7 22:55 sample.py-rw-r--r--1 Anliven 197121 49 March 7 23:40 sample2.py[email protected] MINGW64 /d/anliven/anliven-code/test$ ls-l __pycache__/total 2-rw-r--r--1 anliven 197121 122 March 7 23:41 sample.cpython-36.pyc- rw-r--r--1 Anliven 197121 122 March 7 23:41 SAMPLE2.CPYTHON-36.PYC 
3-Build Pyo file

Similar to generating PYC files, but additional use of the-O and-oo options to generate PYO files.
However, after Python3.5, the. pyo file name is not used, but a file with a filename similar to "XXX.OPT-N.PYC is generated.

command example:
python -O -m py_compile file.pypython -O -m py_compile /dir/{file1,file2}.pypython -O -m compileall /dir/
Example: python3.6 generating a Pyo file
[email protected] MINGW64 /d/Anliven/Anliven-Code/Test$ python -O -m compileall ./Listing ‘./‘...Compiling ‘./sample.py‘...Compiling ‘./sample2.py‘...[email protected] MINGW64 /d/Anliven/Anliven-Code/Test$ ls -l __pycache__/total 4-rw-r--r-- 1 anliven 197121 122 3月   7 23:42 sample.cpython-36.opt-1.pyc-rw-r--r-- 1 anliven 197121 122 3月   7 23:41 sample.cpython-36.pyc-rw-r--r-- 1 anliven 197121 122 3月   7 23:42 sample2.cpython-36.opt-1.pyc-rw-r--r-- 1 anliven 197121 122 3月   7 23:41 sample2.cpython-36.pyc
Example: python2.7 generating a Pyo file
[email protected] MINGW64 /d/Anliven/Anliven-Code/Test$ ls -ltotal 6drwxr-xr-x 1 anliven 197121  0 3月   7 23:42 __pycache__/-rw-r--r-- 1 anliven 197121 50 3月   7 22:55 sample.py-rw-r--r-- 1 anliven 197121 49 3月   7 23:40 sample2.py[email protected] MINGW64 /d/Anliven/Anliven-Code/Test$ py -2 -O -m compileall ./Listing ./ ...Listing ./__pycache__ ...Compiling ./sample.py ...Compiling ./sample2.py ...[email protected] MINGW64 /d/Anliven/Anliven-Code/Test$ ls -ltotal 8drwxr-xr-x 1 anliven 197121   0 3月   7 23:42 __pycache__/-rw-r--r-- 1 anliven 197121  50 3月   7 22:55 sample.py-rw-r--r-- 1 anliven 197121 122 3月   7 23:45 sample.pyo-rw-r--r-- 1 anliven 197121  49 3月   7 23:40 sample2.py-rw-r--r-- 1 anliven 197121 122 3月   7 23:45 sample2.pyo
4-Running the PYC or PYO file to run the PYc file
[email protected] MINGW64 /d/Anliven/Anliven-Code/Test/__pycache__$ python sample.cpython-36.pycHello Python ![email protected] MINGW64 /d/Anliven/Anliven-Code/Test/__pycache__$ python sample2.cpython-36.pycHello World ![email protected] MINGW64 /d/Anliven/Anliven-Code/Test/__pycache__$ python sample.cpython-36.opt-1.pycHello Python ![email protected] MINGW64 /d/Anliven/Anliven-Code/Test/__pycache__$ python sample2.cpython-36.opt-1.pycHello World !
Run the Pyo file
[email protected] MINGW64 /d/Anliven/Anliven-Code/Test$ ls -ltotal 8drwxr-xr-x 1 anliven 197121   0 3月   7 23:42 __pycache__/-rw-r--r-- 1 anliven 197121  50 3月   7 22:55 sample.py-rw-r--r-- 1 anliven 197121 122 3月   7 23:45 sample.pyo-rw-r--r-- 1 anliven 197121  49 3月   7 23:40 sample2.py-rw-r--r-- 1 anliven 197121 122 3月   7 23:45 sample2.pyo[email protected] MINGW64 /d/Anliven/Anliven-Code/Test$ py -2 sample.pyoHello Python ![email protected] MINGW64 /d/Anliven/Anliven-Code/Test$ py -2 sample2.pyoHello World !
5-Python anti-compilation with Uncompyle6 Uncompyle6
    • Python cross-version Byte-code Decompiler
    • Pypi:https://pypi.python.org/pypi/uncompyle6
    • Github:https://github.com/rocky/python-uncompyle6
Installing Uncompyle6
$ PIP3 Install--proxy= "10.144.1.10:8080" uncompyle6collecting uncompyle6 downloading UNCOMPYLE6-3.0.0-PY36-NONE-ANY.WHL (195kB) 100% |████████████████████████████████| 204kB 321kb/srequirement already satisfied:six in C:\python36\lib\site-packages (from Uncompyle6) collecting spark-parser<1.9.0,>=1.8.5 (from uncompyle6) downloading spark_parser-1.8.5-py36-none-any.whlcollecting Xdis <3.7.0,>=3.6.9 (from Uncompyle6) downloading XDIS-3.6.11-PY36-NONE-ANY.WHL (74kB) 100% |███████████████████████ █████████| 81kB 153kb/scollecting Click (from Spark-parser<1.9.0,>=1.8.5->uncompyle6) Using Cached Click-6.7-py2.py3-none-any.whlinstalling collected Packages:click, Spark-parser, Xdis, uncompyle6Successfully Installed click-6.7 spark-parser-1.8.5 uncompyle6-3.0.0 xdis-3.6.11$ PIP3 show Uncompyle6name:uncompyle6version: 3.0.0summary:python cross-version Byte-code Decompilerhome-page:https://github.com/rocky/python-uncompyle6/author : Rocky Bernstein, Hartmut Goebel, John Aycock, and othersauthor-email: [email protected]license:mitlocation:c:\python36\lib\ Site-packagesrequires:xdis, Spark-parser, six
Example: decompile a PYc file
[Email protected] mingw64/d/anliven/anliven-code/test/__pycache__$ ls-ltotal 4-rw-r--r--1 anliven 197121 122 March 7 23:42 sample.cpython-  36.opt-1.pyc-rw-r--r--1 Anliven 197121 122 March 7 23:41 sample.cpython-36.pyc-rw-r--r--1 anliven 197121 122 March 7 23:42 sample2.cpython-36.opt-1.pyc-rw-r--r--1 Anliven 197121 122 March 7 23:41 sample2.cpython-36.pyc[email protected] MIN Gw64/d/anliven/anliven-code/test/__pycache__$[email protected] Mingw64/d/anliven/anliven-code/test/__ pycache__$ uncompyle6 sample.cpython-36.pyc > s1.py[email protected] mingw64/d/anliven/anliven-code/test/__ pycache__$ ls-ltotal 5-rw-r--r--1 anliven 197121 335 March 8 00:01 s1.py-rw-r--r--1 anliven 197121 122 March 7 23:42 Samp le.cpython-36.opt-1.pyc-rw-r--r--1 Anliven 197121 122 March 7 23:41 sample.cpython-36.pyc-rw-r--r--1 anliven 197121 122 3 Month 7 23:42 sample2.cpython-36.opt-1.pyc-rw-r--r--1 anliven 197121 122 March 7 23:41 Sample2.cpython-36.pyc[email pro Tected] Mingw64/d/anliven/anliven-code/test/__pycache__$ Cat s1.py# uncompyle6 version 3.0.1# Python bytecode 3.6 (3379) # decompiled FROM:PYT Hon 3.6.2 (V3.6.2:5FD33B5, Jul 8, 04:57:36) [MSC v.1900-bit (AMD64)]# Embedded file name:./sample.py# Compiled A t:2018-03-07 22:55:30# Size of source mod 2**32:50 bytesprint (' Hello Python! ') # okay decompiling sample.cpython-36.pyc[email protected] mingw64/d/anliven/anliven-code/test/__pycache__$
Example: decompile a pyo file
[Email protected]                                                                                             Mingw64/d/anliven/anliven-code/test $ ls-l                                                                                             Total 8                                           Drwxr-xr-x 1 anliven 197121 0 March 8 00:01 __pycache__/  -rw-r--r--1 Anliven 197121 50 March 7 22:55 sample.py-rw-r--r--1    Anliven 197121 122 March 7 23:45 sample.pyo-rw-r--r--1 Anliven 197121 49 March                                                                                                                           7 23:40 sample2.py-rw-r--r--1 anliven 197121 122 March 7 23:45 Sample2.pyo [Email protected] Mingw64/d/anliven/anliven-code/test $                                                                                                                          [Email protected]                                                                                                                                     Mingw64/d/anliven/anliven-code/test $ uncompyle6 sample2.pyo > s2.py [Email protected]                                                                                         Mingw64/d/anliven/anliven-code/test $ cat s2.py                                                                          # Uncompyle6 Version 3.0.1                                                                       # Python Bytecode 2.7 (62211) # decompiled From:python 3.6.2 (v3.6.2:5fd33b5, Jul 8, 04:57:36) [MSC v.1900-bit (AMD64)] # Embedded file name:./sample2.py # Compiled at:2018-03-                                                                               23:40:22 print ' Hello world! '                                                                                                                                                              # okay decompiling sample2.pyo [Email protected]                                                                                                    Mingw64/d/anliven/anliven-code/test $
Uncompyle6 's Help information
$ uncompyle6--helpusage:uncompyle6 [OPTIONS] ... [FILE | DIR] ... uncompyle6 [--help |-H |--v |--version]examples:uncompyle6 FOO.PYC bar.pyc # decompile Foo.pyc, b Ar.pyc to stdout Uncompyle6-o. FOO.PYC Bar.pyc # decompile to./foo.pyc_dis and./bar.pyc_dis uncompyle6-o/tmp/usr/lib/python1.5 # decompile WH OLE libraryoptions:-o <path> output decompiled files to this path:if multiple input files is decompiled, the common prefix is stripped from these names and the remainder appended to &lt ;p ath> uncompyle6-o/tmp bla/fasel.pyc BLA/FOO.PYC,/tmp/fasel.pyc_dis,/tmp/  Foo.pyc_dis uncompyle6-o/tmp bla/fasel.pyc BAR/FOO.PYC-/tmp/bla/fasel.pyc_dis,  /tmp/bar/foo.pyc_dis Uncompyle6-o/tmp/usr/lib/python1.5,/tmp/smtplib.pyc_dis     .../tmp/lib-tk/fixtk.pyc_dis-c <file>Attempts a disassembly after compiling <file>-D print timestamps-p <integer> use &LT;INTEGER&G T Number of Processes-r recurse directories looking for. PYc and. pyo files--fragments Use fragments Depars Er--verify Compare generated source with input Byte-code--verify-run compile generated source, run it and check                Exit code--weak-verify Compile generated source--linemaps generated line number correspondencies between Byte-code and generated source output--help show this messagedebugging Options:--asm-a include byte- Code (disables--verify)--grammar-g show matching grammar--tree-t include syntax tree (disables- -verify) Extensions of generated files: '. Pyc_dis '. Pyo_dis ' successfully decompiled (and verified if--verify) + ' _  Unverified ' successfully decompile but--verify failed + ' _failed ' decompile failed (contact author for Enhancement)
6-Other Python anti-compilation tools decompyle++

A Python Byte-code Disassembler/decompiler
Https://github.com/zrax/pycdc

Easy Python Decompiler

https://sourceforge.net/projects/easypythondecompiler/
Easy Python decompiler is Python bytecode decompiler, decompiles PYc & pyo files.

Python--on the compilation and decompile of Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.