目前检索到的方式:
方法1: .py to .c to .pyd
1、通过Cython将.pyx文件编译为.c文件
卡在setup.py的安装上,Jupyter notebook无法转换成.pyd
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
setup(ext_modules = cythonize(['test.py'])) #转码成C
import sys
!{sys.executable} - python setup.py build_ext #打开setup
方法2:Compile .py to .pyc
import py_compile
# Compile to .pyc 转译单个文件
py_compile.compile('test.py')
# Execute .pyc
import sys
import os
!{sys} - python s__pycache__/summ.cpython-37.pyc
》》》'__pycache__/summary.cpython-37.pyc'
##其他工具
# 编译单个文件
py_compile.compile('test.py')
# 编译全部文件
compileall.compile_dir('./', force=True,legacy=True)
## 生成pyc
# 目录下所有py文件生成pyc
import compileall
compileall.compile_dir('D:\python项目\ceshi')
# py生成pyc文件
import py_compile
py_compile.compile('proxy.py')
原理:
print (py_compile.compile().__doc__)
Python学习笔记|Python之pycache文件夹_Tester_hope的博客-CSDN博客_pycache文件夹是什么
善后
# 移除全部.pyc/.py
import os
print (os.listdir('__pycache__')) #(os.listdir('.'))
del_paths = [name for name in os.listdir('__pycache__') if or name.endswith('.py') or name.endswith('.pyc') ]
for del_path in del_paths:
os.remove('__pycache__/'+del_path) #('./'+del_path)
print (os.listdir('__pycache__'))
关于反编译:以子之矛攻子之盾