Skip to content

Commit

Permalink
Allow (un)installing integrations with system-wide uv installations
Browse files Browse the repository at this point in the history
  • Loading branch information
schustmi committed Feb 5, 2025
1 parent 64e12ba commit a4f07c2
Showing 1 changed file with 42 additions and 53 deletions.
95 changes: 42 additions & 53 deletions src/zenml/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
import platform
import re
import shutil
import subprocess
import sys
from typing import (
Expand Down Expand Up @@ -1048,22 +1049,18 @@ def install_packages(
# just return without doing anything
return

pip_command = ["uv", "pip"] if use_uv else ["pip"]
if upgrade:
command = (
[
sys.executable,
"-m",
]
+ pip_command
+ [
"install",
"--upgrade",
]
+ packages
)
if use_uv and not is_installed_in_python_environment("uv"):
# If uv is installed globally, don't run as a python module
command = []
else:
command = [sys.executable, "-m"] + pip_command + ["install"] + packages
command = [sys.executable, "-m"]

command += ["uv", "pip", "install"] if use_uv else ["pip", "install"]

if upgrade:
command += ["--upgrade"]

command += packages

if not IS_DEBUG_ENV:
quiet_flag = "-q" if use_uv else "-qqq"
Expand Down Expand Up @@ -1094,62 +1091,54 @@ def uninstall_package(package: str, use_uv: bool = False) -> None:
package: The package to uninstall.
use_uv: Whether to use uv for package uninstallation.
"""
pip_command = ["uv", "pip"] if use_uv else ["pip"]
quiet_flag = "-q" if use_uv else "-qqq"

if use_uv:
subprocess.check_call(
[
sys.executable,
"-m",
]
+ pip_command
+ [
"uninstall",
quiet_flag,
package,
]
)
if use_uv and not is_installed_in_python_environment("uv"):
# If uv is installed globally, don't run as a python module
command = []
else:
subprocess.check_call(
[
sys.executable,
"-m",
]
+ pip_command
+ [
"uninstall",
quiet_flag,
"-y",
package,
]
)
command = [sys.executable, "-m"]

command += (
["uv", "pip", "uninstall", "-q"]
if use_uv
else ["pip", "uninstall", "-y", "-qqq"]
)
command += [package]

def is_uv_installed() -> bool:
"""Check if uv is installed in the current environment.
subprocess.check_call(command)


def is_installed_in_python_environment(package: str) -> bool:
"""Check if a package is installed in the current python environment.
Args:
package: The package to check.
Returns:
True if uv is installed, False otherwise.
True if the package is installed, False otherwise.
"""
try:
pkg_resources.get_distribution("uv")
pkg_resources.get_distribution(package)
return True
except pkg_resources.DistributionNotFound:
return False


def is_uv_installed() -> bool:
"""Check if uv is installed.
Returns:
True if uv is installed, False otherwise.
"""
return shutil.which("uv") is not None


def is_pip_installed() -> bool:
"""Check if pip is installed in the current environment.
Returns:
True if pip is installed, False otherwise.
"""
try:
pkg_resources.get_distribution("pip")
return True
except pkg_resources.DistributionNotFound:
return False
return is_installed_in_python_environment("pip")


def pretty_print_secret(
Expand Down

0 comments on commit a4f07c2

Please sign in to comment.