snowflake.snowpark.Session.add_packages¶
- Session.add_packages(*packages: Union[str, module, Iterable[Union[str, module]]]) None [source] (https://github.com/snowflakedb/snowpark-python/blob/v1.26.0/snowpark-python/src/snowflake/snowpark/session.py#L1347-L1411)¶
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 (https://docs.conda.io/en/latest/). 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 (https://packaging.python.org/en/latest/glossary/#term-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 (https://www.python.org/dev/peps/pep-0508/), but currently only the version matching clause (https://www.python.org/dev/peps/pep-0440/#version-matching) (==
) is supported as a version specifier (https://packaging.python.org/en/latest/glossary/#term-Version-Specifier) for this argument. If amodule
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()
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 infunctions.udf()
orsession.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.