Adding Pip Dependencies to an Electron#
There are three ways to declare Pip package dependencies to an electron:
Assign the dependencies directly in the Covalent
DepsPip
class.Specify a requirements file containing the dependencies.
Use the electron’s
call_before()
andcall_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#
Create a Covalent
DepsPip
object, passing a list of package assignments as thepackages
keyword argument.
[14]:
deps_numpy = ct.DepsPip(packages=["numpy==1.22.4"])
Specifying a Requirements File#
To use a requirements file instead, pass the file path to the
DepsPip
class as thereqs_path
keyword rather than passing thepackages
list.
[15]:
deps_numpy = ct.DepsPip(reqs_path="./requirements_example.txt")
In either case, once the
DepsPip
object is created, pass it to the electron decorator as thedeps_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()#
Rather than assign a
DepsPip
object in thedeps_pip
argument, you can assign aDepsPip
object to either thecall_before()
orcall_after()
hook on an electron, or assign a differentDepsPip
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