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
- CLI Usage
- Python API
- Version Checker
- 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
- ๐ง 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:
- 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
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
- 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: addy
- Email: poor4ddy@gmail.com
- GitHub: @pooraddy
- 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.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
295e7f13f9de735497b189c5c9cffd5abb6ec05c638cf9f3d5b741002ed6c232
|
|
| MD5 |
7880cc0ad2bf44d3343c28aa16d2a251
|
|
| BLAKE2b-256 |
0280955c4d7cc53d9015aaec8d74b8b8998b2f8e3a976970fabe308cf6028e9f
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbb06d699866f88385dc87305112282eb00790c2336bcb28ef43943acf6aa038
|
|
| MD5 |
2bd1361c16a39f20fd4eefc981581f3f
|
|
| BLAKE2b-256 |
42b15c7046e395079326ccb952d6678923bed9e2d835d874a4b98c663fb20efb
|