Cpython编译后再使用Pyinstaller打包

一、Cpython

Python是一门解释型语言,当我们想让其他人运行我们的代码时,如果直接将.py源代码发送给他人,那么源代码将没有任何安全性可言,也就是任何一个人都可以打开源代码一看究竟,任何人都可以随意修改源代码。

而为了防止源代码泄露,可以将Python源代码编译生成.pyd库文件或者.so库文件:Windows平台生成pyd文件,Linux生成so文件。

1.1 Python有以下几种类型的文件

py:Python控制台程序的源代码文件
pyw:Python带用户界面的源代码文件
pyx:Python包源文件
pyc:Python字节码文件(可通过逆向编译来得到源码)
pyo:Python优化后的字节码文件(可通过逆向编译来得到源码)
pyd:在Windows平台上Python的库文件(Python版DLL)
so:在Linux平台上Python的库文件是so文件

1.2 使用Cpython编译项目步骤

example:
hello.py

def say_hello():
    print("Hello!")

1.2.1 安装Cpython

pip3 install Cython

1.2.2 编写转换文件

setup.py

from setuptools import setup
from Cython.Build import cythonize


# python3 setup.py build_ext --inplace


# 所有需要编译的py文件
all_py_file = ['hello.py']

setup(
    name="hello",
    ext_modules=cythonize(all_py_file),
    version="1.0",
    author="Leo",
    author_email="LeoLi.Li@groupm.com"
)
1.2.3 执行转换生成so文件

进入目录
在这里插入图片描述

执行:python3 setup.py build_ext --inplace
在这里插入图片描述
在这里插入图片描述

1.2.4 测试编译好的so文件

将py文件等相关编译文件删除
在这里插入图片描述

测试是否可以正常导入并使用hello.py的函数
在这里插入图片描述

1. 3yd/so文件反编译?

pyd/so文件是由 Cython首先把python源码翻译成了 .c文件(这个过程基本不可逆),再把这个.c文件编译成了pyd/so文件。

二、 Pyinstaller

pyinstaller是一个第三方库,它能够在Windows、Linux、 Mac OS X 等操作系统下将 Python 源文件打包,通过对源文件打包, Python 程序可以在没有安装 Python 的环境中运行,也可以作为一个 独立文件方便传递和管理。

Pyinstaller打包的可执行文件并不是跨平台的,而是希望在哪个平台上运行就需要在哪个平台上进行打包。

安装pyinstaller:

python3 -m pip install --no-cache-dir pyinstaller -i https://mirrors.aliyun.com/pypi/simple/;

2.1 使用pyinstaller打包 py文件

项目目录
在这里插入图片描述

main.py

from hello import say_hello


if __name__ == '__main__':
say_hello()


打包

pyinstaller  --name say_hello --onedir --log-level WARN --strip --paths /leo/gme/pyinstaller_demo --distpath /leo/gme/pyinstaller_demo/package /leo/gme/pyinstaller_demo/main.py

执行打包后的可执行文件:
在这里插入图片描述

2.2 使用pyinstaller打包 Cpython编译后的so

Cpython可以将py编译成so文件,将编译好的so文件以原来的工程组织形式(module)存放好,注意module下要有非编译的__init__.py, 工程的main.py也不要编译

pyinstaller的打包过程会从main.py走一遍所有调用的module,并打包进去,但是编译好的pyd不会被识别import,这就是为什么要保留原来module的__init__.py, 对于这些已经编译为so的module,属于隐式import,需要在打包时加入–hidden-import

项目目录
在这里插入图片描述

main.py

from hello import say_hello


if __name__ == '__main__':
    say_hello()


打包

pyinstaller   --hidden-import "hello" --name say_hello --onedir  --log-level WARN   --strip --paths /leo/gme/pyinstaller_demo --distpath /leo/gme/pyinstaller_demo/package   /leo/gme/pyinstaller_demo/main.py

执行打包后的可执行文件:
在这里插入图片描述

_internal 依赖的是so文件:
在这里插入图片描述

三、参考资料

Cpython编译:https://www.cnblogs.com/gcgc/p/16529975.html
Pyinstaller介绍:https://blog.csdn.net/weixin_45953322/article/details/128774685
Pyinstaller打包so文件 https://blog.csdn.net/weixin_39916966/article/details/130781599

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CPythonPython 的官方实现,它是用 C 语言实现的。如果你安装了 Python,那么你已经安装了 CPython。你可以在命令行中输入 `python` 来启动 CPython 解释器。 使用 CPython,你可以编写 Python 代码,并执行它们。你还可以使用 C 或 C++ 编写 Python 扩展模块,以扩展 Python 的功能。为了编写扩展模块,你需要使用 Python 的 C API,这些 API 允许你在 C 或 C++ 中操作 Python 对象。 下面是一个使用 CPython 的示例: ```python # hello.py print("Hello, world!") ``` 你可以在命令行中使用以下命令来执行 hello.py: ``` $ python hello.py Hello, world! ``` 如果你想编写 Python 扩展模块,那么你需要使用 CPython 的 C API。一个简单的示例是使用 CPython 的 C API 来编写一个函数,将两个 Python 整数相加,并返回结果。 ```c // add.c #include <Python.h> static PyObject *add(PyObject *self, PyObject *args) { int a, b; if (!PyArg_ParseTuple(args, "ii", &a, &b)) { return NULL; } return PyLong_FromLong(a + b); } static PyMethodDef methods[] = { {"add", add, METH_VARARGS, "Add two integers."}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef module = { PyModuleDef_HEAD_INIT, "my_module", "My custom module.", -1, methods }; PyMODINIT_FUNC PyInit_my_module(void) { return PyModule_Create(&module); } ``` 你可以使用以下命令将其编译为共享库: ``` $ gcc -shared -o my_module.so add.c $(python3-config --ldflags) ``` 然后,你可以在 Python使用它: ```python # main.py import my_module result = my_module.add(1, 2) print(result) ``` 你可以在命令行中使用以下命令来执行 main.py: ``` $ python main.py 3 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值