Skip to main content

Compile Python source files into .pyd or .so extension modules for distribution and source protection.

Project description

py2pydso

Compile Python source files into .pyd / .so native extensions for distribution and source protection.

PyPI version Python License


✨ Features

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

安装

pip install py2pydso

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

python -m py2pydso --help

依赖

py2pydso 会自动安装以下 Python 依赖:

依赖 用途
Cython .py 转译为C代码并编译为原生扩展
setuptools 驱动编译流程
mypy 通过 stubgen 生成 .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 -o output

输出 output/demo.pyd

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

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

2. 编译整个模块目录

python -m py2pydso module -i utils -o output

输入:

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

输出:

output/
  utils/
    __init__.py      ← 原样保留(__开头)
    __main__.py      ← 原样保留(__开头)
    config.py        ← 原样保留(--exclude-files 指定)
    tools.pyd        ← 编译产物
  • __开头的 .py 文件(如 __init__.py__main__.py)原样保留,不参与编译
  • 支持 --exclude-files 指定额外排除的文件(可多个):
    python -m py2pydso module -i utils -o output --exclude-files config.py constants.py
    
  • 支持子目录递归编译

3. 构建完整 wheel 包

要求setup.py中要使用ext_modules。示例如下:

import re
from setuptools import setup, Extension, find_packages
from pathlib import Path

this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text(encoding="utf-8")


def _collect_ext_modules() -> list:
    """收集 loki_service 下所有 .py 文件(排除 __init__.py),声明为 Cython 扩展"""
    pkg_dir = this_directory / "loki_service"
    modules = []
    for py_file in sorted(pkg_dir.rglob("*.py")):
        if py_file.name == "__init__.py":
            continue
        rel = py_file.relative_to(this_directory).with_suffix("")
        module_name = str(rel).replace("/", ".").replace("\\", ".")
        modules.append(Extension(module_name, [str(py_file.relative_to(this_directory))]))
    return modules

# 元数据和依赖统一由 pyproject.toml 管理,setup.py 仅负责 Cython ext_modules
setup(
    version="1.0.0",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=find_packages(),
    ext_modules=_collect_ext_modules(),
)
python -m py2pydso package --package-name loki_service --keep-tmp

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

# 跳过 stubgen,异常时保留临时目录用于调试
python -m py2pydso package --package-name loki_service --skip-stubgen --keep-tmp

# 指定输出目录
python -m py2pydso package --package-name loki_service --output-dir /path/to/wheelhouse

输出:<package_name>-x.x.x-cpYY-cpYY-<platform>.whl

公共参数

参数 说明
--skip-stubgen 要是带上参数--skip-stubgen表示不需要生成pyi文件.
--keep-tmp 保留临时编译目录,用于调试

命令行帮助

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

Project details


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.2.3-cp314-cp314-win_amd64.whl (132.4 kB view details)

Uploaded CPython 3.14Windows x86-64

py2pydso-1.2.3-cp314-cp314-manylinux_2_17_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

py2pydso-1.2.3-cp313-cp313-win_amd64.whl (130.0 kB view details)

Uploaded CPython 3.13Windows x86-64

py2pydso-1.2.3-cp313-cp313-manylinux_2_17_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

py2pydso-1.2.3-cp312-cp312-win_amd64.whl (129.8 kB view details)

Uploaded CPython 3.12Windows x86-64

py2pydso-1.2.3-cp312-cp312-manylinux_2_17_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

py2pydso-1.2.3-cp311-cp311-win_amd64.whl (135.0 kB view details)

Uploaded CPython 3.11Windows x86-64

py2pydso-1.2.3-cp311-cp311-manylinux_2_17_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

py2pydso-1.2.3-cp310-cp310-win_amd64.whl (134.4 kB view details)

Uploaded CPython 3.10Windows x86-64

py2pydso-1.2.3-cp310-cp310-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

py2pydso-1.2.3-cp39-cp39-win_amd64.whl (145.4 kB view details)

Uploaded CPython 3.9Windows x86-64

py2pydso-1.2.3-cp39-cp39-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

py2pydso-1.2.3-cp38-cp38-win_amd64.whl (148.5 kB view details)

Uploaded CPython 3.8Windows x86-64

