snowflake.snowpark.Session.add_requirements

Session.add_requirements(file_path: str) None[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.16.0/src/snowflake/snowpark/session.py#L1037-L1087)

Adds a requirement file (https://pip.pypa.io/en/stable/user_guide/#requirements-files) 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 (https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#create-env-file-manually).

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.

Example:

>>> from snowflake.snowpark.functions import udf
>>> import numpy
>>> import pandas
>>> # test_requirements.txt contains "numpy" and "pandas"
>>> session.add_requirements("tests/resources/test_requirements.txt")
>>> @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()
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.

语言: 中文