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

GitHub Workflow:Windows下Mambaforge配置Conda环境numpy导入失败求助

解决Windows环境下GitHub Actions中numpy导入失败的问题

问题核心

从错误日志可以看到,Windows环境中实际运行的是PyPy 7.3.9而非指定的CPython 3.8,旧版PyPy对numpy的C扩展兼容存在问题,导致numpy.core._multiarray_umath模块无法加载,进而引发地理依赖库(如geopandas、pandas)的导入失败。

解决方案

1. 强制使用CPython而非PyPy

在environment-dev.yml中明确指定安装CPython版本的Python,避免conda自动选择PyPy:

dependencies:
  # required
  - python=3.8.*=*cpython*  # 强制指定CPython版本
  - osmnx==1.1.1
  - networkx>=2.5
  - numpy==1.20.3
  # 其余依赖保持不变

同时在Workflow的setup-miniconda步骤中添加通道优先级设置,确保优先从conda-forge获取CPython包:

- name: Setup conda environment with mambaforge
  uses: conda-incubator/setup-miniconda@v2
  with:
    use-mamba: true
    activate-environment: ptac-dev
    python-version: ${{ matrix.python-version }}
    condarc-file: ${{ matrix.condarc-file }}
    environment-file: ${{ matrix.environment-file }}
    miniforge-variant: ${{ matrix.miniforge-variant }}
    miniforge-version: ${{ matrix.miniforge-version }}
    conda-channel-priority: strict  # 严格通道优先级

2. 适配Windows环境的Shell配置

当前默认使用bash -l {0}作为shell,在Windows下可能导致conda环境激活不彻底。针对Windows单独配置PowerShell:
在matrix的include部分补充shell配置:

include:
  - os: ubuntu-latest
    environment-file: etc/environment-dev.yml
    miniforge-variant: Mambaforge
    miniforge-version: 4.9.2-4
    shell: bash -l {0}
  - os: windows-latest
    environment-file: etc/environment-dev.yml
    miniforge-variant: Mambaforge
    miniforge-version: 4.9.2-4
    shell: powershell  # Windows使用PowerShell

修改defaults.run为动态读取matrix中的shell配置:

defaults:
  run:
    shell: ${{ matrix.shell }}

3. 确保在激活的conda环境中执行命令

在安装包和测试步骤中,使用conda run明确指定环境,避免环境变量冲突:

- name: Install PtAC (name of my own python package)
  run: |
    conda run -n ptac-dev python setup.py install
    conda run -n ptac-dev conda list
    conda run -n ptac-dev conda info --all

- name: Test code
  run: |
    conda run -n ptac-dev coverage run --source ./ptac --module pytest --verbose
    conda run -n ptac-dev coverage xml -i
    conda run -n ptac-dev coverage report -m

4. 可选:更新numpy版本

如果上述步骤仍有问题,可以尝试将numpy版本更新到1.21.x及以上(该版本对PyPy兼容性更好),但更推荐使用CPython,因为地理类库对CPython的支持更完善。

验证步骤

修改后重新触发GitHub Actions,检查Windows环境日志,确认Python版本为CPython格式(如Python 3.8.13 (default, ...)而非带[pypy]的版本),并验证测试环节numpy导入正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 21:27:12