Skip to main content

Compile Python code to binary using Cython

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 ...

Project details


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.1.tar.gz (12.0 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.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py2dist-0.1.1.tar.gz
  • Upload date:
  • Size: 12.0 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.1.tar.gz
Algorithm Hash digest
SHA256 bb7a244f44caa3537089dea6d78fde00021c7d72e45a29723c97b9e66cee0a71
MD5 88745850445d9aad9cc6579dd6aea0d0
BLAKE2b-256 15c2eb98ea1f401719969bf0f1e374556e11f7cca2a317c0063216849844eea6

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2dist-0.1.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: py2dist-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5cd6a36cc230b88dc002a051acfcd3786485c19525e81a1e09278f2b3851e513
MD5 1efd28e01a78a2bec89c264571843719
BLAKE2b-256 bb681bf8a8026339a2029a5fc05d7b55dbee5fa24ba29c56c05a323d32b699fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for py2dist-0.1.1-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