snowflake.snowpark.Session.add_packagesΒΆ

Session.add_packages(*packages: Union[str, module, Iterable[Union[str, module]]]) β†’ None[source]ΒΆ

Adds third-party packages as dependencies of a user-defined function (UDF). Use this method to add packages for UDFs as installing packages using conda. You can also find examples in UDFRegistration. See details of third-party Python packages in Snowflake.

To use Python packages that are not available in Snowflake, refer to custom_package_usage_config().

Parameters:

packages – A requirement specifier, a module object or a list of them for installing the packages. An exception will be raised if two conflicting requirement specifiers are provided. The syntax of a requirement specifier is defined in full in PEP 508, but currently only the version matching clause (==) is supported as a version specifier for this argument. If a module object is provided, the package will be installed with the version in the local environment.

Example:

>>> import numpy as np
>>> from snowflake.snowpark.functions import udf
>>> import numpy
>>> import pandas
>>> import dateutil
>>> # add numpy with the latest version on Snowflake Anaconda
>>> # and pandas with the version "1.3.*"
>>> # and dateutil with the local version in your environment
>>> session.custom_package_usage_config = {"enabled": True}  # This is added because latest dateutil is not in snowflake yet
>>> session.add_packages("numpy", "pandas==1.5.*", dateutil)
>>> @udf
... def get_package_name_udf() -> list:
...     return [numpy.__name__, pandas.__name__, dateutil.__name__]
>>> session.sql(f"select {get_package_name_udf.name}()").to_df("col1").show()
----------------
|"COL1"        |
----------------
|[             |
|  "numpy",    |
|  "pandas",   |
|  "dateutil"  |
|]             |
----------------

>>> session.clear_packages()
Copy

Note

1. This method will add packages for all UDFs created later in the current session. If you only want to add packages for a specific UDF, you can use packages argument in functions.udf() or session.udf.register().

2. We recommend you to setup the local environment with Anaconda, to ensure the consistent experience of a UDF between your local environment and the Snowflake server.