How to add pip dependencies to an electron#

In this section, we illustrate the steps in adding a list of PyPI packages that are required to execute an electron.

First, let’s import covalent.

[17]:
import covalent as ct
import numpy

We can then use the DepsPip class to declare which pip packages we want our electrons to depend on.

[18]:
deps_pip = ct.DepsPip(packages=["numpy==1.22.4"])
[19]:
@ct.electron(
    deps_pip=deps_pip,
)
def get_result():
    matrix = numpy.identity(3)
    return numpy.sum(matrix)

@ct.lattice
def workflow():
    return get_result()

We can then dispatch our workflow to get our result.

[20]:
dispatch_id = ct.dispatch(workflow)()
r = ct.get_result(dispatch_id, wait=True)
print(r.result)
3.0

Alternatively, we can specify the path to a requirements.txt file that contains the list of required packages:

[22]:
import os
with open('requirements_example.txt', 'w') as f:
    f.write('numpy==1.22.4')
f.close()

deps_pip=ct.DepsPip(reqs_path="./requirements_example.txt")
[23]:
@ct.electron(
    deps_pip=deps_pip,
)
def get_result():
    matrix = numpy.identity(3)
    return numpy.sum(matrix)

@ct.lattice
def workflow():
    return get_result()

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

We can also add pip dependencies as call_before & call_after hooks on electrons.

[ ]:
@ct.electron(
    call_before=[ct.DepsPip(packages=["numpy==1.22.4"])],
    call_after=[ct.DepsPip(packages=["networkx==2.5"])]
)
def my_task():
    pass