管理 Streamlit 应用程序的依赖项

By default, Streamlit in Snowflake environments come with Python, Streamlit, and Snowflake Snowpark installed. How you manage your app’s dependencies differs based on the runtime environment you choose:

  • Container runtimes manage packages with uv (https://docs.astral.sh/uv/). You can specify dependencies in a pyproject.toml (recommended) or requirements.txt file. By default, your app doesn’t have access to a package index like PyPI. Therefore, if you want to edit or specify the versions of your app’s dependencies, you must create an external access integration (EAI). Additionally, you can install packages from wheel files included in your project directory.
  • Warehouse runtimes manage packages with conda (https://docs.conda.io/en/latest/). You can specify dependencies using an environment.yml file or the built-in package picker in Snowsight. You can only install packages from the Snowflake Anaconda Channel (https://repo.anaconda.com/pkgs/snowflake/).

To learn how to add or edit files in your deployed app, see Edit your Streamlit app.

Supported dependency sourcesWarehouse runtimeContainer runtime
PyPI or other external “simple” (https://peps.python.org/pep-0503/) package indexesNoYes (with EAI)
Snowflake Anaconda ChannelYes, with limitations on Streamlit versionsNo
Internal stageNoYes, but only via relative paths within the app’s source files
Snowflake Artifact Repository (snowflake.snowpark.pypi_shared_repository)NoNo

受支持的 Python 版本

Newly created Streamlit in Snowflake apps run in Python 3.11 by default.

  • 对于容器运行时,Python 3.11 是目前唯一支持的版本。
  • 对于仓库运行时,您可以选择 Python 3.9、3.10 或 3.11。

支持的 Streamlit 版本

Newly created Streamlit in Snowflake apps use the latest supported version of Streamlit available in their runtime environment. When a new version of Streamlit is released, there might be a delay before the new version becomes the default.

您可以通过包索引安装最新的 Streamlit 版本,从而立即使用该版本。

  • For warehouse runtimes, you are limited to a subset of versions starting from 1.22.0. streamlit-nightly versions aren’t supported.

无法立即在仓库运行时中使用最新的 Streamlit 版本。

为防止意外的包升级,请按照本页所述配置应用程序的依赖项。

仓库运行时中支持的 Streamlit 库版本

Streamlit in Snowflake supports the following versions of the Streamlit open-source library:

  • 1.52.2
  • 1.52.1
  • 1.52.0
  • 1.51.0
  • 1.50.0
  • 1.49.1
  • 1.48.0
  • 1.47.0
  • 1.46.1
  • 1.45.1
  • 1.45.0
  • 1.44.1
  • 1.44.0
  • 1.42.0
  • 1.39.0
  • 1.35.0
  • 1.31.1
  • 1.29.0
  • 1.26.0
  • 1.22.0

非 Python 依赖项

Some Python packages require non-Python system libraries to be installed in the runtime environment. For example, the Pillow package requires libraries for handling different image formats.

  • For non-Python dependencies in container runtimes, you can only use the pre-installed system libraries. Installing additional non-Python dependencies isn’t supported yet.
  • For non-Python dependencies in warehouse runtimes, some system libraries are available in the Snowflake Anaconda Channel.

声明依赖项的最佳实践

声明应用程序的依赖项时,请考虑以下最佳实践:

  • 固定关键包版本。

    • For container runtimes, use the == operator in pyproject.toml or requirements.txt files.
    • For warehouse runtimes, use the = operator in environment.yml files.
  • 使用版本范围以提高灵活性。

    • For container runtimes, use the <, <=, >=, and > operators in pyproject.toml or requirements.txt files.
    • For warehouse runtimes, use * wildcard suffixes in environment.yml files.
  • 尽量减少依赖项列表,以缩短构建时间。

  • 在部署之前测试开发中的依赖项变更。

  • 确保依赖项与运行时中的 Python 版本兼容。

在运行时之间迁移或更改包管理器时,请查看依赖项名称。例如,某些包在 Conda 和 PyPI 之间具有不同的名称:

PackageConda NamePyPI Name
PillowpillowPillow
OpenCVopencvopencv-python
PyYAMLpyyamlPyYAML

管理容器运行时的依赖项

容器运行时应用程序需要外部访问集成 (EAI) 以从外部包索引(例如 PyPI)安装包。没有 EAI,您只能使用运行时附带的包或包含在应用程序源文件中的包。

即使你只想指定 Streamlit 的版本,也必须在应用程序中包含一个 EAI。没有 EAI,如果尝试在预安装的包上使用版本说明符,则在更新运行时基础镜像时可能会遇到错误。这是因为版本说明符可能不再与预安装的包兼容。

容器运行时的外部访问集成

For a general overview of external access integrations (EAIs), see External network access overview.

PyPI EAI

PyPI is the default package index used by uv to install Python packages in your container runtime. Snowflake provides a managed network rule that simplifies creating an EAI for PyPI. Your account administrator can use this rule to create a PyPI EAI and grant your role access to it. For setup instructions, see Set up a PyPI EAI for app developers.

If you need to use a private or authenticated package repository such as JFrog Artifactory, your administrator must create a custom EAI with the appropriate network rule and authentication secrets. For an example, see Example: Authenticate to a private JFrog Artifactory repository.

After your administrator has created a PyPI EAI and granted your role USAGE on it, you need to add it to your Streamlit object. You can do this in Snowsight or with SQL:

  1. Sign in to Snowsight.
  2. In the navigation menu, select Projects » Streamlit, and then select your app.
  3. In the upper-right corner, select Three vertical dots indicating more options (more options) » App settings.
  4. In the App settings dialog, select the External networks tab.
  5. 从可用 EAIs 列表中,选择 PyPI 的 EAI。
  6. To save the change and close the dialog, select Save.

依赖项文件

Container runtimes use uv for fast, reliable dependency resolution. uv works like pip to install Python packages, but it’s more performant and customizable. For more information about uv’s features, see the Features (https://docs.astral.sh/uv/getting-started/features/) overview in the uv documentation.

容器运行时在与应用程序入口点文件相同的目录中搜索依赖项文件。如果未找到依赖项文件,搜索将继续沿目录树向上进行,直到到达应用程序源位置的根目录。找到的第一个依赖项文件用于安装应用程序的依赖项。

当同一目录下存在多个依赖文件时,它们将按以下优先级顺序使用:

  • requirements.txt: Lists the Python packages and versions required by your Streamlit app, including Streamlit itself. You can’t configure your Python version with requirements.txt.

    For more information about the format of requirements.txt, see Requirements File Format (https://pip.pypa.io/en/stable/reference/requirements-file-format/) in the pip documentation.

  • pyproject.toml (recommended): Manages your Python version and dependencies. Currently, only Python version 3.11 is supported. When you provide a pyproject.toml file, uv will generate a uv.lock file to lock your dependency versions. This lock file will be updated whenever you update your dependencies. You must use pyproject.toml if you want to use a different package index than PyPI.

    For more information about the format of pyproject.toml, see Writing your pyproject.toml (https://packaging.python.org/en/latest/guides/writing-pyproject-toml/) in the Python documentation.

requirements.txt is the simplest way to declare your app’s dependencies and is provided for the convenience of getting started. However, for more advanced dependency management, Snowflake recommends using pyproject.toml instead. For example, this lets you lock dependency versions to ensure that your builds are reproducible.

Tip

  • You can install a package from any URL if you have the necessary EAI assigned to your app. URLs requiring authentication must support embedded credentials.
  • You can install a package from within your project directory by using a relative path from the dependency file to a wheel file.
  • If you use version specifiers on pre-installed packages, you must have an EAI to a package index to avoid errors when the runtime base image is updated.
  • In your local project directory with uv installed, you can run uv init --bare to generate a minimal pyproject.toml file to edit.

通常,您的入口点文件和依赖项文件会位于项目目录的根目录下。但是,您的入口点文件可以位于子目录中,而您的依赖项文件可以位于同一目录或直至项目根目录的任何父级目录中。

例如,您的项目目录可能具有以下结构之一:

source_directory/
 requirements.txt
 streamlit_app.py
source_directory/
 pyproject.toml
 streamlit_app.py
 uv.lock
source_directory/
 pyproject.toml
 subdirectory/
    streamlit_app.py
 uv.lock
source_directory/
 subdirectory/
     pyproject.toml
     streamlit_app.py
     uv.lock

Note

The container runtime will use the directory containing the dependency file as its working directory for uv. Therefore, if you use a relative path to install a package from among your app source files, the path should be relative to the dependency file location. For more information about declaring package sources, see Dependency sources (https://docs.astral.sh/uv/concepts/projects/dependencies/#dependency-sources) in the uv documentation.

PyPI 依赖项文件示例

Your pyproject.toml file must include a name and version to be in a valid format for uv, but their values can be arbitrary. Use requires-python to set your Python version, even though container runtimes only support Python 3.11 for now. Use dependencies to list your Python packages for your container runtime.

Tip

Install Streamlit as streamlit[snowflake] to include its Snowflake connector dependencies (snowflake-snowpark-python).

If you have an EAI for PyPI, the following pyproject.toml file declares a minimum Python version of 3.11 and includes five Python packages which will be installed from PyPI:

[project]
name = "my-streamlit-app"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
    "streamlit[snowflake]==1.50.0",
    "pandas>=2.0.0",
    "plotly>5.0.0",
    "requests>2.0.0,<3.0.0"
]

As an alternative to pyproject.toml, you can use a requirements.txt file to declare your app’s dependencies. The following requirements.txt contains the same Python packages as the previous pyproject.toml example:

streamlit[snowflake]==1.50.0
pandas>=2.0.0
plotly>5.0.0
requests>2.0.0,<3.0.0

Note

To pin a version of a package, you must use the == operator. To specify a version range, you must use <, <=, >=, and > operators. For example, pandas>=2.0.0,<3.0.0 will install any version between 2.0.0 and 2.99.99. For more information, see Dependency specifiers (https://packaging.python.org/en/latest/specifications/dependency-specifiers/).

JFrog 依赖项文件示例

为了提高安全性,系统管理员可能会要求您使用精选或专用包索引,例如 JFrog Artifactory。这是容器运行时的独有功能。使用 JFrog,您可以创建一个公共或专用包索引,用于代理 PyPI 或托管自定义包。这使您能够控制哪些包及版本可供您的 Streamlit 应用程序使用。

To specify a package index, you must use pyproject.toml. For more information, see Using alternative package indexes (https://docs.astral.sh/uv/guides/integration/alternative-indexes/) in the uv documentation.

The following pyproject.toml file declares a minimum Python version of 3.11, includes five Python packages, and specifies JFrog as the package index that proxies PyPI:

[project]
name = "my-streamlit-app"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
    "streamlit[snowflake]==1.50.0",
    "pandas>=2.0.0",
    "plotly>=5.0.0",
    "requests>2.0.0,<3.0.0"
]

[[tool.uv.index]]
name = "jfrog"
url = "<server_name>.jfrog.io/artifactory/api/pypi/<repository_key>/simple"
default = true

If your JFrog repository requires authentication, generate a personal access token or get a scoped token from your JFrog system administrator. Then, include the token in the URL. Don’t use your JFrog password in the URL. In this case, the [[tool.uv.index]] table in the previous example would be replaced with the following:

[[tool.uv.index]]
name = "jfrog"
url = "https://<username>:<access_token>@<server_name>.jfrog.io/artifactory/api/pypi/<repository_key>/simple"
default = true

管理仓库运行时的依赖项

Warehouse runtimes use conda to manage your app’s dependencies. You can declare your dependencies using an environment.yml file or the built-in package picker in Snowsight. Dependencies are installed from the Snowflake Anaconda Channel (https://repo.anaconda.com/pkgs/snowflake/), which includes both Python packages and some non-Python system libraries.

The Snowflake Anaconda Channel contains more versions of Streamlit than are supported in Streamlit in Snowflake warehouse runtimes. To avoid compatibility issues, only use versions of Streamlit that are listed in 仓库运行时中支持的 Streamlit 库版本. Otherwise, you may install any other package available in the Snowflake Anaconda Channel.

environment.yml file

To install dependencies in your warehouse runtime environment using an environment.yml file, create or edit the file in the root of your app’s source location. If you don’t provide an environment.yml file, Snowflake uses only the pre-installed packages for your selected environment. For more information about the structure of environment.yml, see the conda documentation (https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-file-manually).

The following limitations apply when using environment.yml files in Streamlit in Snowflake warehouse runtimes:

  • 您只能使用 Snowflake Anaconda Channel 来安装包。
  • You can only use the Streamlit versions listed in 仓库运行时中支持的 Streamlit 库版本.
  • You can’t declare pip packages in the dependencies section, including relative paths to local packages.

The following environment.yml declares Python 3.11 and five Python packages:

name: my-streamlit-app
channels:
  - snowflake
dependencies:
  - python=3.11
  - streamlit=1.50.0
  - pandas=2.*
  - plotly=5.0.*
  - requests
  - snowflake-snowpark-python

Snowflake 建议固定 Streamlit 的某个版本,以防止当 Snowflake Anaconda 通道中发布新版本的 Streamlit 时,升级该应用程序。

Note

To pin a version of a package, you must use the = operator. To specify a version range, you must use * wildcards. For example, pandas=2.* will install any version of pandas between 2.0.0 and 2.99.99.

使用 conda 进行本地开发

When developing your warehouse-runtime app locally with conda, you must include additional details in your environment.yml file to ensure the dependencies are installed correctly.

  • Identify the Snowflake Anaconda Channel by its URL: https://repo.anaconda.com/pkgs/snowflake.
  • 屏蔽默认通道。

In your environment.yml file, use the following two channels:

channels:
  - https://repo.anaconda.com/pkgs/snowflake
  - nodefaults

If defaults appears in your ~/.condarc file, comment it out:

channels:
  # - defaults

Snowsight package picker

Besides editing the environment.yml file directly for your warehouse-runtime app, you can also use the built-in package picker in Snowsight to add or remove packages from your app’s environment. The package picker is only available for apps using warehouse runtimes. Additionally, the package picker only displays packages compatible with the current Python version of your app. Some system libraries that are independent of Python version might not be shown in the package picker and must be added manually to environment.yml.

  1. Sign in to Snowsight.

  2. In the navigation menu, select Projects » Streamlit, and then select your Streamlit app.

  3. In the upper-right corner, select Edit.

  4. In the upper-left corner of the editor pane, select Packages.

    A drop-down pane appears with the Anaconda Packages tab selected.

  5. 执行以下任意操作:

  • To set the Python version, in the Python version selector, choose the desired version.
  • 要添加包,使用搜索栏按名称查找包,然后选择所需的包。
  • To remove a package, in the Installed Packages section, select the x icon to the right of the package version.
  • To set the version of an installed package, in the Installed Packages section, use the version selector next to the package name.

Snowflake updates your environment.yml file automatically and reboots your app. If you have the environment.yml file open in the editor, refresh the page to see the changes.