-
Notifications
You must be signed in to change notification settings - Fork 0
venv manager
venv_manager.py is a small command-line utility for creating, listing, and deleting Python virtual environments in the current working directory. It wraps the stdlib venv module and shutil.rmtree behind a consistent subcommand interface.
No installation required — it is a single-file script with stdlib-only dependencies.
The script is Unix-oriented (macOS/Linux). It has no Windows-specific handling:
-
Environment detection (
list): checks only forbin/activate. Windows venvs useScripts\activate.bat— those will not be detected. -
createcommand: always callspython3 -m venv. On Windows,python3may not exist in PATH; creation will fail withError: 'python3' command not found. -
Activation instructions: always prints the Unix form
source <env>/bin/activate.
Running on Windows without modification will produce incorrect results or errors for list and create.
python venv_manager.py <command> [arguments]
Run without arguments to print the help message.
Lists all virtual environments found in the current working directory.
python venv_manager.py list
A directory is considered a virtual environment if it contains bin/activate. Subdirectories that fail this check are ignored. Windows venvs (Scripts\activate.bat) are not detected.
Output example:
Listing virtual environments in the current directory:
✅ myenv
✅ testenv
To activate an environment, use: 'source <env_name>/bin/activate'
If no environments are found, prints: No virtual environments found.
Creates a new virtual environment with the given name in the current directory.
python venv_manager.py create <name>
| Argument | Type | Description |
|---|---|---|
name |
string | Directory name for the new virtual environment |
Behavior:
- Calls
python3 -m venv <name>(Unix only;python3is hardcoded). - Prints the activation command for the current OS on success.
- Reports a clear error if
python3is not found in PATH. - Reports a clear error (including stderr from the venv command) on failure.
Error cases:
- Python not found in PATH →
Error: 'python3' command not found. - Directory already exists with the same name → reported via
subprocess.CalledProcessError
Deletes an existing virtual environment by removing its directory tree.
python venv_manager.py delete <name>
| Argument | Type | Description |
|---|---|---|
name |
string | Name of the virtual environment to delete |
Behavior:
- Checks that
<name>exists before proceeding; exits with an error message if not. -
Prompts for confirmation before deleting:
Are you sure you want to delete '<name>'? (y/n).-
y→ deletes the directory withshutil.rmtree. - Any other input → prints
Deletion cancelled.and exits without deleting.
-
Note: This is an interactive script —
deleterequires stdin input. It cannot be run non-interactively without modification.
# Create a virtual environment named 'myproject'
python venv_manager.py create myproject
# List all virtual environments in the current directory
python venv_manager.py list
# Delete a virtual environment (will prompt for confirmation)
python venv_manager.py delete myproject-
listdetects environments only in the current working directory (depth 1). Nested environments are not found. -
deleteremoves any directory named<name>, not just directories that pass the venv detection check. Passing the wrong name can delete unrelated directories. - No
activatesubcommand — activation modifies the shell environment and cannot be done by a subprocess. Use the printed activation command directly in your shell.