You are viewing documentation about an older version (1.12.1). View latest version

snowflake.snowpark.Session.replicate_local_environment

Session.replicate_local_environment(ignore_packages: Set[str] = None, relax: bool = False) None[source]

Adds all third-party packages in your local environment 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.

If you find that certain packages are causing failures related to duplicate dependencies, try adding duplicate dependencies to the ignore_packages parameter. If your local environment contains Python packages that are not available in Snowflake, refer to custom_package_usage_config().

This function is experimental, please do not use it in production!

Example:

>>> from snowflake.snowpark.functions import udf
>>> import numpy
>>> import pandas
>>> # test_requirements.txt contains "numpy" and "pandas"
>>> session.custom_package_usage_config = {"enabled": True, "force_push": True} # Recommended configuration
>>> session.replicate_local_environment(ignore_packages={"snowflake-snowpark-python", "snowflake-connector-python", "urllib3", "tzdata", "numpy"}, relax=True)
>>> @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()
>>> session.clear_imports()
Copy
Parameters:
  • ignore_packages – Set of package names that will be ignored.

  • relax – If set to True, package versions will not be considered.

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.

This function or method is experimental since 1.7.0.