snowflake.snowpark.Session.add_requirements

Session.add_requirements(file_path: str, artifact_repository: Optional[str] = None) None[source]

Adds a requirement file that contains a list of packages as dependencies of a user-defined function (UDF). This function also supports addition of requirements via a conda environment file.

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

Parameters:
  • file_path – The path of a local requirement file.

  • artifact_repository – When set this parameter specifies the artifact repository that packages will be added from. Only functions using that repository will use the packages. (Default None). Otherwise, uses the default artifact repository configured in the current context.

Example:

>>> from snowflake.snowpark.functions import udf
>>> import numpy
>>> import pandas
>>> import sys
>>> # test_requirements.txt contains "numpy" and "pandas"
>>> file = "test_requirements.txt" if sys.version_info < (3, 13) else "test_requirements_py313.txt"
>>> session.add_requirements(f"tests/resources/{file}")
>>> @udf
... def get_package_name_udf() -> list:
...     return [numpy.__name__, pandas.__name__]
>>> session.sql(f"select {get_package_name_udf.name}()").to_df("col1").show()
--------------
|"COL1"      |
--------------
|[           |
|  "numpy",  |
|  "pandas"  |
|]           |
--------------

>>> 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 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.