Skip to main content

py2pydso

PyPI version Python License Documentation Status

Python源文件编译为.pyd/.so原生扩展,以便分发和保护源代码。


✨ Features

  • 🔒 源码保护 — 将 .py 编译为 .pyd/.so 原生扩展,不暴露源码
  • 📦 三种编译模式 — 单文件 / 模块目录 / 完整 wheel 包
  • 🗂️ 智能过滤 — 自动保留 __init__.py 等元文件,支持自定义排除
  • 📝 类型提示 — 可选生成 .pyi 存根文件(--pyi),保留 IDE 补全体验
  • 🌍 跨平台Windows (.pyd) / Linux / macOS (.so) 全支持

安装

pip install py2pydso

安装完成后,可通过以下命令验证:

python -m py2pydso --help

依赖

py2pydso 在构建 wheel 包(package 子命令)时会自动安装项目依赖:

依赖 用途
Cython .py 转译为C代码并编译为原生扩展
setuptools 驱动编译流程
mypy 通过 stubgen 生成 .pyi 类型提示文件(指定 --pyi 时使用)
tomli 解析 pyproject.toml(Python < 3.11 时自动安装)

C 编译器

编译原生扩展需要 C 编译器,请根据平台安装:

  • Windows: Visual Studio Build Tools(勾选 "C++ build tools")
  • macOS: xcode-select --install
  • Linux: sudo apt install build-essential gcc

使用方式

1. 编译文件

# 编译单个文件,输出到源文件同目录
python -m py2pydso file -i demo.py

输出 demo.pyd(或 demo.so),位于 demo.py 同目录。

# 同时编译多个文件
python -m py2pydso file -i demo.py utils.py config.py

每个文件输出到各自源文件所在目录。

# 指定输出目录,所有产物统一输出
python -m py2pydso file -i demo.py utils.py -o output

输出 output/demo.pydoutput/utils.pyd

# 编译并生成 .pyi 类型存根文件
python -m py2pydso file -i demo.py --pyi

输出 demo.pyd + demo.pyi

python -m py2pydso file -i utils/demo.py

输出 utils/demo.pyd(或 utils/demo.so)。

2. 编译模块目录

# 编译单个模块
python -m py2pydso module -i utils -o output

# 同时编译多个模块
python -m py2pydso module -i utils tools helpers -o output

输入:

utils/
  __init__.py
  __main__.py
  tools.py
  config.py

输出(始终输出到 output/<模块名>):

output/
  utils/
    __init__.py      ← 原样保留(__开头)
    __main__.py      ← 原样保留(__开头)
    config.py        ← 原样保留(--exclude-files 指定)
    tools.pyd        ← 编译产物

多模块时每个模块各自独立子目录:

output/
  utils/
    __init__.py
    tools.pyd
  tools/
    __init__.py
    core.pyd
  • __开头的 .py 文件(如 __init__.py__main__.py)原样保留,不参与编译

  • 支持 -i 指定多个模块目录,空格分隔

  • 支持 --exclude-files 指定额外排除的文件(可多个):

    python -m py2pydso module -i utils -o output --exclude-files config.py constants.py
    
  • 支持 --pyi 生成 .pyi 类型存根文件(保留 IDE 补全提示):

    python -m py2pydso module -i utils -o output --pyi
    
  • 支持子目录递归编译

3. 构建完整 wheel 包

python -m py2pydso package -m <module_name>

构建保护版本的 wheel 包(.pyd/.so 原生扩展),不暴露源码。

# 生成 .pyi 类型提示文件(需显式指定)
python -m py2pydso package -m <module_name> --pyi

# 指定输出目录
python -m py2pydso package -m <module_name> -o /path/to/wheelhouse

# 排除指定文件不编译,保留原始 .py
python -m py2pydso package -m <module_name> --exclude-files config.py constants.py

# 加固编译:隐藏内部符号 + 剥离调试信息
python -m py2pydso package -m <module_name> --harden

# 不编译为 .pyd/.so,直接打包源码为纯 Python wheel
python -m py2pydso package -m <module_name> --no-compile

# src 布局:模块在 src 目录下
python -m py2pydso package -m <module_name> --src-dir src

# 额外目录:复制 setup.py 依赖的目录(如 C 源码目录)
python -m py2pydso package -m <module_name> --extra-dirs libwebp third_party

# 显示详细编译输出
python -m py2pydso package -m <module_name> -v

输出:

  • 编译模式:<package_name>-x.x.x-cpYY-cpYY-<platform>.whl
  • --no-compile 模式:<package_name>-x.x.x-py3-none-any.whl

