You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure部署Python(Dash)应用:Pandas编译过慢的优化与版本兼容问题

问题

我有一个Python(Dash) Web应用,requirements.txt内容如下:

dash==2.17.0
numpy==1.26.4
pandas==1.5.3
dash-mantine-components==0.14.3
python-dotenv==1.0.1
dash-iconify==0.1.2
dash-bootstrap-components==1.6.0
pyspark==3.5.1
pytz==2024.1
package @ git+https://${GITHUB_TOKEN}:x-oauth-basic@github.com/package
...

该私有包内的Pipfile内容为:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pandas = "==1.5.3"
pyspark = "==3.5.1"
... other dependencies

[dev-packages]

[requires]
python_version = "3.9"

Azure App Service配置为Python栈,Python版本3.12。使用Azure DevOps流水线构建部署时,单阶段耗时至少20分钟,其中Pandas编译步骤耗时10-12分钟,日志持续显示Building wheel for pandas (pyproject.toml): still running...。

构建部署yml中安装Python依赖的脚本如下:

- script: |
        python -m venv antenv
        source antenv/bin/activate
        python -m pip install --upgrade pip
        pip install setup
        pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
      displayName: 'Install Python dependencies'

流水线使用Python版本3.12(versionSpec: '3.12')。请问如何优化该Azure上Python应用的部署流程?是否存在版本兼容性问题?


解决方案

一、版本兼容性问题分析

  1. Python版本与依赖不匹配

    • pandas 1.5.3官方支持范围为Python 3.7~3.10,不兼容Python 3.12。在3.12环境下安装时,PyPI无对应预编译wheel包,只能从源码编译,这是导致编译耗时超10分钟的核心原因。
    • 私有包Pipfile明确要求Python 3.9,但流水线和App Service使用3.12,版本不匹配不仅拖慢安装,还可能引发运行时未知错误。
  2. 依赖版本连锁问题

    • numpy 1.26.4虽支持Python 3.9~3.12,但搭配pandas 1.5.3在3.12环境下,仍会因pandas的源码编译拖慢整体安装流程。

二、部署流程优化措施

1. 统一Python版本,解决核心兼容性问题

有两种可选方案:

  • 方案A:降级Python版本到3.9
    将Azure App Service和Azure DevOps流水线的Python版本统一设为3.9,匹配私有包Pipfile要求。此时pandas 1.5.3有预编译wheel包,安装速度会大幅提升,无需源码编译。
  • 方案B:升级依赖版本适配Python 3.12
    升级pandas到2.1.0及以上版本(官方从2.1.0开始支持Python 3.12),同时确认dash、dash-mantine-components等其他依赖兼容Python 3.12。同步更新私有包内Pipfile的依赖版本,确保整体环境一致。

2. 优化pip安装命令,移除无效操作

修改流水线中的依赖安装脚本:

- script: |
        python -m venv antenv
        source antenv/bin/activate
        # 升级pip、setuptools、wheel,确保支持预编译包
        python -m pip install --upgrade pip setuptools wheel
        # 强制使用预编译wheel包,避免源码编译
        pip install --target="./.python_packages/lib/site-packages" --only-binary :all: -r ./requirements.txt
      displayName: 'Install Python dependencies'
  • 增加--only-binary :all:参数,强制使用预编译wheel包(前提是Python版本与依赖版本匹配,存在对应wheel)。
  • 移除无效的pip install setup命令,若项目有setup.py,应使用pip install .而非单独安装setup。

3. 启用Azure DevOps依赖缓存

在流水线中添加Cache任务,缓存已安装的依赖包,避免每次构建重复下载:

- task: Cache@2
  inputs:
    key: 'python | "$(Agent.OS)" | requirements.txt'
    restoreKeys: |
       python | "$(Agent.OS)"
    path: ./.python_packages
  displayName: 'Cache Python dependencies'

缓存路径对应pip install --target指定的目录,后续构建优先使用缓存依赖,大幅缩短安装时间。

4. 私有包依赖优化

  • 将私有包内的依赖合并到主项目requirements.txt中统一管理,避免与主项目依赖冲突。
  • 若私有包需单独维护,确保其Pipfile的python_version与流水线、App Service的Python版本一致,同时更新私有包内的依赖版本以适配目标Python版本。

内容的提问来源于stack exchange,提问作者Jason Susanto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 18:14:54