首页 > 解决方案 > Bitbucket:ModuleNotFoundError:没有名为“numpy”的模块

问题描述

我正在尝试使用以下方法在 Bitbucket 中设置单元测试bitbucket-pipelines.yml

image: python:3.7.3

pipelines:
  default:
    - step:
      caches:
        - pip
      script: # Modify the commands below to build your repository.
        - pip3 install --upgrade pip setuptools wheel
        - pip3 install -r requirements.txt
    - step:
      script:
        - python3 -m unittest discover -vp 'Test*.py'

文件 requirements.txt 如下:

tensorflow==2.4.1
Keras==2.4.3
pandas==1.1.3
requests==2.24.0
matplotlib==3.3.2
numpy==1.19.2

numpy 存在于需求中,但是当它运行单元测试时,我收到以下错误:

  import numpy as np
ModuleNotFoundError: No module named 'numpy'

标签: python-3.xbitbucket-pipelines

解决方案


问题是,在第一步中,您使用 pip3 安装依赖项,因为它是不同的容器,所以在下一步中将不可用。如果要保留依赖项,则必须按如下方式缓存它们:

image: python:buster
definitions:
  caches:
    pip3: /usr/local/lib/python3.9/site-packages

路径是可变的,具体取决于您拥有的 python 版本,您可以pip3 show <dependency>在管道中运行命令以验证是否是正确的路径。

命令结果

并且您需要将缓存添加到需要依赖项的步骤中,在您的情况下是第二步:

image: python:3.7.3
definitions:
  caches:
    pip3: <path>
pipelines:
  default:
    - step:
      name: First step
      caches:
        - pip3
      script: # Modify the commands below to build your repository.
        - pip3 install --upgrade pip setuptools wheel
        - pip3 install -r requirements.txt
    - step:
      name: Second step
      caches:
        - pip3
      script:
        - python3 -m unittest discover -vp 'Test*.py'

推荐阅读