Skip to content

pip

dev_tool.services.python.package.pip

log = logging.getLogger(__name__) module-attribute

WindowsPipPackageManager

Bases: PackageManager

A Windows-specific implementation of the Pip package manager.

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

is_available

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

Returns:

  • bool

    True if pip is available, False otherwise.

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

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

    command = ['where', 'pip']

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

    return result.returncode == 0

install_package

A method that installs a package using pip.

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/pip.py
def install_package(self, package: str) -> bool:
    """
    A method that installs a package using pip.

    :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)

    command = [
        VENV_PYTHON,
        '-m',
        'pip',
        'install',
        package
    ]

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

install_package_manager

A method that ensures pip is installed.

Returns:

  • bool

    True if pip is available, False otherwise.

Source code in dev_tool/services/python/package/pip.py
def install_package_manager(self) -> bool:
    """
    A method that ensures pip is installed.

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

    return self.is_available()

create_virtual_environment

A method that creates a virtual environment using venv.

Returns:

  • bool

    True if the creation was successful, False otherwise.

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

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

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

    log.debug(message)

    python = Path('python')

    command = [
        python,
        '-m',
        'venv',
        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 pip.

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/pip.py
def install_from_requirements(self, requirements: Path) -> bool:
    """
    A method that installs dependencies from a requirements file using pip.

    :param requirements_path: 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)

    command = [
        VENV_PYTHON,
        '-m',
        'pip',
        'install',
        '-U',
        '-r',
        requirements
    ]

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

install_from_pyproject

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

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/pip.py
def install_from_pyproject(self, extras: list[str] | None = None) -> bool:
    """
    A method that installs dependencies from pyproject.toml using pip.

    :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 = [
        VENV_PYTHON,
        '-m',
        'pip',
        'install',
        '-e',
        target
    ]

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

uninstall_package

A method that uninstalls a package using pip.

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/pip.py
def uninstall_package(self, package: str) -> bool:
    """
    A method that uninstalls a package using pip.

    :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)

    command = [
        VENV_PYTHON,
        '-m',
        'pip',
        'uninstall',
        '-y',
        package
    ]

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

upgrade_package_manager

A method that upgrades pip to the latest version.

Returns:

  • bool

    True if the upgrade was successful, False otherwise.

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

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

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

    log.debug(message)

    command = [
        VENV_PYTHON,
        '-m',
        'pip',
        'install',
        '--upgrade',
        'pip'
    ]

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

list_installed_packages

A method that lists all installed packages using pip.

Returns:

  • list[str]

    A list of installed package names.

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

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

    command = [VENV_PYTHON, '-m', 'pip', 'freeze']

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

    if result.returncode != 0:
        return []

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

UnixPipPackageManager

Bases: PackageManager

A Unix-specific implementation of the Pip package manager.

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

is_available

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

Returns:

  • bool

    True if pip3 is available, False otherwise.

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

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

    command = ['which', 'pip3']

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

    return result.returncode == 0

install_package

A method that installs a package using pip.

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/pip.py
def install_package(self, package: str) -> bool:
    """
    A method that installs a package using pip.

    :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)

    command = [
        VENV_PYTHON,
        '-m',
        'pip',
        'install',
        package
    ]

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

install_package_manager

A method that ensures pip3 is installed.

Returns:

  • bool

    True if pip3 is available, False otherwise.

Source code in dev_tool/services/python/package/pip.py
def install_package_manager(self) -> bool:
    """
    A method that ensures pip3 is installed.

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

    return self.is_available()

create_virtual_environment

A method that creates a virtual environment using venv.

Returns:

  • bool

    True if the creation was successful, False otherwise.

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

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

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

    log.debug(message)

    command = [
        'python3',
        '-m',
        'venv',
        VENV
    ]

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

install_from_requirements

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

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/pip.py
def install_from_requirements(self, requirements: Path) -> bool:
    """
    A method that installs dependencies from a requirements file using pip.

    :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)

    command = [
        VENV_PYTHON,
        '-m',
        'pip',
        'install',
        '-U',
        '-r',
        requirements
    ]

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

install_from_pyproject

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

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/pip.py
def install_from_pyproject(self, extras: list[str] | None = None) -> bool:
    """
    A method that installs dependencies from pyproject.toml using pip.

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

    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 = [
        VENV_PYTHON,
        '-m',
        'pip',
        'install',
        '-e',
        target
    ]

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

uninstall_package

A method that uninstalls a package using pip.

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/pip.py
def uninstall_package(self, package: str) -> bool:
    """
    A method that uninstalls a package using pip.

    :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)

    command = [
        VENV_PYTHON,
        '-m',
        'pip',
        'uninstall',
        '-y',
        package
    ]

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

upgrade_package_manager

A method that upgrades pip to the latest version.

Returns:

  • bool

    True if the upgrade was successful, False otherwise.

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

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

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

    log.debug(message)

    command = [
        VENV_PYTHON,
        '-m',
        'pip',
        'install',
        '--upgrade',
        'pip'
    ]

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

list_installed_packages

A method that lists all installed packages using pip.

Returns:

  • list[str]

    A list of installed package names.

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

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

    command = [VENV_PYTHON, '-m', 'pip', 'freeze']

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

    if result.returncode != 0:
        return []

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