Skip to main content

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

Project description

๐Ÿ” p2private

Python 3.7+ 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

๐Ÿ“ฆ Installation

From PyPI (Recommended)

pip install p2private

From Source

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

Development Installation

git clone https://github.com/yourusername/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

๐Ÿ“– Usage

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 (Python 3.10 required)
p2private script.py -v 3.10

# Full example
p2private script.py -o myapp -d ./build -v 3.11

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.10
--version - Show version --version
--help -h Show help --help

Python API

from p2private import P2PrivateEncryptor

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

# With version check
encryptor = P2PrivateEncryptor(version_check='3.10')
output_path = encryptor.encrypt('script.py')

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

Convenience Function

from p2private import encrypt_file

# Simple encryption
encrypt_file('script.py')

# With options
encrypt_file(
    file_path='script.py',
    output_dir='./build',
    output_name='myapp.py',
    version_check='3.10'
)

Version Checker

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

p2private script.py -v 3.10

This adds the following code to the encrypted output:

PYTHON_VERSION = ".".join(sys.version.split(" ")[0].split(".")[:-1])
if PYTHON_VERSION != "3.10":
    print('[!] No support for 3.10')
    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/yourusername/p2private.git
cd p2private

Step 2: Create Virtual Environment (Recommended)

# Using venv
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

Publishing to PyPI

Step 1: Create PyPI Account

  1. Go to pypi.org
  2. Register for an account
  3. Verify your email
  4. Go to Account Settings โ†’ API Tokens
  5. Create a new API token with scope "Entire account"
  6. Copy and save the token securely

Step 2: Setup GitHub Secrets (for GitHub Actions)

  1. Go to your GitHub repository
  2. Click Settings โ†’ Secrets and variables โ†’ Actions
  3. Click New repository secret
  4. Name: PYPI_API_TOKEN
  5. Value: Your PyPI API token
  6. Click Add secret

Step 3: Update Package Metadata

Edit these files with your information:

setup.py:

author='Your Name',
author_email='your.email@example.com',
url='https://github.com/yourusername/p2private',

pyproject.toml:

authors = [
    {name = "Your Name", email = "your.email@example.com"}
]

Step 4: Build and Test Locally

# Install build tools
pip install build twine

# Build the package
python -m build

# Check the package
twine check dist/*

# Test upload to TestPyPI (optional)
twine upload --repository testpypi dist/*

Step 5: Publish via GitHub Actions

  1. Update version in setup.py and pyproject.toml
  2. Update CHANGELOG.md
  3. Commit and push changes:
git add .
git commit -m "Release v1.0.0"
git push origin main
  1. Create and push a tag:
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
  1. GitHub Actions will automatically build and publish to PyPI!

Step 6: Manual Publishing (Alternative)

If you prefer manual publishing:

# Clean previous builds
rm -rf dist/ build/ *.egg-info

# Build
python -m build

# Upload to PyPI
twine upload dist/*

๐Ÿ“ 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

# Require Python 3.10+
p2private myapp.py -v 3.10 -o secure_app

Example 3: Batch Encryption

# Encrypt multiple files
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.10')

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}")

๐Ÿ“‹ Requirements

System Requirements

  • Python 3.7 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:

# Install Xcode Command Line Tools
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

Debug Mode

To see detailed error messages:

import traceback
# Add at the beginning of your script
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.1.tar.gz (19.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.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: p2private-1.0.1.tar.gz
  • Upload date:
  • Size: 19.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.1.tar.gz
Algorithm Hash digest
SHA256 440add8a42d2646dcacbd77581b93eeeb7f9872604eb6549e76d946c1b00ab24
MD5 f05a9d3a810cd80a21e7005c0062356b
BLAKE2b-256 faab0cd83a7c1636443ba74753e5e01a1446953e199f9c58f60c3ffb13b24800

See more details on using hashes here.

File details

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

File metadata

  • Download URL: p2private-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cadb9d1e7a04388cb94c4d8653acc35811de2dc98caa9675f00ba1897f15b604
MD5 d831f0cd9cb0cf1639902e70dfefa1ea
BLAKE2b-256 11ae7a436714798685ab7909289cdb1bbb4b7c7fcc5eeb616440ecae63f76629

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