Skip to content

uv

dev_tool.services.python.package.uv

log = logging.getLogger(__name__) module-attribute

WindowsUvPackageManager

Bases: PackageManager

A Windows-specific implementation of the uv package manager.

This class provides methods for managing Python packages using uv on Windows.

create_virtual_environment

A method that creates a virtual environment using uv.

Returns:

  • bool

    True if the creation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def create_virtual_environment(self) -> bool:
    """
    A method that creates a virtual environment using uv.

    :return: True if the creation was successful, False otherwise.
    """

    message = 'Setting up virtual environment using uv...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    command = ['uv', 'venv', '--seed', str(VENV)]
    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

install_from_pyproject

A method that installs dependencies from pyproject.toml using uv.

Parameters:

  • extras (list[str] | None, default: None ) –

    Optional list of extra dependency groups to install.

Returns:

  • bool

    True if the installation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def install_from_pyproject(self, extras: list[str] | None = None) -> bool:
    """
    A method that installs dependencies from pyproject.toml using uv.

    :param extras: Optional list of extra dependency groups to install.
    :return: True if the installation was successful, False otherwise.
    """

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)
        return False

    if not self._validate_pyproject():
        message = 'pyproject.toml is not configured for installation'
        CONTEXT.notification.warning_text(message)

        log.debug(message)
        return False

    extras = extras or []

    if extras:
        string = ','.join(extras)
        target = f'.[{string}]'

        message = f'Installing project dependencies with extras: {string}...'
        CONTEXT.notification.normal_text(message)

        log.debug(message)
    else:
        target = '.'

        message = 'Installing project dependencies...'
        CONTEXT.notification.normal_text(message)

        log.debug(message)

    command = [
        'uv',
        'pip',
        'install',
        '--python',
        str(VENV_PYTHON),
        '-e',
        target
    ]

    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

install_from_requirements

A method that installs dependencies from a requirements file using uv.

Parameters:

  • requirements (Path) –

    The path to the requirements file.

Returns:

  • bool

    True if the installation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def install_from_requirements(self, requirements: Path) -> bool:
    """
    A method that installs dependencies from a requirements file using uv.

    :param requirements: The path to the requirements file.
    :return: True if the installation was successful, False otherwise.
    """

    message = f'Installing dependencies from {requirements.name}...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

        return False

    command = [
        'uv',
        'pip',
        'install',
        '--python',
        str(VENV_PYTHON),
        '-r', str(requirements)
    ]

    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

install_package

A method that installs a package using uv.

Parameters:

  • package (str) –

    The name of the package to install.

Returns:

  • bool

    True if the installation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def install_package(self, package: str) -> bool:
    """
    A method that installs a package using uv.

    :param package: The name of the package to install.
    :return: True if the installation was successful, False otherwise.
    """

    message = f'Installing {package}...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

        return False

    command = [
        'uv',
        'pip',
        'install',
        '--python',
        str(VENV_PYTHON),
        package
    ]

    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

install_package_manager

A method that installs uv if it is not already installed.

Returns:

  • bool

    True if the installation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def install_package_manager(self) -> bool:
    """
    A method that installs uv if it is not already installed.

    :return: True if the installation was successful, False otherwise.
    """

    if self.is_available():
        message = 'uv is already installed.'
        log.debug(message)

        return True

    message ='Installing uv package manager...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    if self._is_special_username():
        destination = rf'{SYSTEM_DRIVE}\uv'
        os.environ['UV_INSTALL_DIR'] = destination

        message = f'Username contains special characters, installing to {destination}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

    command = [
        'powershell',
        '-ExecutionPolicy',
        'ByPass',
        '-c',
        'irm https://astral.sh/uv/install.ps1 | iex'
    ]

    result = subprocess.run(command, check=False, shell=True)

    if result.returncode == 0:
        if self._is_special_username():
            destination = rf'{SYSTEM_DRIVE}\uv'
        else:
            destination = str(Path.home() / '.local' / 'bin')

        if self._is_special_username() or Path(destination).exists():
            path = os.environ['PATH']

            if destination not in path:
                os.environ['PATH'] = f'{destination};{path}'

        EnvironmentVariables.refresh_environment()

        message = 'Verifying uv installation...'
        CONTEXT.notification.normal_text(message)

        log.debug(message)

        command = ['uv', '--version']
        verification = subprocess.run(command, check=False, shell=True)
        return verification.returncode == 0

    return False

