Skip to main content

Python Package Utilities: Run C/C++ code, auto-install packages, generate projects, analyze dependencies, and more

Project description

๐Ÿš€ PypUtil - Python Package Utilities

Write C code directly in Python. Auto-install packages on import.

Generate complete projects in one line.

Python License Version PyPI


๐Ÿง  What is PypUtil?

Ever wished you could:

  • Run C/C++ code inside Python without compiling separately?
  • Auto-install packages when you import them?
  • Generate a complete project structure in one command?
  • Freeze modules in memory for instant loading?
  • Load code from URLs as if it were local?

PypUtil does all that and more.

It's an all-in-one toolkit for Python developers who want deeper control, automation, and performance in their workflows.


โšก Quick Examples

from pyputil.cutil import cfast
from pyputil.template import build_structure_template
from pyputil.install import auto_install

# 1. Run C code (1000x faster)
add = cfast.cfunc("int add(int a, int b) { return a + b; }")
print(add(5, 3))  # 8

# 2. Generate a complete project
build_structure_template("my_awesome_project")

# 3. Auto-install missing packages
auto_install(mode="confirm")
import requests  # Installs automatically if missing!

๐Ÿ“ฆ Installation

pip install pyputil-python

๐Ÿ”ฅ Why PypUtil?

Feature PypUtil cookiecutter ctypes pip poetry Project templates โœ… โœ… โŒ โŒ โŒ Run C in Python โœ… โŒ โœ… โŒ โŒ Run C++ in Python โœ… โŒ โŒ โŒ โŒ Auto-install packages โœ… โŒ โŒ โŒ โŒ Hot reload โœ… โŒ โŒ โŒ โŒ Freeze modules โœ… โŒ โŒ โŒ โŒ Load from URL โœ… โŒ โŒ โŒ โŒ Single library โœ… โŒ โŒ โŒ โŒ


๐Ÿ“– Detailed Usage

โšก C/C++ in Python (Two Options)

cfast (lightweight - for simple C functions):

from pyputil.cutil import cfast

# Simple function
add = cfast.cfunc("""
int add(int a, int b) {
    return a + b;
}
""")
print(add(5, 3))  # 8

# With math library
distance = cfast.cfunc("""
#include <math.h>
double distance(double x1, double y1, double x2, double y2) {
    double dx = x2 - x1;
    double dy = y2 - y1;
    return sqrt(dx*dx + dy*dy);
}
""", libraries=["m"])

print(distance(0, 0, 3, 4))  # 5.0

# Decorator style
@cfast.cfunc
def factorial(n: int) -> int:
    """
    int factorial(int n) {
        if (n <= 1) return 1;
        return n * factorial(n - 1);
    }
    """
print(factorial(10))  # 3628800

cimporter (full-featured - for C++/C/Cython projects):

from pyputil.cutil import cimporter as cimp

# Load C++ with optimizations
module = cimp.load(
    "neural_net.cpp",
    optimization="aggressive",
    simd="avx2",
    openmp=True,
    language_standard="c++17"
)

# Load multiple files in parallel
modules = cimp.load_batch(["kernel.c", "utils.c", "math.c"], parallel=True)

# Sandbox for security
sandbox = cimp.create_sandbox(
    policy="strict",
    timeout_seconds=30,
    memory_limit_mb=512
)
result = sandbox.run(["gcc", "untrusted_code.c"])

# Hot reload
module = cimp.load("dev_module.cpp", auto_reload=True)
# ... edit the file ...
module = cimp.reload("dev_module")  # Recompiles automatically

๐Ÿ“ฆ Auto-Install Missing Packages

from pyputil.install import auto_install, auto_install_context, PackageInstaller

# Mode 1: Confirm before installing (safest)
auto_install(mode="confirm")

# Mode 2: Silent install (for trusted environments)
auto_install(mode="silent")

# Mode 3: Dry run (preview only)
auto_install(mode="dry_run")

# Mode 4: Strict mode (only specific packages)
auto_install(mode="strict", safe_packages={"requests", "numpy", "pandas"})

# Temporary auto-install (only inside context)
with auto_install_context(mode="confirm"):
    import flask  # Will auto-install if missing

# Manual package management
installer = PackageInstaller("requests")
if not installer.is_installed():
    installer.install(version="2.31.0")
installer.upgrade()
print(installer.get_version())  # '2.31.0'

๐Ÿ—๏ธ Generate Complete Project Templates

from pyputil.template import build_structure_template, write_readme, write_pyproject

