Skip to main content

Python to Private Executable Converter - Encrypt Python scripts to standalone executables

Project description

๐Ÿ” p2private

Python 3.11+ Cython License: MIT PyPI

Python to Private Executable Converter
Encrypt your Python scripts to standalone executables using Cython


๐Ÿ“‹ Table of Contents


โœจ Features

  • ๐Ÿ”’ Encrypt Python scripts to C code using Cython
  • ๐Ÿš€ Generate standalone executables that compile on first run
  • ๐Ÿ›ก๏ธ Optional version checking for Python compatibility
  • ๐Ÿ“ Custom output naming and directory support
  • ๐Ÿงน Automatic cleanup of temporary files
  • ๐Ÿ’ป Cross-platform support (Linux, macOS, Windows)
  • ๐ŸŽฏ Simple CLI and Python API
  • ๐Ÿ”ง Auto-fixes mixed tabs/spaces indentation issues
  • โœ… Python 3.11+ compatible - no deprecated warnings

๐Ÿ“ฆ Installation

From PyPI (Recommended)

pip install p2private

From Source

git clone https://github.com/pooraddy/p2private.git
cd p2private
pip install -e .

Development Installation

git clone https://github.com/pooraddy/p2private.git
cd p2private
pip install -e ".[dev]"

๐Ÿš€ Quick Start

1. Create a Python Script

# hello.py
print("Hello, World!")
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")

2. Encrypt It

p2private hello.py

3. Run the Encrypted File

python Enc_hello.py

๐Ÿ’ป CLI Usage

Basic Encryption

p2private script.py

Custom Output Name

p2private script.py -o myapp

Specify Output Directory

p2private script.py -d ./output

Add Version Check

p2private script.py -v 3.11

Full Example

p2private script.py -o myapp -d ./build -v 3.12

CLI Options

Option Short Description Example
--output -o Custom output filename -o myapp
--directory -d Output directory -d ./build
--version-check -v Python version requirement -v 3.11
--version - Show version --version
--help -h Show help --help

๐Ÿ Python API

Method 1: Using P2PrivateEncryptor Class

from p2private import P2PrivateEncryptor

encryptor = P2PrivateEncryptor()
output_path = encryptor.encrypt('script.py')
print(f"Encrypted file: {output_path}")

Method 2: With Version Check

from p2private import P2PrivateEncryptor

encryptor = P2PrivateEncryptor(version_check='3.11')
output_path = encryptor.encrypt('script.py')

Method 3: Custom Output

from p2private import P2PrivateEncryptor

encryptor = P2PrivateEncryptor()
output_path = encryptor.encrypt(
    file_path='script.py',
    output_dir='./build',
    output_name='myapp.py'
)

Method 4: Using encrypt_file Function

from p2private import encrypt_file

encrypt_file('script.py')

Method 5: encrypt_file with Options

from p2private import encrypt_file

encrypt_file(
    file_path='script.py',
    output_dir='./build',
    output_name='myapp.py',
    version_check='3.12'
)

๐Ÿ›ก๏ธ Version Checker

When you use the -v or --version-check option, the encrypted file will include a version check at the beginning:

CLI Usage

p2private script.py -v 3.11

Python API Usage

from p2private import encrypt_file

encrypt_file('script.py', version_check='3.11')

Generated Code

This adds the following code to the encrypted output:

PYTHON_VERSION = ".".join(sys.version.split(" ")[0].split(".")[:-1])
if PYTHON_VERSION != "3.11":
    print('[!] No support for 3.11')
    exit(0)

Use Cases

  • Ensure compatibility with specific Python versions
  • Prevent running on unsupported Python versions
  • Add version-specific behavior

๐Ÿ”ง Complete Setup Guide

Local Installation

Step 1: Clone the Repository

git clone https://github.com/pooraddy/p2private.git
cd p2private

Step 2: Create Virtual Environment (Recommended)

python -m venv venv

Activate on Linux/macOS:

source venv/bin/activate

Activate on Windows:

venv\Scripts\activate

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Install in Development Mode

pip install -e .

Step 5: Verify Installation

p2private --version

๐Ÿ“ Examples

Example 1: Simple Script Encryption

Original script (calculator.py):

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

if __name__ == "__main__":
    print("Simple Calculator")
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    print(f"Sum: {add(x, y)}")
    print(f"Difference: {subtract(x, y)}")

Encrypt:

p2private calculator.py -o calc

Run:

python calc.py

Example 2: With Version Check

p2private myapp.py -v 3.11 -o secure_app

Example 3: Batch Encryption

for file in *.py; do
    p2private "$file" -d ./encrypted
done

Example 4: Python API in Script

#!/usr/bin/env python3
# batch_encrypt.py

import os
from p2private import P2PrivateEncryptor

encryptor = P2PrivateEncryptor(version_check='3.11')

