Python to Private Executable Converter - Encrypt Python scripts to standalone executables
Project description
๐ p2private
Python to Private Executable Converter
Encrypt your Python scripts to standalone executables using Cython
๐ Table of Contents
- Features
- Installation
- Quick Start
- Usage
- Complete Setup Guide
- Examples
- Requirements
- Troubleshooting
- License
โจ 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
- Go to pypi.org
- Register for an account
- Verify your email
- Go to Account Settings โ API Tokens
- Create a new API token with scope "Entire account"
- Copy and save the token securely
Step 2: Setup GitHub Secrets (for GitHub Actions)
- Go to your GitHub repository
- Click Settings โ Secrets and variables โ Actions
- Click New repository secret
- Name:
PYPI_API_TOKEN - Value: Your PyPI API token
- 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
- Update version in
setup.pyandpyproject.toml - Update
CHANGELOG.md - Commit and push changes:
git add .
git commit -m "Release v1.0.0"
git push origin main
- Create and push a tag:
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
- 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:
- Install MinGW-w64 or Visual Studio Build Tools
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
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
๐ง Contact
- Author: Your Name
- Email: your.email@example.com
- GitHub: @yourusername
- PyPI: p2private
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
440add8a42d2646dcacbd77581b93eeeb7f9872604eb6549e76d946c1b00ab24
|
|
| MD5 |
f05a9d3a810cd80a21e7005c0062356b
|
|
| BLAKE2b-256 |
faab0cd83a7c1636443ba74753e5e01a1446953e199f9c58f60c3ffb13b24800
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cadb9d1e7a04388cb94c4d8653acc35811de2dc98caa9675f00ba1897f15b604
|
|
| MD5 |
d831f0cd9cb0cf1639902e70dfefa1ea
|
|
| BLAKE2b-256 |
11ae7a436714798685ab7909289cdb1bbb4b7c7fcc5eeb616440ecae63f76629
|