GitHub Actions错误:缺少依赖,即使requirements.txt中已安装

0 投票
1 回答
35 浏览
提问于 2025-04-13 02:23

我在运行 GitHub Actions 的时候,出现了一个错误,提示缺少 matplotlib 这个包,尽管它已经在 requirements.txt 文件里作为依赖安装过了。

name: install and run test package

on: [push]

jobs:
  build:

    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest] 
        python-version: ["3.11"]
        
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          # pip install -e .
      - name: Test with pytest
        run: |
          pytest

输出信息:

Collecting matplotlib (from -r requirements.txt (line 3))
  Downloading matplotlib-3.8.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (5.8 kB)
Collecting lightkurve==2.4.1 (from -r requirements.txt (line 4))
  Downloading lightkurve-2.4.1-py3-none-any.whl.metadata (6.0 kB)
Collecting numpy==1.23 (from -r requirements.txt (line 5))
  Downloading numpy-1.23.0.tar.gz (10.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.7/10.7 MB 87.8 MB/s eta 0:00:00
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'done'
Collecting transitleastsquares==1.0.31 (from -r requirements.txt (line 6))
  Downloading transitleastsquares-1.0.31-py3-none-any.whl.metadata (5.4 kB)
Collecting wotan==1.10 (from -r requirements.txt (line 7))
  Downloading wotan-1.10-py3-none-any.whl.metadata (11 kB)
Collecting aesthetic==0.6 (from -r requirements.txt (line 8))
  Downloading aesthetic-0.6.tar.gz (474 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 474.2/474.2 kB 52.7 MB/s eta 0:00:00
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'error'
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [20 lines of output]
      Traceback (most recent call last):
        File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-gjzfg86r/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-gjzfg86r/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-gjzfg86r/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 487, in run_setup
          super().run_setup(setup_script=setup_script)
        File "/tmp/pip-build-env-gjzfg86r/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 13, in <module>
      ModuleNotFoundError: No module named 'matplotlib'
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

这是 requirements.txt 文件的内容:

pre-commit==3.4.0
matplotlib
lightkurve==2.4.1
numpy==1.23
transitleastsquares==1.0.31
wotan==1.10
aesthetic==0.6
flammkuchen==1.0.3
reproject==0.12.0
astroplan==0.9

1 个回答

0
import matplotlib

看起来aesthetic/setup.py文件的开头有一段代码是aesthetic==0.6

这意味着在安装aesthetic之前,必须先安装matplotlib

你看到的错误是在pip install下载和构建包的时候出现的,但这个时候还没有真正安装这些包。虽然你在需求列表里提到了matplotlib,但是在pip尝试构建aesthetic的时候,matplotlib实际上还没有被安装或导入。

为了让这个过程顺利进行,而不需要对aesthetic做任何修改,你需要确保在环境中先安装好matplotlib

比如,可以这样做:

- name: Install dependencies
  run: |
    python -m pip install --upgrade pip
    pip install matplotlib
    pip install -r requirements.txt

撰写回答