公共参数

参数 适用命令 说明
-m / --module package 要编译的模块目录名(必填),对应项目下的同名目录
--src-dir package 源码根目录(默认 .),如 src 布局使用 --src-dir src
--extra-dirs package 额外需要复制到临时目录的目录名(可多个),如 setup.py 依赖的 C 源码目录:--extra-dirs libwebp
-i / --input filemodule 输入路径(均可指定多个,空格分隔);file.py 文件,module 为模块目录
--exclude-files modulepackage 排除不编译的文件(可多个),按相对路径匹配,保留原始 .py。如 build.py 仅排除包根目录的文件,demo/build.py 排除子目录的文件
--pyi filemodulepackage 生成 .pyi 类型存根文件(默认不生成)
--harden filemodulepackage 加固编译:隐藏内部符号(仅导出 PyInit_*)+ 剥离调试信息,增加逆向难度
--no-compile package 不编译为 .pyd/.so,直接打包源码为纯 Python wheel(py3-none-any),跨平台通用
-v / --verbose package 显示详细编译输出(CMake、Cython、编译器日志),调试问题时使用

--harden 加固说明

--harden 后,编译产物会经过两层加固:

层级 GCC / Clang(Linux / macOS) MSVC(Windows)
符号隐藏 version script,仅导出 PyInit_* 无编译期支持,依赖 strip
符号剥离 strip --strip-unneeded 需手动安装 MinGW strip,未安装则跳过

效果:

  • nm / dumpbin /exports 查看 .pyd/.so 时,内部函数符号全部消失
  • 文件体积显著减小(去除调试段)
  • 逆向工具无法通过符号表快速定位函数

注意--harden 不影响 Python 层的功能,模块加载和调用完全正常。

命令行帮助

python -m py2pydso --help
python -m py2pydso file --help
python -m py2pydso module --help
python -m py2pydso package --help

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

py2pydso-1.4.5-cp314-cp314-win_amd64.whl (238.4 kB view details)

Uploaded CPython 3.14Windows x86-64

