1、安装依赖文件

pip install cython
yum install python-devel
yum install gcc

2、将py文件打包成so

(1) 例如创建待编译文件track.py(自己编写的代码),以下为配置文件setup.py的内容

from distutils.core import setup
from Cython.Build import cythonize
# 将一个py文件打包成so文件
setup(ext_modules = cythonize(["track.py"]))
# 打包多个py文件
setup(ext_modules = cythonize(["track.py", "xxx.py", "xxxx.py", ...]))

(2) 在setup.py文件所在路径,运行python setup.py build_ext

(3) 执行成功后再当前目录下生成filename.c(track.c)文件以及一个build文件夹

(4) 在build文件夹下存放着编译好的so文件

(5) 调用时直接加载from track import *

3、将整个文件夹(项目文件)打包成so格式

打包整个文件夹(项目文件时),与一个或多个py文件直接打包的区别就是配置文件setup.py的编写。

# -*- coding:utf-8 -*-
import sys, os, shutil, time
from distutils.core import setup
from Cython.Build import cythonizestarttime = time.time()
currdir = os.path.abspath('.')
parentpath = sys.argv[1] if len(sys.argv)>1 else ""
setupfile= os.path.join(os.path.abspath('.'), __file__)
build_dir = "build"
build_tmp_dir = build_dir + "/temp"def getpy(basepath=os.path.abspath('.'), parentpath='', name='', excepts=(), copyOther=False,delC=False):"""获取py文件的路径:param basepath: 根路径:param parentpath: 父路径:param name: 文件/夹:param excepts: 排除文件:param copy: 是否copy其他文件:return: py文件的迭代器"""fullpath = os.path.join(basepath, parentpath, name)for fname in os.listdir(fullpath):ffile = os.path.join(fullpath, fname)#print basepath, parentpath, name,fileif os.path.isdir(ffile) and fname != build_dir and not fname.startswith('.'):for f in getpy(basepath, os.path.join(parentpath, name), fname, excepts, copyOther, delC):yield felif os.path.isfile(ffile):ext = os.path.splitext(fname)[1]if ext == ".c":if delC and os.stat(ffile).st_mtime > starttime:os.remove(ffile)elif ffile not in excepts and os.path.splitext(fname)[1] not in('.pyc', '.pyx'):if os.path.splitext(fname)[1] in('.py', '.pyx') and not fname.startswith('__'):yield os.path.join(parentpath, name, fname)elif copyOther:dstdir = os.path.join(basepath, build_dir, parentpath, name)if not os.path.isdir(dstdir): os.makedirs(dstdir)shutil.copyfile(ffile, os.path.join(dstdir, fname))else:pass#获取py列表
module_list = list(getpy(basepath=currdir,parentpath=parentpath, excepts=(setupfile)))
try:setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])
except Exception as e:print (e)
else:module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), copyOther=True))
module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), delC=True))
if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir)
print ("complate! time:", time.time()-starttime, 's')

4、在gpu环境下打包的就是so文件,在cpu环境下打包的就是pyc文件,作用是加密源码。

将python项目文件加密相关推荐

  1. python项目加密

    在python项目部署的时候,我们常常会遇到要将python项目加密,本文采取的做法是将python项目编译成so文件,从而达到加密的效果. 对单个python脚本进行加密: 安装Cython 在py ...

  2. Python代码加密,将python文件编译成so文件

    Python程序(.py文件)是公开的,容易被别人拿去使用,为了更好保护知识产权,可以对python文件加密为.so文件. 首先,我们需要在Ubuntu系统中安装一些准备工具,包括python3-de ...

  3. Python代码加密混淆

    python作为一种解释型语言,源代码加密本身比较困难.但有时候我们在发布一款python产品时又必须考虑到代码的加密性,以避免源代码泄露.为此,我查阅了一些资料,研究了几种python代码加密的常见 ...

  4. python基础教程zip密码_python基础教程Python实现加密的RAR文件解压的方法(密码已知)...

    博主之前在网上找了很多资料,发现rarfile库不能直接调用,需要安装unrar模块,下面将详细介绍整个实现流程. 第一步:安装unrar模块,直接pip install unrar可能会找不到库,需 ...

  5. python md5加密_Python MD5加密实例详解

    详解Python MD5加密 Python 3下MD5加密 # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待 ...

  6. python自带的对称算法_一种基于对称算法和专用加载模块的Python程序模块加密方法...

    一种基于对称算法和专用加载模块的Python程序模块加密方法 [专利说明]一种基于对称算法和专用加载模块的Python程序模块加密方法 技术领域 [0001]本发明涉及一种网络安全技术,具体涉及一种P ...

  7. 在Azure Data StudioSQL笔记本中使用Python脚本加密密码

    This article explores the Python scripts for encrypting and decrypting passwords in SQL Server using ...

  8. 使用python下载加密的流媒体m3u8视频文件,获取电影资源

    使用python下载加密的流媒体m3u8视频文件,获取电影资源 m3u8简介 代码示例 获取文件名与m3u8地址 媒体序列解密 视频序列片段下载 完整代码 后记 m3u8简介 M3U8是Unicode ...

  9. python打包加密工具:Pyinstaller和Nuitka

    python打包加密工具概述 参考链接 谈谈 Pyinstaller 的编译和反编译,如何保护你的代码 Linux之Python代码打包工具Nuitka使用说明 Nuitka-Python 打包 Li ...

最新文章

  1. 在 Jenkins 中使用声明式 Pipeline 构建 Android 项目
  2. Android Jetpack组件之Hilt使用
  3. c++判断ftp服务器文件存在性判断_BitTorrent协议与传统文件分发协议对比
  4. matlab入门4-mdlInitializeSizes解析
  5. 程序怎么在matlab运行不了,这个程序在MATLAB 7.0中为什么运行不起来 那个工具箱怎么装...
  6. mysql c#开发库_c# 开发+MySql数据库
  7. centos6 java安装_CentOS6下安装Java JDK8
  8. pytho tkinter 应用第一个窗口
  9. 一个影响我现在生活状况的故事[转载]
  10. 重温经典:Windows1.0系统体验和尝试自己编写Windows1.0系统
  11. 身份证地区码数据表-SqlServer版
  12. Java新特性(一 · JDK1.5)
  13. 3大奇葩排序之猴子算法
  14. Xcode4.6 自制iOS可用的 Framework
  15. 亚马逊巴西站对中国商家正式开放试运营,如何快速提升销量
  16. 甲骨文服务器(Oracle Cloud)开启root用户登录
  17. 2022 上半年 FDA 小分子药物盘点 - MedChemExpres
  18. 后端做app连续会员包月功能 -- IOS连续订阅 支付宝周期扣款
  19. 微信公众号采集,历史文章采集,万能key采集,点赞阅读评论采集
  20. hdu6608 Fansblog(威尔逊定理)

热门文章

  1. stm32 同一个定时器输入捕获测量双通道PWM占空比
  2. watch监听的几种写法
  3. Openjudge 1.1编程基础之输入输出
  4. 互联旅馆项目的经历路线
  5. 全网最详细实战用Xcode开发苹果轻应用(App Clip) 教程
  6. 关于CSeq,RAck,以及CANCEL,ACK的特殊性
  7. L2-018 多项式A除以B (25 分)
  8. Microsoft Office Word 遇到问题需要关闭。我们对此引起的不便表示抱歉。
  9. 三元一次方程组例题_三元一次方程组练习题
  10. html5图片自动翻转,纯js实现360度旋转预览图片特效