对pyt源代码进行编译

目前检索到的方式:

方法1: .py to .c to .pyd

1、通过Cython将.pyx文件编译为.c文件

Cython(一)安装与使用_寂小小寞的博客-CSDN博客_cython安装

Cython

卡在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__)

pyc文件

Python学习笔记|Python之pycache文件夹_Tester_hope的博客-CSDN博客_pycache文件夹是什么

python bad magic number

善后

# 移除全部.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__'))

关于反编译:以子之矛攻子之盾

https://github.com/rocky/python-uncompyle6/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值