Pyvers
Pyvers is a Python library designed to manage multiple versions of project dependencies locally within a `.pyvers` directory. It helps isolate project environments and ensures consistent dependency versions across development teams. The current version is 0.2.2, with releases occurring periodically, focusing on core functionality and stability.
Warnings
- gotcha When initializing `Pyvers`, ensure you explicitly provide `project_path` (e.g., `Pyvers(project_path=os.getcwd())`). If omitted, it defaults to the current working directory (`os.getcwd()`), which might not be the intended project root, leading to `.pyvers` directories being created in unexpected locations.
- gotcha The `add_dependency` method requires a `dep_type` parameter (e.g., 'prod', 'dev', 'test'). Forgetting this or providing an unrecognized type will result in an error, as the library enforces specific dependency categories.
- gotcha Adding a dependency via `add_dependency` only updates the `pyvers.json` configuration file. To actually install the specified packages into the local `.pyvers` virtual environment, you must explicitly call `pyvers_manager.install()` afterward.
Install
-
pip install pyvers
Imports
- Pyvers
from pyvers import Pyvers
Quickstart
import os
import shutil
import json
from pyvers import Pyvers
temp_project_name = "my_pyvers_project_quickstart"
project_root = os.path.join(os.getcwd(), temp_project_name)
# Clean up previous run if exists
if os.path.exists(project_root):
shutil.rmtree(project_root)
os.makedirs(project_root)
print(f"Created temporary project directory: {project_root}")
try:
pyvers_manager = Pyvers(project_path=project_root)
# 1. Initialize the Pyvers project
print("Initializing Pyvers project...")
pyvers_manager.init()
print(f"Pyvers project initialized at {os.path.join(project_root, '.pyvers')}")
# 2. Add a dependency to the pyvers.json config
dep_name = "requests"
dep_version = "2.28.1"
print(f"Adding dependency: {dep_name}@{dep_version} (type='prod')...")
pyvers_manager.add_dependency(name=dep_name, version=dep_version, dep_type="prod")
print(f"Dependency '{dep_name}' added to config.")
# 3. List dependencies from the config
print("\nListing dependencies from config:")
deps = pyvers_manager.list_dependencies()
if deps:
for dep in deps:
print(f" - {dep.name} @ {dep.version} ({dep.type})")
else:
print(" No dependencies found in config.")
# 4. Install dependencies (creates/updates local virtual environment)
print("\nInstalling dependencies...")
# In a real scenario, this would install the added 'requests' package into '.pyvers/env'
# For this quickstart, we just demonstrate the call.
pyvers_manager.install()
print("Dependencies installed to local .pyvers environment.")
finally:
# Clean up the temporary directory
if os.path.exists(project_root):
shutil.rmtree(project_root)
print(f"\nCleaned up temporary project directory: {project_root}")