Reuse an existing Python package

Snowflake CLI allows you to add existing Python packages to Snowpark imports using the snow snowpark package commands. You can use already implemented packages, such as those from PyPi, in your functions and procedures.

To add a Python package to Snowpark imports, do the following:

  1. Check whether package is not already available using the snow snowpark package lookup command.

    If the --yes flag is provided, this command checks all dependencies of the packages outside the Snowflake channel

    snow snowpark package lookup tomlkit
    
    Copy
    Package tomlkit is available on the Snowflake anaconda channel.
    

    If a package is not available on the Snowflake anaconda channel, you can get a message similar to the following:

    snow snowpark package lookup samrand
    
    Copy
    Lookup for package samrand resulted in some error. Please check the package name or try again with --pypi-download option
    

    If the package exists on PyPi:

    snow snowpark package lookup samrand -y
    
    Copy
    The package samrand is available through PIP. You can create a zip using:
    
                snow snowpark package create samrand --pypy-download
    
  2. Download package and create Snowflake artifact to be uploaded using the snow snowpark package create command.

    This command creates a Python package as a zip file that can be uploaded to a stage and later imported for a Snowpark Python app.

    snow snowpark package create samrand -y
    
    Copy
    Package samrand.zip created. You can now upload it to a stage
    (`snow snowpark package upload -f samrand -s <stage-name>`) and reference it
    in your procedure or function.
    
  3. Upload your package using the snow snowpark package upload command.

    This command uploads a Python package zip file to a Snowflake stage so it can be referenced in the imports of a procedure or function.

    snow snowpark package upload --file="samrand.zip" --stage="packages"
    
    Copy
    Package samrand.zip UPLOADED to Snowflake @packages/samrand.zip.
    
  4. To use the package in procedures or functions, add it to the imports section of Snowpark definition section in snowflake.yml.

    functions:
     - name: "get_custom_package_version"
       handler: "functions.get_custom_package_version"
       signature: ""
       returns: string
       imports:
        - "@packages/samrand.zip"
    
    Copy

    Then import your package in the function handler.

    # functions.py
    import samrand
    
    def get_custom_package_version():
      return samrand.VERSION
    
    Copy