scripts = ['app1.py', 'app2.py', 'app3.py']

for script in scripts:
    if os.path.exists(script):
        try:
            output = encryptor.encrypt(script, output_dir='./encrypted')
            print(f"โœ“ Encrypted: {script} -> {output}")
        except Exception as e:
            print(f"โœ— Failed: {script} - {e}")
    else:
        print(f"โœ— Not found: {script}")

Example 5: Using encrypt_file Function

from p2private import encrypt_file

# Encrypt single file
encrypt_file('script.py')

# Encrypt with options
encrypt_file(
    file_path='script.py',
    output_dir='./output',
    output_name='myapp',
    version_check='3.12'
)

๐Ÿ“‹ Requirements

System Requirements

  • Python 3.11 or higher
  • GCC compiler (for compiling C code)
  • Cython 0.29.0 or higher

Install System Dependencies

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install python3-dev gcc

CentOS/RHEL/Fedora:

sudo yum install python3-devel gcc

macOS:

xcode-select --install

Windows:

Python Dependencies

Cython>=0.29.0
autopep8>=1.6.0

๐Ÿ” Troubleshooting

Common Issues

Issue: gcc: command not found

Solution: Install GCC compiler

# Ubuntu/Debian
sudo apt-get install gcc

# macOS
xcode-select --install

Issue: Python.h: No such file or directory

Solution: Install Python development headers

# Ubuntu/Debian
sudo apt-get install python3-dev

# CentOS/RHEL
sudo yum install python3-devel

Issue: cythonize: command not found

Solution: Install Cython

pip install Cython

Issue: Permission denied when running encrypted file

Solution: Make the file executable

chmod +x Enc_script.py

Issue: Compilation fails on Windows

Solution: Ensure you have Visual C++ Build Tools installed

# Or use MinGW
pip install --global-option=build_ext --global-option="-c mingw32" Cython

Issue: Mixed use of tabs and spaces Error

Solution: This is now automatically fixed by p2private! The tool automatically converts tabs to spaces before compilation.


Issue: Deprecated warnings (Py_SetProgramName, PySys_SetArgv)

Solution: Fixed in version 1.0.3! The code now uses conditional compilation to avoid deprecated functions on Python 3.11+.


Debug Mode

To see detailed error messages:

import traceback
traceback.print_exc()

๐Ÿ“‚ Project Structure

p2private/
โ”œโ”€โ”€ p2private/              # Main package
โ”‚   โ”œโ”€โ”€ __init__.py         # Package initialization
โ”‚   โ”œโ”€โ”€ encryptor.py        # Core encryption logic
โ”‚   โ””โ”€โ”€ cli.py              # Command line interface
โ”œโ”€โ”€ tests/                  # Test suite
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ test_encryptor.py
โ”‚   โ””โ”€โ”€ test_cli.py
โ”œโ”€โ”€ .github/workflows/      # GitHub Actions
โ”‚   โ”œโ”€โ”€ publish.yml         # PyPI publishing
โ”‚   โ””โ”€โ”€ tests.yml           # Test runner
โ”œโ”€โ”€ setup.py                # Package setup
โ”œโ”€โ”€ pyproject.toml          # Modern Python packaging
โ”œโ”€โ”€ requirements.txt        # Dependencies
โ”œโ”€โ”€ LICENSE                 # MIT License
โ”œโ”€โ”€ CHANGELOG.md            # Version history
โ”œโ”€โ”€ MANIFEST.in             # Package manifest
โ”œโ”€โ”€ .gitignore              # Git ignore rules
โ””โ”€โ”€ README.md               # This file

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ™ Acknowledgments

  • Cython - C-Extensions for Python
  • autopep8 - Automatic Python code formatter

๐Ÿ“ง Contact


Made with โค๏ธ using Python & Cython

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

p2private-1.0.4.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

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

p2private-1.0.4-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file p2private-1.0.4.tar.gz.

File metadata

  • Download URL: p2private-1.0.4.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for p2private-1.0.4.tar.gz
Algorithm Hash digest
SHA256 295e7f13f9de735497b189c5c9cffd5abb6ec05c638cf9f3d5b741002ed6c232
MD5 7880cc0ad2bf44d3343c28aa16d2a251
BLAKE2b-256 0280955c4d7cc53d9015aaec8d74b8b8998b2f8e3a976970fabe308cf6028e9f

See more details on using hashes here.

File details

Details for the file p2private-1.0.4-py3-none-any.whl.

File metadata

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

File hashes

Hashes for p2private-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fbb06d699866f88385dc87305112282eb00790c2336bcb28ef43943acf6aa038
MD5 2bd1361c16a39f20fd4eefc981581f3f
BLAKE2b-256 42b15c7046e395079326ccb952d6678923bed9e2d835d874a4b98c663fb20efb

See more details on using hashes here.

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