is_available

A method that checks if uv is available on the system.

Returns:

  • bool

    True if uv is available, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def is_available(self) -> bool:
    """
    A method that checks if uv is available on the system.

    :return: True if uv is available, False otherwise.
    """

    command = ['where', 'uv']

    result = subprocess.run(
        command,
        capture_output=True,
        check=False,
        shell=True
    )

    return result.returncode == 0

list_installed_packages

A method that lists all installed packages using uv.

Returns:

  • list[str]

    A list of installed package names.

Source code in dev_tool/services/python/package/uv.py
def list_installed_packages(self) -> list[str]:
    """
    A method that lists all installed packages using uv.

    :return: A list of installed package names.
    """

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

        return []

    command = [
        'uv',
        'pip',
        'freeze',
        '--python',
        str(VENV_PYTHON)
    ]

    result = subprocess.run(
        command,
        capture_output=True,
        check=False,
        shell=True,
        text=True
    )

    if result.returncode != 0:
        return []

    return [
        package.split('==')[0]
        for package in result.stdout.splitlines()
    ]

uninstall_package

A method that uninstalls a package using uv.

Parameters:

  • package (str) –

    The name of the package to uninstall.

Returns:

  • bool

    True if the uninstallation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def uninstall_package(self, package: str) -> bool:
    """
    A method that uninstalls a package using uv.

    :param package: The name of the package to uninstall.
    :return: True if the uninstallation was successful, False otherwise.
    """

    message = f'Uninstalling {package}...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

        return False

    command = [
        'uv',
        'pip',
        'uninstall',
        '--python',
        str(VENV_PYTHON),
        package
    ]

    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

upgrade_package_manager

Source code in dev_tool/services/python/package/uv.py
def upgrade_package_manager(self) -> bool:
    message = 'Checking for uv updates...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    command = ['uv', 'self', 'update']
    result = subprocess.run(command, capture_output=True, text=True, check=False)

    if result.returncode == 0:
        output = (result.stdout + result.stderr).lower()

        if 'you\'re on the latest version' in output or 'already up-to-date' in output:
            message = 'uv is already up-to-date'
            log.debug(message)
        else:
            message = 'uv updated successfully'
            CONTEXT.notification.normal_text(message)

            log.debug(message)

        EnvironmentVariables.refresh_environment()
        return True

    return False

UnixUvPackageManager

Bases: PackageManager

A Unix-specific implementation of the uv package manager.

This class provides methods for managing Python packages using uv on Unix-like systems.

is_available

A method that checks if uv is available on the system.

Returns:

  • bool

    True if uv is available, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def is_available(self) -> bool:
    """
    A method that checks if uv is available on the system.

    :return: True if uv is available, False otherwise.
    """

    command = ['which', 'uv']

    result = subprocess.run(
        command,
        capture_output=True,
        check=False,
        shell=True
    )

    return result.returncode == 0

install_package

A method that installs a package using uv.

Parameters:

  • package (str) –

    The name of the package to install.

Returns:

  • bool

    True if the installation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def install_package(self, package: str) -> bool:
    """
    A method that installs a package using uv.

    :param package: The name of the package to install.
    :return: True if the installation was successful, False otherwise.
    """

    message = f'Installing {package}...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

        return False

    command = [
        'uv',
        'pip',
        'install',
        '--python',
        str(VENV_PYTHON),
        package
    ]

    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

install_package_manager

A method that installs uv if it is not already installed.

Returns:

  • bool

    True if the installation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def install_package_manager(self) -> bool:
    """
    A method that installs uv if it is not already installed.

    :return: True if the installation was successful, False otherwise.
    """

    if self.is_available():
        message = 'uv is already installed.'
        log.debug(message)

        return True

    message = 'Installing uv package manager...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    command = [
        'curl',
        '-sSf',
        'https://astral.sh/uv/install.sh',
        '|',
        'sh'
    ]

    result = subprocess.run(' '.join(command), check=False, shell=True)

    if result.returncode == 0:
        local = str(Path.home() / '.local' / 'bin')
        path = os.environ['PATH']

        os.environ['PATH'] = f'{local}:{path}'

        message = 'Verifying uv installation...'
        CONTEXT.notification.normal_text(message)

        log.debug(message)

        command = ['uv', '--version']
        is_installed = subprocess.run(command, check=False, shell=True)
        return is_installed.returncode == 0

    return False

