Adding Pip Dependencies to an Electron#

There are three ways to declare Pip package dependencies to an electron:

  1. Assign the dependencies directly in the Covalent DepsPip class.

  2. Specify a requirements file containing the dependencies.

  3. Use the electron’s call_before() and call_after() hooks.

All three methods are illustrated below.

Prerequisites#

Import covalent and the packages required by the electron.

[12]:
import covalent as ct
import numpy

Optionally, if you’re going to assign dependencies from a requirements file, create the file.

[13]:
with open('requirements_example.txt', 'w') as f:
    f.write('numpy==1.22.4')

Procedure#

Using the DepsPip Class#

  1. Create a Covalent DepsPip object, passing a list of package assignments as the packages keyword argument.

[14]:
deps_numpy = ct.DepsPip(packages=["numpy==1.22.4"])

Specifying a Requirements File#

  1. To use a requirements file instead, pass the file path to the DepsPip class as the reqs_path keyword rather than passing the packages list.

[15]:
deps_numpy = ct.DepsPip(reqs_path="./requirements_example.txt")
  1. In either case, once the DepsPip object is created, pass it to the electron decorator as the deps_pip keyword argument.

[16]:
@ct.electron(
    deps_pip=deps_numpy
)
def get_result():
    matrix = numpy.identity(3)
    return numpy.sum(matrix)

Using call_before() and call_after()#

  1. Rather than assign a DepsPip object in the deps_pip argument, you can assign a DepsPip object to either the call_before() or call_after() hook on an electron, or assign a different DepsPip object to both.

[17]:
@ct.electron(
    call_before = [ct.DepsPip(packages=["numpy==1.22.4"])],
    call_after = [ct.DepsPip(packages=["networkx==2.8.6"])]
)
def get_result():
    matrix = numpy.identity(3)
    return numpy.sum(matrix)

Complete the Workflow#

Regardless of how you’ve assigned the dependencies, assign the electron to a workflow and dispatch the workflow as you normally would.

[18]:
@ct.lattice
def workflow():
    return get_result()

dispatch_id = ct.dispatch(workflow)()
res = ct.get_result(dispatch_id, wait=True)
print(res.result)
3.0