snowflake.snowpark.udf.UDFRegistration.register_from_file¶
- UDFRegistration.register_from_file(file_path: str, func_name: str, return_type: DataType | None = None, input_types: List[DataType] | None = None, name: str | Iterable[str] | None = None, is_permanent: bool = False, stage_location: str | None = None, imports: List[str | Tuple[str, str]] | None = None, packages: List[str | module] | None = None, replace: bool = False, if_not_exists: bool = False, parallel: int = 4, strict: bool = False, secure: bool = False, *, statement_params: Dict[str, str] | None = None, source_code_display: bool = True) UserDefinedFunction [source]¶
Registers a Python function as a Snowflake Python UDF from a Python or zip file, and returns the UDF. Apart from
file_path
andfunc_name
, the input arguments of this method are the same asregister()
. See examples inUDFRegistration
.- Parameters:
file_path – The path of a local file or a remote file in the stage. See more details on
path
argument ofsession.add_import()
. Note that unlikepath
argument ofsession.add_import()
, here the file can only be a Python file or a compressed file (e.g., .zip file) containing Python modules.func_name – The Python function name in the file that will be created as a UDF.
return_type – A
DataType
representing the return data type of the UDF. Optional if type hints are provided.input_types – A list of
DataType
representing the input data types of the UDF. Optional if type hints are provided.name – A string or list of strings that specify the name or fully-qualified object identifier (database name, schema name, and function name) for the UDF in Snowflake, which allows you to call this UDF in a SQL command or via
call_udf()
. If it is not provided, a name will be automatically generated for the UDF. A name must be specified whenis_permanent
isTrue
.is_permanent – Whether to create a permanent UDF. The default is
False
. If it isTrue
, a validstage_location
must be provided.stage_location – The stage location where the Python file for the UDF and its dependencies should be uploaded. The stage location must be specified when
is_permanent
isTrue
, and it will be ignored whenis_permanent
isFalse
. It can be any stage other than temporary stages and external stages.imports – A list of imports that only apply to this UDF. You can use a string to represent a file path (similar to the
path
argument inadd_import()
) in this list, or a tuple of two strings to represent a file path and an import path (similar to theimport_path
argument inadd_import()
). These UDF-level imports will override the session-level imports added byadd_import()
. Note that an empty list means no import for this UDF, andNone
or not specifying this parameter means using session-level imports.packages – A list of packages that only apply to this UDF. These UDF-level packages will override the session-level packages added by
add_packages()
andadd_requirements()
. Note that an empty list means no package for this UDF, andNone
or not specifying this parameter means using session-level packages.replace – Whether to replace a UDF that already was registered. The default is
False
. If it isFalse
, attempting to register a UDF with a name that already exists results in aSnowparkSQLException
exception being thrown. If it isTrue
, an existing UDF with the same name is overwritten.if_not_exists – Whether to skip creation of a UDF when one with the same signature already exists. The default is
False
.if_not_exists
andreplace
are mutually exclusive and aValueError
is raised when both are set. If it isTrue
and a UDF with the same signature exists, the UDF creation is skipped.parallel – The number of threads to use for uploading UDF files with the PUT command. The default value is 4 and supported values are from 1 to 99. Increasing the number of threads can improve performance when uploading large UDF files.
strict – Whether the created UDF is strict. A strict UDF will not invoke the UDF if any input is null. Instead, a null value will always be returned for that row. Note that the UDF might still return null for non-null inputs.
secure – Whether the created UDF is secure. For more information about secure functions, see Secure UDFs.
statement_params – Dictionary of statement level parameters to be set while executing this action.
source_code_display – Display the source code of the UDF func as comments in the generated script. The source code is dynamically generated therefore it may not be identical to how the func is originally defined. The default is
True
. If it isFalse
, source code will not be generated or displayed.
- Note::
The type hints can still be extracted from the source Python file if they are provided, but currently are not working for a zip file. Therefore, you have to provide
return_type
andinput_types
whenpath
points to a zip file.
See also