Build a Virtual Environment Manager
Advanced · Write the code · from Python Virtual Environments and pip: A Practical Guide
From the lesson: Python Virtual Environments and pip: A Practical Guide
Imagine you're a carpenter working on two different projects. One needs tiny finishing nails and the other needs heavy-duty bolts. You wouldn't dump all your hardware into one toolbox - you'd have separate toolboxes so the right parts are always ready for the right job.
Read the lessonCreate a class called VenvManager that simulates managing virtual environments:
__init__(): Initialize with an empty dictionary envs and set active to Nonecreate(name): Add an environment with name as key and an empty list as value. Print Created: nameactivate(name): Set active to name if it exists. Print Activated: name. If it doesn't exist, print Error: name not foundinstall(package): Append package to the active environment's list. Print Installed: package. If no environment is active, print Error: no active environmentlist_packages(): Print each package in the active environment, one per line. If no environment is active, print Error: no active environment. If the environment is empty, print No packages installedclass VenvManager:
# Build your virtual environment manager
pass
What Check runs
Your code runs first, then each of these lines. The output must match exactly.
| Runs | Expected output |
|---|---|
| vm = VenvManager() vm.create('myenv') vm.activate('myenv') vm.install('flask') vm.install('requests') vm.list_packages() | Created: myenv Activated: myenv Installed: flask Installed: requests flask requests |
| vm = VenvManager() vm.install('flask') vm.activate('missing') vm.create('empty') vm.activate('empty') vm.list_packages() | Error: no active environment Error: missing not found Created: empty Activated: empty No packages installed |
More Virtual Environment exercises
- Parse a Requirements FileBeginner
- Fix the Dependency ResolverIntermediate
- Predict the Environment CheckIntermediate
All Python exercises · 50 practice problems · Modules & Packaging quiz