Skip to main content

Compile Python code to binary for distribution

Project description

py2dist

中文文档

py2dist is a tool that uses Cython to compile Python source code into binary extension modules (.so/.pyd), primarily for source code protection.

Features

  • Compile single .py files or entire directories into binary files.
  • Preserve directory structure.
  • Support excluding specific files or directories.
  • Automatically detect and use ccache to accelerate compilation.
  • Provide both CLI and Python API.

Installation

pip install py2dist

Usage

Command Line Interface (CLI)

Compile a single file:

py2dist -f myscript.py

Compile an entire directory:

py2dist -d myproject -o dist

Arguments:

  • -f, --file: Specify a single .py file to compile.
  • -d, --directory: Specify the directory to compile.
  • -o, --output: Output directory (default is dist).
  • -m, --maintain: Files or directories to exclude (comma-separated).
  • -x, --nthread: Number of compilation threads (default is 1).
  • -q, --quiet: Quiet mode.
  • -r, --release: Release mode (cleans up temporary build files).
  • -c, --ccache: Use ccache (auto-detect by default, or specify path).

Python API

from py2dist import compile_file, compile_dir

# Compile a single file
compile_file("myscript.py", output_dir="dist")

# Compile a directory
compile_dir(
    "myproject",
    output_dir="dist",
    exclude=["tests", "setup.py"],
    nthread=4
)

⚠️ Important Note: Python Version Consistency

The compiled binary extension modules (.so/.pyd) are bound to a specific Python version. You must ensure that the Python version used for compilation is exactly the same as the Python version used at runtime (including minor version numbers; for example, 3.10 and 3.11 are incompatible).

If the versions do not match, you may encounter errors like the following when importing the module: ImportError: ... undefined symbol: _PyThreadState_UncheckedGet or ModuleNotFoundError: No module named ...

Download files

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

Source Distribution

py2dist-0.1.2.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

py2dist-0.1.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file py2dist-0.1.2.tar.gz.

File metadata

  • Download URL: py2dist-0.1.2.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for py2dist-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f5330e9223e279dfbf2095cb324cd8f7975a2c351174b84c3628658c0298ed78
MD5 b6a25688c4116ade74440703ead68dc5
BLAKE2b-256 b92679ceffc07829d868446218afd7da1b8f829a8aff6940d095f1edc3363ae0

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2dist-0.1.2.tar.gz:

Publisher: publish.yml on xxnuo/py2dist

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

File details

Details for the file py2dist-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: py2dist-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for py2dist-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cc1829d0177727d4daabeccbba6792ea0755126ab9a2f64535a91fa2f6ee89bd
MD5 94fdcb755acb3ffd8b3323a5dd2bf846
BLAKE2b-256 4a06e101acae0347cd29114f87dc304e328522dae54ca1879a46458f9eaa1543

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2dist-0.1.2-py3-none-any.whl:

Publisher: publish.yml on xxnuo/py2dist

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