Skip to main content

A simple python package to leverage pip programmatically.

Project description

piphyperd

pipeline status

pylint

A simple python package to leverage pip programmatically. piphyperd is a wrapper around pip; it can provide features like automation or dependencies control within your workflows.

About this package

The reasons behind this package, in alternative to the standard pip (import pip) module, are in the attempt to expose a more stable interface, when programmatically installing or managing python packages, within pipelines or automation workflows.

Although it is a Python module, and it is available via import pip, by design, pip is not supposed to be a library; every detail is subject to changes for any reason, from the import name itself to its API. It might be better to call the pip’s internal APIs differently.

Pitfalls

When leveraging pip programmatically, there are also other topics worth considering:

  1. The pip code assumes that it is in sole control of the global state of the program;
  2. pip’s code is not thread-safe. If you were to run pip in a thread;
  3. pip assumes that once it has finished its work, the process terminates.

Furthermore, installing packages under the sys.path from a running Python process might result in unexpected or undesired behaviors.

Taking everything into account, still might be necessary, or usefull, handling pip packages within code and automations, as much as possible in controlled manner. The most reliable way to do so, is leveraging pip in a subprocess:

# leverage subprocess.Popen to execute pip commands
process = Popen(
    [sys.executable if self.python_path is None else self.python_path,
     "-m", "pip", command]
    + self.pip_options + self.packages + self.command_args, stdout=PIPE, stderr=PIPE)

For further information, continue reading from the source of this topic at the the official pypa user guide.

Install the package

Refer to the official project page for further information about the package status and releases.

To install the latest version, run the following command in your terminal:

pip install --user piphyperd

API overview

Once installed, you can import the package as follows from piphyperd import PipHyperd. The module is wrapping pip commands in methods, exposed through the object PipHyperd. You can initialize it by optionally passing pip commands extra options:

def __init__(self, *pip_options: Any, python_path: Optional[Path] = None):
    # Path to the python binary to use
    self.python_path: Optional[Path] = python_path
    # A list of pip packages to install || show || download || uninstall
    self.packages: List[str] = list()
    # pip command args, e.g.: pip download testpypi {command_args}
    self.command_args: List[str] = list()
    # pip options, e.g.: pip {pip_options} uninstall testpypi
    self.pip_options: List[str] = list(pip_options)
# ...

The API exposed conveniently wraps a set of pip commands that can help generating virtual environments, reports of the installed packages, outdated libraries. The stdout and stderr are returned by each method, allowing to store the output or to read it in a second instance.

Object description

To follow, a brief walkthrough through the methods exposed by the PipHyperd object.

pip freeze

Output installed pip packages in requirements format:

piphyperd.PipHyperd().freeze()

pip list

List installed pip packages.

list_outdated -- True || False to list or not the outdated packages

piphyperd.PipHyperd("--verbose").list_packages() # the argument "--verbose" is of course optional

# List outdated packages
piphyperd.PipHyperd().list(True)

pip show {{ package }}

Show information about installed packages.

piphyperd.PipHyperd("--verbose").show("ansible")

pip check

Verify installed packages have compatible dependencies.

piphyperd.PipHyperd().check()

pipdeptree

Render installed packages with dependencies tree.

piphyperd.PipHyperd().dependencies_tree()

pip install {{ packages }}

Install pip packages.

piphyperd.PipHyperd("-U").install("ansible", "cryptography") # -U is of course optional, set here as example

pip download {{ package }}

Download pip packages.

piphyperd.PipHyperd("-U").download("ansible", "pip", "cryptography", destination="/your/path/here") # the destination argument is optional

pip uninstall {{ packages }}

Uninstall pip packages.

piphyperd.PipHyperd().uninstall("ansible", "pip", "cryptography") # the destination argument is optional

License

GNU General Public License v3 (GPLv3)

Author Information

Francesco Cosentino

I'm a surfer, a crypto trader, and a DevSecOps Engineer with 15 years of experience designing highly-available distributed production environments and developing cloud-native apps in public and private clouds.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

piphyperd-1.8.1.tar.gz (34.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page