py2pydso-1.2.3-cp38-cp38-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: py2pydso-1.2.3-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 132.4 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py2pydso-1.2.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 1a18c7659ace9ac111ea85315ab264c0444320e4e6d350ce2a51bc8c7b558f04
MD5 5880e739956d63fd19a54d571c0b3883
BLAKE2b-256 320dc7464664748ddd4b694f4691de5f7d7e4c57cc7538a3608d4b7f8f9b34c2

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.2.3-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 2ef0d58dd158a96e56360e7ec4a04fa639c609b082bf627947709b8adaff5a01
MD5 8df1bd6087098da936f1aeb449d129eb
BLAKE2b-256 cf0329546f004f6221e31f529334f3ac90bdcd9904bfb0eabc6c897c8cf16a45

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.2.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 130.0 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py2pydso-1.2.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c8dff6ffe2b1730cb958bdc692898dd2954ef365c23576fa008832f6ae6a3457
MD5 27b4451eb545bfc9227d81d91761fb6c
BLAKE2b-256 12d52fa346b970d478d85206f463236fc2d6097b83e08f1f8ec0824e01e7234e

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.2.3-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4be49dabcb27213a9bdb467a970722cbe72cfad70dc42468b892fd5a0d506459
MD5 d8d687d0dc353887ea208c7b8f595946
BLAKE2b-256 19af4b15ecf8bbb5c08903dc544daa0489415f8f28c7962e7c82956f83124ed2

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.2.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 129.8 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py2pydso-1.2.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7b069456121e98314584449e8df5d9370c96f041ed574b1ec803f7c5327360db
MD5 12e136df3d2376257f816425e83a8a6d
BLAKE2b-256 373bd676e3e73d1b3d29692d0399d23bb76b4ce2ab0621e171bfae14aabe665e

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.2.3-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3c8eeba81d8c4057cc354bfa6393107b0dbe119e8d1c1f5aa80fbc6970433b0b
MD5 2c0d21cb0b2b62d33c854226ed7b1a6f
BLAKE2b-256 fd680680fa43d876e4c5cdf1412870cca0cd6872261470c173937bd35f1b6880

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.2.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 135.0 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py2pydso-1.2.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 183c2e46b81797cde8a6cd0a629ecb8fe8ed1f83e9d49837fc2fb2785ece8518
MD5 d2ed95927f8d67004e705b97137c5145
BLAKE2b-256 28f4defac867f38a34ad1c94af0ed65d939cba02de38079bf20e02b6c6fe3bf5

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.2.3-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 01c76b83e030aaf383be081c5d338182b54f498d1a936c35a7229e040b4ae395
MD5 35ab4420478a8c208fe8a990da3212ec
BLAKE2b-256 b34551303c0ae640e29b76cdf11ae7f61412e3929962eddfb13f31bdc544dc8f

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.2.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 134.4 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py2pydso-1.2.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0e69cc72b3ac18e0eb435c406c58828cc673d77386ccf5307d738682a6b0b55e
MD5 0716cf490d30441b3827ba52a60c0073
BLAKE2b-256 567a8c4cf5ea168df97a229de2ec90c1a1b09ab7efb75b577e07b2ed0f52eed5

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.2.3-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 626ee280a61eb5ab0820fa1190a794e65e18bbc6eae86216f2c7da75adb028cc
MD5 c1e1ddd37a591ee1eb87643f8b420e53
BLAKE2b-256 ee583f24ccd686e3d4faff80336523627d912376ea0f21eb5e24e45890658a21

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.2.3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 145.4 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py2pydso-1.2.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 2e4b26d2b4877a9bb61cecfd52934502af83e1b8513038f3ec2114bc5ab1ba6d
MD5 5cc43d27ea98debb9cd48f7c10d97a92
BLAKE2b-256 0b5798797c72cf5946ab66c585c230e1dacc99a934450f1e29d7655deba3d2f0

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.2.3-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3bfbeb7ced2c33c182c835da74ed0b0f506929a93bdd70b68cf60cd685babf53
MD5 e065fd70123feee2ba9da479a3dca61a
BLAKE2b-256 e8aaff9bd54ac72687358f922c35fc68bcb010f69da48b905792612d321a7df5

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: py2pydso-1.2.3-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 148.5 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py2pydso-1.2.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 5aa09684f6fff52ea6fe935c39c22280fd929dd9029bf64472ca937a28134a6a
MD5 ee48219b18da9257a66844241420acb6
BLAKE2b-256 e655c5175c1af3b08bd23475e907e461ba7bb56728840b5a1b9d4eac32722f50

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/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.2.3-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py2pydso-1.2.3-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 8469ba61df9ad5ef33c428c3240c7fe64ea3576bb381e63f666d301b95ccc5fe
MD5 aef95076b3d1d54e831f7c06e7db53fe
BLAKE2b-256 388519427878b083f0e0cfc696458437084051f4f15ccbca956289f0adeb0602

See more details on using hashes here.

Provenance

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

Publisher: wheels.yml on zhenzi0322/py2pydso

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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page