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.pyd、output/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 file、module 输入路径(均可指定多个,空格分隔);file 为 .py 文件,module 为模块目录
--exclude-files module、package 排除不编译的文件(可多个),按相对路径匹配,保留原始 .py。如 build.py 仅排除包根目录的文件,demo/build.py 排除子目录的文件
--pyi file、module、package 生成 .pyi 类型存根文件(默认不生成)
--harden file、module、package 加固编译:隐藏内部符号(仅导出 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

Metadata

Release files for py2pydso 1.4.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for py2pydso 1.4.5
File
py2pydso-1.4.5-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
py2pydso-1.4.5-cp314-cp314-manylinux_2_17_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ x86-64 Details
py2pydso-1.4.5-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
py2pydso-1.4.5-cp313-cp313-manylinux_2_17_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
py2pydso-1.4.5-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
py2pydso-1.4.5-cp312-cp312-manylinux_2_17_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
py2pydso-1.4.5-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
py2pydso-1.4.5-cp311-cp311-manylinux_2_17_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
py2pydso-1.4.5-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
py2pydso-1.4.5-cp310-cp310-manylinux_2_17_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
py2pydso-1.4.5-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
py2pydso-1.4.5-cp39-cp39-manylinux_2_17_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details
py2pydso-1.4.5-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
py2pydso-1.4.5-cp38-cp38-manylinux_2_17_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details

Total release size: 4.4 MB

Release files / py2pydso-1.4.5-cp314-cp314-win_amd64.whl

Download URL py2pydso-1.4.5-cp314-cp314-win_amd64.whl
Size 238.4 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
18bd91f2438415a372e5f73eba43adc0c8626eefb4595928e24be73b5d43da3b
BLAKE2b-256 checksum
How to use checksums
7abdcc19f68ab5a7b64202704e04e321555355781dbea7856ae9de4c6b50de6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp314-cp314-manylinux_2_17_x86_64.whl

Download URL py2pydso-1.4.5-cp314-cp314-manylinux_2_17_x86_64.whl
Size 366.9 kB
Tags CPython 3.14 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b2fdb9f2adc005a37a4be8bd0027d1bf20cc0e201bc4df3b6a2ab585e1489021
BLAKE2b-256 checksum
How to use checksums
cb0efe3f6d49df7892f2252817c4736fb9420b90c34214de0bc74befa38ed4df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp313-cp313-win_amd64.whl

Download URL py2pydso-1.4.5-cp313-cp313-win_amd64.whl
Size 234.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
b943b661a2c665fee6994d16dbf7592c4eb16cdeed1ea0dfa003ddcfb8f52d33
BLAKE2b-256 checksum
How to use checksums
bc6c48818ae3c3cf50e0317050e23206a6af2480146f37287bc995c6e8ee494e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp313-cp313-manylinux_2_17_x86_64.whl

Download URL py2pydso-1.4.5-cp313-cp313-manylinux_2_17_x86_64.whl
Size 369.2 kB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e75cbb640a605c5b27aa42f9b2999b0b99a82ab7ef38a37b24eb16c3cd456d14
BLAKE2b-256 checksum
How to use checksums
cb04300c0eb955dcb0c3150801959e5a2538ba7dc336aae6792d2fb96349d017
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp312-cp312-win_amd64.whl

Download URL py2pydso-1.4.5-cp312-cp312-win_amd64.whl
Size 235.6 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
fce53a8eade8a760ecdfc7642e507f02c43aec8a36d5496238764331350550c1
BLAKE2b-256 checksum
How to use checksums
b1dd57949815ebaef130bf8ae0446e6106002c819b5bc247dd43ead85b28b1df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp312-cp312-manylinux_2_17_x86_64.whl

Download URL py2pydso-1.4.5-cp312-cp312-manylinux_2_17_x86_64.whl
Size 367.8 kB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
f73d932b4046c374c893fe227f51b3107e2436600db8037d24714a8642a5f7b4
BLAKE2b-256 checksum
How to use checksums
af8c4a16af6cc4bb05fd5f79c15571ec6e572ca64dc7f7f81982984c3ed94738
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp311-cp311-win_amd64.whl

Download URL py2pydso-1.4.5-cp311-cp311-win_amd64.whl
Size 243.7 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
8ec2fccaaa62bcad9868a0b7805ba7f03594ec8c01c5cdba26898aa8ea6fb13e
BLAKE2b-256 checksum
How to use checksums
64d44de759502799ddabd7321729db01c3f9fd64796c7b89eabf2b0bce75def6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp311-cp311-manylinux_2_17_x86_64.whl

Download URL py2pydso-1.4.5-cp311-cp311-manylinux_2_17_x86_64.whl
Size 385.8 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
1274a07187fed52ed55ce585b81d306cc4119d9dbb30d2ec7bc11861584cb871
BLAKE2b-256 checksum
How to use checksums
ac971ef81f0d275c75ed83e61a91fe49396c3b603849617f7f1a4e6aa89447ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp310-cp310-win_amd64.whl

Download URL py2pydso-1.4.5-cp310-cp310-win_amd64.whl
Size 243.1 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
a1f05ab299d6c64a73f4a6c8ddfd2f36152cc9aecff87352e8acc843df39723a
BLAKE2b-256 checksum
How to use checksums
746d1282896b24ab55dadf94a7f9872fe91c2d68e0c4098c1ca4e3d0382f09ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp310-cp310-manylinux_2_17_x86_64.whl

Download URL py2pydso-1.4.5-cp310-cp310-manylinux_2_17_x86_64.whl
Size 379.5 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
dfe9499f873af0c64148e2622cbe1544daae81a2cde765c5fc79b9fc0cf217b1
BLAKE2b-256 checksum
How to use checksums
a8bb01c95d681cc700dea676d622ae6c96c1118cc316746310cb5765eff2d230
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp39-cp39-win_amd64.whl

Download URL py2pydso-1.4.5-cp39-cp39-win_amd64.whl
Size 265.2 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
06024b8b5e6e4b8fea9fa4915b4dd98b846ee6c81122e963aff49638d4fe2135
BLAKE2b-256 checksum
How to use checksums
36214224ecb61bfa524e232d5b767aedca7ba311417bf1ee4b4c0579c864e919
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp39-cp39-manylinux_2_17_x86_64.whl

Download URL py2pydso-1.4.5-cp39-cp39-manylinux_2_17_x86_64.whl
Size 379.9 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
f31675b98f6549111cb9838d77d73b9ffd16fbc461253e9f96f4fae6298edfc2
BLAKE2b-256 checksum
How to use checksums
1ae10890f64807c21564264ed73859426dc81d3000c15ca3173be4b8444400b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp38-cp38-win_amd64.whl

Download URL py2pydso-1.4.5-cp38-cp38-win_amd64.whl
Size 273.8 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
c5bd46cb06c2372cdbc6126407518fdbae23b59703f2af8b9902463862492bde
BLAKE2b-256 checksum
How to use checksums
c7f7e238ed0029d75f47c91666088f643aeb21e658de44e63c99320341d39707
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Release files / py2pydso-1.4.5-cp38-cp38-manylinux_2_17_x86_64.whl

Download URL py2pydso-1.4.5-cp38-cp38-manylinux_2_17_x86_64.whl
Size 381.4 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
3f2e3f4207011a8d66a07a8d94596e84ca6837dbb3b7201bed70f9e74fc0f427
BLAKE2b-256 checksum
How to use checksums
6730f1596737b8f627a917cadd443ddee358ed3b3fc2492b6954c77ef9963d22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

Security for py2pydso 1.4.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 14 files in this release have been uploaded with provenance metadata.

Select from 14 files

Attestation for py2pydso-1.4.5-cp314-cp314-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.4.5 This release

14 release files Full provenance

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