create_virtual_environment

A method that creates a virtual environment using uv.

Returns:

  • bool

    True if the creation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def create_virtual_environment(self) -> bool:
    """
    A method that creates a virtual environment using uv.

    :return: True if the creation was successful, False otherwise.
    """

    message = 'Setting up virtual environment using uv...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    command = ['uv', 'venv', '--seed', str(VENV)]
    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

install_from_requirements

A method that installs dependencies from a requirements file using uv.

Parameters:

  • requirements (Path) –

    The path to the requirements file.

Returns:

  • bool

    True if the installation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def install_from_requirements(self, requirements: Path) -> bool:
    """
    A method that installs dependencies from a requirements file using uv.

    :param requirements: The path to the requirements file.
    :return: True if the installation was successful, False otherwise.
    """

    message = f'Installing dependencies from {requirements.name}...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

        return False

    command = [
        'uv',
        'pip',
        'install',
        '--python',
        str(VENV_PYTHON),
        '-r',
        str(requirements)
    ]

    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

install_from_pyproject

A method that installs dependencies from pyproject.toml using uv.

Parameters:

  • extras (list[str] | None, default: None ) –

    Optional list of extra dependency groups to install.

Returns:

  • bool

    True if the installation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def install_from_pyproject(self, extras: list[str] | None = None) -> bool:
    """
    A method that installs dependencies from pyproject.toml using uv.

    :param extras: Optional list of extra dependency groups to install.
    :return: True if the installation was successful, False otherwise.
    """

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)
        return False

    extras = extras or []

    if extras:
        extras_string = ','.join(extras)
        target = f'.[{extras_string}]'

        message = f'Installing project dependencies with extras: {extras_string}...'
        CONTEXT.notification.normal_text(message)

        log.debug(message)
    else:
        target = '.'

        message = 'Installing project dependencies...'
        CONTEXT.notification.normal_text(message)

        log.debug(message)

    command = [
        'uv',
        'pip',
        'install',
        '--python',
        str(VENV_PYTHON),
        '-e',
        target
    ]

    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

uninstall_package

A method that uninstalls a package using uv.

Parameters:

  • package (str) –

    The name of the package to uninstall.

Returns:

  • bool

    True if the uninstallation was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def uninstall_package(self, package: str) -> bool:
    """
    A method that uninstalls a package using uv.

    :param package: The name of the package to uninstall.
    :return: True if the uninstallation was successful, False otherwise.
    """

    message = f'Uninstalling {package}...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

        return False

    command = [
        'uv',
        'pip',
        'uninstall',
        '--python',
        str(VENV_PYTHON),
        package
    ]

    result = subprocess.run(command, check=False, shell=True)
    return result.returncode == 0

upgrade_package_manager

A method that upgrades uv to the latest version.

Returns:

  • bool

    True if the upgrade was successful, False otherwise.

Source code in dev_tool/services/python/package/uv.py
def upgrade_package_manager(self) -> bool:
    """
    A method that upgrades uv to the latest version.

    :return: True if the upgrade was successful, False otherwise.
    """

    message = 'Upgrading uv...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    command = [
        'curl',
        '-sSf',
        'https://astral.sh/uv/install.sh',
        '|',
        'sh'
    ]

    result = subprocess.run(' '.join(command), check=False, shell=True)
    return result.returncode == 0

list_installed_packages

A method that lists all installed packages using uv.

Returns:

  • list[str]

    A list of installed package names.

Source code in dev_tool/services/python/package/uv.py
def list_installed_packages(self) -> list[str]:
    """
    A method that lists all installed packages using uv.

    :return: A list of installed package names.
    """

    if not VENV_PYTHON.exists():
        message = f'Python executable not found at {VENV_PYTHON}'
        CONTEXT.notification.warning_text(message)

        log.debug(message)

        return []

    command = [
        'uv',
        'pip',
        'freeze',
        '--python',
        str(VENV_PYTHON)
    ]

    result = subprocess.run(
        command,
        capture_output=True,
        check=False,
        shell=True,
        text=True
    )

    if result.returncode != 0:
        return []

    return [
        package.split('==')[0]
        for package in result.stdout.splitlines()
    ]