1.2. 完整安装教程

1.2.1. 前提条件

Pyarmor 需要 Python 运行动态库以及 C 库,缺少它们 Pyarmor 无法正常启动运行。

在 Linux 平台,如有必要请安装 Python 运行动态库。例如,使用下面的命令安装 Python 3.10 的运行动态库:

$ apt install libpython3.10

在 Darwin 平台,确保文件 @rpath/lib/libpythonX.Y.dylib 存在,这里 X.Y 表示 Python 的版本。例如:

@rpath/lib/libpython3.10.dylib

@rpath 是下列路径之一:

  • @executable_path/..

  • @loader_path/..

  • /System/Library/Frameworks/Python.framework/Versions/3.10

  • /Library/Frameworks/Python.framework/Versions/3.10

如果没有这个文件,请安装必要的包或者使用必要的选项重新编译 Python ,或者使用 install_name_tool 适配当前的 Python 环境,参阅 ../question 中如何解决 Apple 下面的奔溃问题。

1.2.2. 从 PyPI 直接安装

Pyarmor 发布在 PyPI 上面,最方便的方式就是使用命令 pip 直接安装。

在 Linux 和 MacOS,直接打开命令终端并运行下面的命令:

$ pip install pyarmor

在 Windows 环境下,需要使用 Win-r 打开命令输入框,然后输入 cmd 打开命令窗口,并运行下面的命令:

C:\> pip install pyarmor

安装完成之后,输入命令 pyarmor --version 并回车。如果安装成功,会显示安装的 Pyarmor 的版本信息。

如果需要跨平台发布加密脚本,根据需要安装相应平台的辅助运行包:

$ pip install pyarmor.cli.core.windows
$ pip install pyarmor.cli.core.themida
$ pip install pyarmor.cli.core.linux
$ pip install pyarmor.cli.core.darwin
$ pip install pyarmor.cli.core.freebsd
$ pip install pyarmor.cli.core.android

并不是所有的平台都支持 Pyarmor,所有支持的运行平台请查看 生成加密脚本的环境

备注

如果无需使用老版本 Pyarmor 7 的功能,直接安装 pyarmor.cli 而不是安装 pyarmor 可以显著减少下载时间。例如:

$ pip install pyarmor.cli

1.2.2.1. 安装的命令

  • pyarmor 是最重要的一个,所有的工作基本都由它来完成,详细使用方法请参考 命令手册

  • pyarmor-7 是为了和老版本兼容的命令,它等价于 Pyarmor 7.x 的修正版本。

  • pyarmor-auth 是集团版许可证支持运行不受限制 Docker 容器的辅助命令。

1.2.2.2. 使用 Python 解释器直接运行 Pyarmor

pyarmor 等价于下面的命令:

$ python -m pyarmor.cli

1.2.3. 从 Github 上面进行安装

自 8.2.9 版本弃用.

也可以直接从 Pyarmor Github 安装 Pyarmor。下载库到本地,然后使用 pip 进行安装:

$ git clone https://github.com/dashingsoft/pyarmor
$ cd pyarmor
$ pip install .

你可以直接下载一个库的压缩文件 tar.gz 或者 zip ,解压之后在使用 pip 进行安装。

备注

从 8.2.9 开始不推荐使用这种方式进行安装,这种方式可能因为无法正常运行。

1.2.4. 在离线设备上安装

所有的 Pyarmor 需要的包发布在 PyPI ,需要从上面下载必要的包,然后拷贝到离线设备。

首先安装 pyarmor.cli.core

其次安装 pyarmor 或者 pyarmor.cli

例如,在 64位 Linxu 平台下面为 Python 3.10 安装 Pyarmor:

$ pip install pyarmor.cli.core-3.2.9-cp310-none-manylinux1_x86_64.whl
$ pip install pyarmor-8.2.9.zip

在 Android 或者 FreeBSD 系统,因为在包 pyarmor.cli.core 没有预编译的 wheel ,所以需要下载额外的包 pyarmor.cli.core.android 或者 pyarmor.cli.core.freebsd 。例如,在 Android 系统,运行下面的命令离线安装 Pyarmor:

$ pip install pyarmor.cli.core-3.2.9.zip
$ pip install pyarmor.cli.core.android-3.2.9-cp310-none-any.whl
$ pip install pyarmor-8.2.9.zip

如果需要跨平台加密,还需要安装相应的包 pyarmor.cli.core.NAME

例如,需要使用 Themida 保护的运行包,就需要安装:

$ pip install pyarmor.cli.themida-3.2.9-cp310-none-any.whl

在 Linux 平台加密运行在 Windows 平台的包,需要安装:

$ pip install pyarmor.cli.windows-3.2.9-cp310-none-any.whl

如果不需要使用老版本的命令 pyarmor-7 ,推荐安装 pyarmor.cli 而不是 pyarmor ,前者需要下载的文件显著小于后者。例如:

$ pip install pyarmor.cli-8.2.9.zip

1.2.5. Termux 平台的额外补丁

在 Termux 平台,安装完成之后还需要额外对扩展模块打补丁。例如:

$ patchelf --add-needed /data/data/com.termux/files/usr/lib/python3.11/site-packages/pyarmor/cli/core/android/aarch64/pytransform3.so
$ patchelf --add-needed /data/data/com.termux/files/usr/lib/python3.11/site-packages/pyarmor/cli/core/android/aarch64/pyarmor_runtime.so

有时候可以还需要额外的配置。例如:

$ patchelf --set-rpath /data/data/com.termux/files/usr/lib /path/to/{pytransform3,pyarmor_runtime}.so

否则,运行 pyarmor 会报错 dlopen failed: cannot locate symbol "PyFloat_Type"

1.2.6. 在 Python 脚本中调用 Pyarmor

首先创建一个任意的脚本,例如 tool.py

from pyarmor.cli.__main__ import main_entry

args = ['gen', 'foo.py']
main(args)

然后运行这个脚本:

$ python tool.py

以上只是一个示例说明,具体使用的加密选项和参数可以通过各种方式进行传递。

1.2.7. 完全卸载

使用下面的命令可以完全卸载 Pyarmor:

$ pip uninstall pyarmor
$ pip uninstall pyarmor.cli.core

# 下面的包不一定都安装,根据安装情况卸载相应的包

$ pip uninstall pyarmor.cli.runtime
$ pip uninstall pyarmor.cli.core.windows
$ pip uninstall pyarmor.cli.core.themida
$ pip uninstall pyarmor.cli.core.linux
$ pip uninstall pyarmor.cli.core.darwin
$ pip uninstall pyarmor.cli.core.freebsd
$ pip uninstall pyarmor.cli.core.android

$ rm -rf ~/.pyarmor
$ rm -rf ./.pyarmor

备注

~ 是当前登录用户的个人目录,可以通过查看环境变量 HOME 得到具体位置。使用不同用户登陆,路径 ~ 可能不一样。