py2pydso-1.4.5-cp314-cp314-manylinux_2_17_x86_64.whl (366.9 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

py2pydso-1.4.5-cp313-cp313-win_amd64.whl (234.7 kB view details)

Uploaded CPython 3.13Windows x86-64

py2pydso-1.4.5-cp313-cp313-manylinux_2_17_x86_64.whl (369.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

py2pydso-1.4.5-cp312-cp312-win_amd64.whl (235.6 kB view details)

Uploaded CPython 3.12Windows x86-64

py2pydso-1.4.5-cp312-cp312-manylinux_2_17_x86_64.whl (367.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

py2pydso-1.4.5-cp311-cp311-win_amd64.whl (243.7 kB view details)

Uploaded CPython 3.11Windows x86-64

py2pydso-1.4.5-cp311-cp311-manylinux_2_17_x86_64.whl (385.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

py2pydso-1.4.5-cp310-cp310-win_amd64.whl (243.1 kB view details)

Uploaded CPython 3.10Windows x86-64

py2pydso-1.4.5-cp310-cp310-manylinux_2_17_x86_64.whl (379.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

py2pydso-1.4.5-cp39-cp39-win_amd64.whl (265.2 kB view details)

Uploaded CPython 3.9Windows x86-64

py2pydso-1.4.5-cp39-cp39-manylinux_2_17_x86_64.whl (379.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

py2pydso-1.4.5-cp38-cp38-win_amd64.whl (273.8 kB view details)

Uploaded CPython 3.8Windows x86-64

py2pydso-1.4.5-cp38-cp38-manylinux_2_17_x86_64.whl (381.4 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file py2pydso-1.4.5-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.4.5-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 238.4 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for py2pydso-1.4.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 18bd91f2438415a372e5f73eba43adc0c8626eefb4595928e24be73b5d43da3b
MD5 4bdbd975904649923cbe64b14bc1b830
BLAKE2b-256 7abdcc19f68ab5a7b64202704e04e321555355781dbea7856ae9de4c6b50de6c

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp314-cp314-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.4.5-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 b2fdb9f2adc005a37a4be8bd0027d1bf20cc0e201bc4df3b6a2ab585e1489021
MD5 c12ac91e44283b4eb786161df9be68c9
BLAKE2b-256 cb0efe3f6d49df7892f2252817c4736fb9420b90c34214de0bc74befa38ed4df

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp314-cp314-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.4.5-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 234.7 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for py2pydso-1.4.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b943b661a2c665fee6994d16dbf7592c4eb16cdeed1ea0dfa003ddcfb8f52d33
MD5 3c6ed4f39a337df0b7d251ebb7350b71
BLAKE2b-256 bc6c48818ae3c3cf50e0317050e23206a6af2480146f37287bc995c6e8ee494e

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp313-cp313-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.4.5-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 e75cbb640a605c5b27aa42f9b2999b0b99a82ab7ef38a37b24eb16c3cd456d14
MD5 22ba1e96133b09ba0682ecfc32b287fe
BLAKE2b-256 cb04300c0eb955dcb0c3150801959e5a2538ba7dc336aae6792d2fb96349d017

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp313-cp313-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.4.5-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 235.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for py2pydso-1.4.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fce53a8eade8a760ecdfc7642e507f02c43aec8a36d5496238764331350550c1
MD5 c386fec784d0214dc6b8540daf0b1d85
BLAKE2b-256 b1dd57949815ebaef130bf8ae0446e6106002c819b5bc247dd43ead85b28b1df

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp312-cp312-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.4.5-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 f73d932b4046c374c893fe227f51b3107e2436600db8037d24714a8642a5f7b4
MD5 a0540cba94f2ab6f6ed4c3b57c50f09d
BLAKE2b-256 af8c4a16af6cc4bb05fd5f79c15571ec6e572ca64dc7f7f81982984c3ed94738

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp312-cp312-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.4.5-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 243.7 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for py2pydso-1.4.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8ec2fccaaa62bcad9868a0b7805ba7f03594ec8c01c5cdba26898aa8ea6fb13e
MD5 a540cd07bf7fed0d4aae32489396d176
BLAKE2b-256 64d44de759502799ddabd7321729db01c3f9fd64796c7b89eabf2b0bce75def6

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp311-cp311-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.4.5-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 1274a07187fed52ed55ce585b81d306cc4119d9dbb30d2ec7bc11861584cb871
MD5 6406998dbfac69657d6baa4746cba992
BLAKE2b-256 ac971ef81f0d275c75ed83e61a91fe49396c3b603849617f7f1a4e6aa89447ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp311-cp311-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.4.5-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 243.1 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for py2pydso-1.4.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a1f05ab299d6c64a73f4a6c8ddfd2f36152cc9aecff87352e8acc843df39723a
MD5 8d6eb336136fa2bd72beafb25b2bbf3f
BLAKE2b-256 746d1282896b24ab55dadf94a7f9872fe91c2d68e0c4098c1ca4e3d0382f09ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp310-cp310-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.4.5-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 dfe9499f873af0c64148e2622cbe1544daae81a2cde765c5fc79b9fc0cf217b1
MD5 762cb459bc1328fd95b4f9d6a126f757
BLAKE2b-256 a8bb01c95d681cc700dea676d622ae6c96c1118cc316746310cb5765eff2d230

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp310-cp310-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.4.5-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 265.2 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for py2pydso-1.4.5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 06024b8b5e6e4b8fea9fa4915b4dd98b846ee6c81122e963aff49638d4fe2135
MD5 c3e00ec076836673a9715404481353d3
BLAKE2b-256 36214224ecb61bfa524e232d5b767aedca7ba311417bf1ee4b4c0579c864e919

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp39-cp39-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.4.5-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 f31675b98f6549111cb9838d77d73b9ffd16fbc461253e9f96f4fae6298edfc2
MD5 8f0b8851dc16dcd652480cc63c816059
BLAKE2b-256 1ae10890f64807c21564264ed73859426dc81d3000c15ca3173be4b8444400b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp39-cp39-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.4.5-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 273.8 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for py2pydso-1.4.5-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 c5bd46cb06c2372cdbc6126407518fdbae23b59703f2af8b9902463862492bde
MD5 a2fa6fcc45164b5755c6d2bfc4409954
BLAKE2b-256 c7f7e238ed0029d75f47c91666088f643aeb21e658de44e63c99320341d39707

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp38-cp38-win_amd64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py2pydso-1.4.5-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.4.5-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3f2e3f4207011a8d66a07a8d94596e84ca6837dbb3b7201bed70f9e74fc0f427
MD5 92449292a4d5fd8e9f9cbae22f93ac7b
BLAKE2b-256 6730f1596737b8f627a917cadd443ddee358ed3b3fc2492b6954c77ef9963d22

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2pydso-1.4.5-cp38-cp38-manylinux_2_17_x86_64.whl:

Publisher: wheels.yml on zhenzi0322-package/py2pydso

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.4.5 This release

14 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page