RPM Shim for Virtualenvs

0.4.0 · active · verified Wed Apr 15

The `rpm-shim` library (available on PyPI as `rpm`) provides a shim module to enable the use of system RPM Python bindings within Python virtual environments. This is necessary because native RPM Python bindings are typically tied to the system's RPM installation and are not distributed as standard Python packages. The library is currently at version 0.4.0 and releases are made on an as-needed basis to improve compatibility and search heuristics.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates installing `rpm-shim` (as `rpm`) into a virtual environment and then importing and using the `rpm` module. The core idea is that once `rpm-shim` is installed, a simple `import rpm` will make the system's RPM bindings available within your virtual environment. Ensure that your system has the native RPM Python bindings installed (e.g., `python3-rpm` package on RPM-based systems).

import venv
import subprocess
import sys
import os

# Create a dummy virtual environment for demonstration
venv_path = './my_rpm_venv'
if not os.path.exists(venv_path):
    venv.create(venv_path, with_pip=True)

# Activate the virtual environment and install rpm-shim
# In a real scenario, you'd activate the venv and then `pip install rpm`
print(f"Installing rpm-shim in {venv_path}...")
python_executable = os.path.join(venv_path, 'bin', 'python')
subprocess.run([python_executable, '-m', 'pip', 'install', 'rpm'], check=True, capture_output=True)
print("Installation complete.")

# Now demonstrate importing and using rpm inside the virtual environment
# (simulating execution within the venv)
python_code = """
import sys
import os

try:
    import rpm
    print(f"Successfully imported rpm from: {rpm.__file__}")
    # Example of using rpm (check version as a simple call)
    print(f"RPM library version: {rpm.expand_macro('%{_rpm_version}')}")
except ImportError as e:
    print(f"Failed to import rpm: {e}")
    print("Make sure system RPM Python bindings are installed on your system (e.g., python3-rpm).")
"""

print("\nRunning rpm import test within the virtual environment...")
result = subprocess.run([python_executable, '-c', python_code], capture_output=True, text=True, check=True)
print(result.stdout)
if result.stderr:
    print("Stderr:", result.stderr)

# Clean up (optional)
# import shutil
# shutil.rmtree(venv_path)

view raw JSON →