virtualenv-clone

0.5.7 · maintenance · verified Sat Apr 11

virtualenv-clone is a Python package that provides a command-line script to clone existing non-relocatable virtual environments. It handles updating internal paths (like `VIRTUAL_ENV` in `activate` scripts) and shebangs in executables to point to the new location, aiming to make a copied virtualenv functional without reinstalling packages. The current version is 0.5.7, released in September 2021, suggesting a maintenance-level release cadence.

Warnings

Install

Quickstart

This quickstart demonstrates how to create a source virtual environment, install a package into it, then use `virtualenv-clone` to create a functional copy, and finally verify that the cloned environment contains the installed package and is correctly configured.

import os
import subprocess
import shutil

def run_command(cmd, shell=True, check=True):
    print(f"Executing: {cmd}")
    result = subprocess.run(cmd, shell=shell, check=check, capture_output=True, text=True)
    print(result.stdout)
    if result.stderr:
        print(f"Stderr: {result.stderr}")
    return result

original_venv_name = "my_original_venv"
cloned_venv_name = "my_cloned_venv"

# Cleanup any previous runs
if os.path.exists(original_venv_name):
    shutil.rmtree(original_venv_name)
if os.path.exists(cloned_venv_name):
    shutil.rmtree(cloned_venv_name)

print("1. Creating original virtual environment...")
run_command(f"python -m venv {original_venv_name}")

print("2. Activating original virtual environment and installing requests...")
# On Windows, activate is in Scripts; on Unix-like, it's in bin
activate_script = os.path.join(original_venv_name, 'Scripts', 'activate') if os.name == 'nt' else os.path.join(original_venv_name, 'bin', 'activate')

# For cross-platform activation and command execution within venv
# Using subprocess.run with full path to python/pip within the venv
python_executable = os.path.join(original_venv_name, 'Scripts', 'python') if os.name == 'nt' else os.path.join(original_venv_name, 'bin', 'python')
pip_executable = os.path.join(original_venv_name, 'Scripts', 'pip') if os.name == 'nt' else os.path.join(original_venv_name, 'bin', 'pip')

run_command(f"{pip_executable} install requests", shell=False) # Use full path, not shell activation

print(f"3. Verifying requests in {original_venv_name}...")
run_command(f"{python_executable} -c \"import requests; print('requests imported successfully in original venv')\"", shell=False)

print(f"4. Cloning {original_venv_name} to {cloned_venv_name}...")
# virtualenv-clone is expected to be installed globally or accessible in PATH
run_command(f"virtualenv-clone {original_venv_name} {cloned_venv_name}")

print(f"5. Activating cloned virtual environment and verifying requests...")
cloned_python_executable = os.path.join(cloned_venv_name, 'Scripts', 'python') if os.name == 'nt' else os.path.join(cloned_venv_name, 'bin', 'python')

run_command(f"{cloned_python_executable} -c \"import requests; print('requests imported successfully in cloned venv')\"", shell=False)

print("Quickstart complete: virtual environment cloned and verified.")

# Cleanup
print("Cleaning up...")
shutil.rmtree(original_venv_name)
shutil.rmtree(cloned_venv_name)
print("Cleanup complete.")

view raw JSON →