Conda 和预提交,错误:未为“请求”安装库存根(或与 Python 3.10 不兼容)

a.t*_*.t. 6 typing pre-commit conda mypy pre-commit.com

错误信息

在使用 conda 环境时,pre-commit我收到错误:

error: Library stubs not installed for "requests" (or incompatible with Python 3.10)
Run Code Online (Sandbox Code Playgroud)

完整的错误消息是:

rc/export_data/plantuml_get_package.py:69: error: Library stubs not installed for "requests" (or incompatible with Python 3.10)
src/export_data/plantuml_get_package.py:69: note: Hint: "python3 -m pip install types-requests"
src/export_data/plantuml_get_package.py:69: note: (or run "mypy --install-types" to install all missing stub packages)
src/export_data/plantuml_get_package.py:69: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 24 source files)
Run Code Online (Sandbox Code Playgroud)

错误消息中给出了默认的解决方案:

mypy --install-types
Run Code Online (Sandbox Code Playgroud)

然而,要求已经满足:

Installing missing stub packages:
/usr/bin/python3 -m pip install types-requests

Install? [yN] y

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: types-requests in /usr/lib/python3/dist-packages (2.25)
Run Code Online (Sandbox Code Playgroud)

康达·亚米尔

这表明它可能安装在设备上,但不是安装在 conda 环境中,但是,它应该是,因为以下 conda 环境已成功创建(最后 2 行):

# run: conda env create --file environment.yml
# include new packages: conda env update --file environment.yml
name: someenvironment
channels:
  - conda-forge
  - conda
dependencies:
# Specify specific python version.
- python=3.10
- anaconda
- nb_conda
- conda:
# Run python tests.
  - pytest=6.1.2
# Convert notebooks to pdf.
  - nbconvert
  - matplotlib
# Support notebooks.
  - nb_conda
# Visualise graphs for PlantUML
  - graphviz
# Run graph software quickly
  - networkx
- pip
- pip:
# Compile pdf from python.
  - pdflatex
# Write unit tests on Jupyter notebooks.
  - testbook
# Auto generate docstrings
  - pyment
# Auto generate documentation.
  - pdoc3
# Code formatting compliance
  - black
# Generate diagrams.
  - plantuml
# Identify and remove dead code.
  - vulture
# Get PlantUML .jar file.
  - requests
  - types-requests
Run Code Online (Sandbox Code Playgroud)

问题

所以我想知道,如何确保在环境中mypy识别该包?types-requests

a.t*_*.t. 30

在此讨论中找到了解决方案mypy将中的条目 更改为.pre-commit-config.yaml

# Test if the variable typing is correct. (Variable typing is when you say:
# def is_larger(nr: int) -> bool: instead of def is_larger(nr). It makes
# it explicit what type of input and output a function has.
 - repo: https://github.com/pre-commit/mirrors-mypy
   rev: v0.950
   hooks:
    - id: mypy
      verbose: true
      args: [--show-error-codes]
      additional_dependencies: ['types-requests']
Run Code Online (Sandbox Code Playgroud)

产生的输出:

mypy.....................................................................Passed
- hook id: mypy
- duration: 1.3s

Success: no issues found in 24 source files
Run Code Online (Sandbox Code Playgroud)

  • 从心底里感谢:) (6认同)
  • 谢谢,我与 ChatGPT 关于解决这个问题进行了无休止的争论,但它没有帮助 - 回到好的 'ole SO (2认同)