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.
import covalent as ct
import numpy
(Optional) If you're going to assign dependencies from a requirements file, create the file.
import os
with open('requirements_example.txt', 'w') as f:
f.write('numpy==1.22.4')
f.close()
Procedure
Using the DepsPip Class
- Create a Covalent
DepsPip
object, passing a list of package assignments as thepackages
keyword argument.
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.
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.
@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.
@ct.electron(
call_before = [ct.DepsPip(packages=["numpy==1.22.4"])],
call_after = [ct.DepsPip(packages=["networkx==2.5"])]
)
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.
@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