# Basic usage - one line!
build_structure_template("my_package")

# Advanced usage
build_structure_template(
    pathname="myproject",
    project_type="cli",  # or "library", "web", "data-science"
    package_name="myproject",
    version="1.0.0",
    description="An awesome Python package",
    author="Jane Doe",
    author_email="jane@example.com",
    license_type="MIT",
    python_requires=">=3.9",
    dependencies=["requests>=2.28", "click>=8.0"],
    extras={
        "dev": ["pytest>=7.0", "black>=23.0", "ruff>=0.1"],
        "ml": ["numpy>=1.21", "pandas>=1.3"]
    },
    entry_points={
        "console_scripts": ["mycli = myproject.cli:main"]
    },
    create_github_actions=True,
    create_changelog=True,
    create_tests=True,
    create_docs=True,
    git_init=True,
    verbose=True
)

# Generate individual files
write_pyproject("pyproject.toml", name="mypackage", version="1.0.0")
write_readme("README.md", name="My Project", badges={"python": "3.10"})

What gets generated:

myproject/
โ”œโ”€โ”€ ๐Ÿ“„ pyproject.toml        # PEP 621 compliant
โ”œโ”€โ”€ ๐Ÿ“„ README.md             # Professional with badges
โ”œโ”€โ”€ ๐Ÿ“„ LICENSE               # MIT/Apache/GPL
โ”œโ”€โ”€ ๐Ÿ“„ .gitignore            # Comprehensive
โ”œโ”€โ”€ ๐Ÿ“„ CHANGELOG.md          # Keep a Changelog format
โ”œโ”€โ”€ ๐Ÿ“ src/myproject/
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ __init__.py
โ”‚   โ””โ”€โ”€ ๐Ÿ“„ main.py
โ”œโ”€โ”€ ๐Ÿ“ tests/
โ”œโ”€โ”€ ๐Ÿ“ docs/
โ”œโ”€โ”€ ๐Ÿ“ examples/
โ””โ”€โ”€ ๐Ÿ“ .github/workflows/
    โ””โ”€โ”€ ๐Ÿ“„ ci.yml            # GitHub Actions ready

โ„๏ธ Freeze Modules in Memory

from pyputil.modules import freeze_module, freeze_package, frozen_modules_context

# Freeze a single module
creator = freeze_module('hello', '''
def greet(name):
    return f"Hello {name}!"

VERSION = "1.0.0"
''', install=True)

# Now use it normally
import hello
print(hello.greet("World"))  # Hello World!
print(hello.VERSION)         # 1.0.0

# Freeze an entire package
creator = freeze_package('./my_package', prefix='mypkg')

# Temporary context (modules only available inside)
with frozen_modules_context(creator):
    import mypkg.module1
    import mypkg.module2
    # Modules work normally here

# Outside context - modules are gone

๐Ÿ“‚ Dynamic Module Loading

from pyputil.loader import load_from_code, load_from_file, load_from_url, lazy_load

# Load from code string
mod = load_from_code("""
def hello(): 
    return "Hello World!"

def add(a, b):
    return a + b
""", name="mymod")
print(mod.hello())  # Hello World!
print(mod.add(5, 3))  # 8

# Load from any file
module = load_from_file("/path/to/script.py")
module.some_function()

# Load from URL (remote code!)
url = "https://raw.githubusercontent.com/python/cpython/main/Lib/calendar.py"
module = load_from_url(url)
print(module.month_name[1])  # January

# Lazy loading (loads only when accessed)
np = lazy_load('numpy')  # Nothing loaded yet
arr = np.array([1, 2, 3])  # Now numpy loads

๐Ÿ” Package Search & Analysis

from pyputil.scan import Scanner, ScanConfig, SearchMethod
from pyputil.metadata import get_package_metadata
from pyputil.tree import print_tree, find_conflicts

# Scan for modules
scanner = Scanner()
result = scanner.scan("json")
print(f"Found {result.total_modules_found} modules")

# Advanced scanning
config = ScanConfig(
    search_method=SearchMethod.ALL,
    analyze_dependencies=True,
    parallel_scan=True,
    workers=4
)
result = scanner.scan("pytest", config)

# Get package metadata
info = get_package_metadata("requests")
print(f"Version: {info.version}")
print(f"Author: {info.author}")
print(f"Dependencies: {info.deps}")

# Visualize dependency tree
print_tree("requests", max_depth=3)

# Find version conflicts
conflicts = find_conflicts(tree)
if conflicts:
    print("Version conflicts detected!")

๐Ÿ“ฅ Package Installation & Management

from pyputil.install import PackageInstaller
from pyputil.path import copy, move, remove

# Install/upgrade packages
installer = PackageInstaller("requests")
if not installer.is_installed():
    installer.install(version="2.31.0")
    
if installer.check_upgrade():
    installer.upgrade()

# Copy/move packages
copy("requests", target="/backup")
move(["numpy", "pandas"], target="/archive")

# Safe removal with backup
remove("my_old_package", backup=True, dry_run=False)

๐Ÿงฉ Module Utilities

from pyputil.modules import is_stdlib, isbuiltin, mkmod
from pyputil.version import get_version_info, version_compare, Version

# Check module types
print(is_stdlib("os"))      # True (standard library)
print(isbuiltin("sys"))     # True (built-in module)

# Create dynamic module
module = mkmod("calculator", source="""
def add(a, b): 
    return a + b

def multiply(a, b):
    return a * b
""")
print(module.add(5, 3))      # 8
print(module.multiply(5, 3)) # 15

# Get package version
print(get_version_info("requests").version)   # '2.31.0'
print(version_compare("2.0.0", "1.0.0"))  # 1 (greater)

๐Ÿ’ก Use Cases

๐Ÿ”ง For Tool Developers

ยท Build CLI tools that auto-install dependencies ยท Create code generators and scaffolding tools ยท Develop package managers and analyzers

๐Ÿš€ For Performance-Critical Apps

ยท Move hot loops to C without leaving Python ยท Use SIMD (AVX2, SSE) and OpenMP for parallel processing ยท Compile C++ with maximum optimizations

๐Ÿ“ฆ For Package Maintainers

ยท Generate PyPI-ready projects in seconds ยท Auto-update CHANGELOG and version numbers ยท Create GitHub Actions workflows automatically

๐Ÿงช For Security Researchers

ยท Sandbox untrusted C code ยท Analyze package dependencies for vulnerabilities ยท Trace module imports and origins

๐ŸŽ“ For Educators

ยท Distribute Python scripts that auto-install requirements ยท Create coding exercise templates instantly ยท No "pip install" instructions needed


๐Ÿงช Testing

Run the test suite:

pytest tests/

Run specific module tests:

pytest tests/test_cfast.py
pytest tests/test_template.py
pytest tests/test_loader.py

๐Ÿค Contributing

Contributions are welcome!

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

๐Ÿ“„ License

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


๐Ÿ‘ค Author

Moamen Walid Iraq ๐Ÿ‡ฎ๐Ÿ‡ถ

ยท GitHub: @moamen-walid-pyputil


๐Ÿ“ฌ Support

ยท Issues: GitHub Issues ยท Discussions: GitHub Discussions


๐Ÿ™ Acknowledgments

ยท Python Software Foundation for PEP standards ยท All contributors and users of PypUtil


๐Ÿ”ฅ Final Note

PypUtil aims to simplify and supercharge Python development workflows by combining multiple powerful utilities into one cohesive library.

From running C code at native speed to auto-installing packages on import, from generating complete project templates to freezing modules in memory - PypUtil does it all.

Try it today:

pip install pyputil-python
from pyputil.cutil import cfast

add = cfast.cfunc("int add(int a, int b) { return a + b; }")
print(add(5, 3))  # 8 - That's C code running in Python!

Made with โค๏ธ for the Python community

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

pyputil-0.1.0.tar.gz (1.7 MB view details)

Uploaded Source

Built Distribution

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

pyputil-0.1.0-py3-none-any.whl (1.9 MB view details)

Uploaded Python 3

File details

Details for the file pyputil-0.1.0.tar.gz.

File metadata

  • Download URL: pyputil-0.1.0.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for pyputil-0.1.0.tar.gz
Algorithm Hash digest
SHA256 05af95856c9d9427fb5ebfe1841aab855cf65edde243900f9773e8b9ec499a69
MD5 fd0824c59e00e9d91cfa89569c62ce53
BLAKE2b-256 e25f9e3cd71dcaaf3b7f45fd37ecac51c88be53c118d406aa56b3b36ca93c3e5

See more details on using hashes here.

File details

Details for the file pyputil-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyputil-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for pyputil-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75caa7c8f7187a4f6f8432503b55313621e83c768f3e1ba3441326094b617829
MD5 a55ef535dd9c69a400a56aaa2d01aea7
BLAKE2b-256 dda87cada42ea3551fdbbd08ad2a838ba05b8ee2e4ff24f930245c2df66dd625

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