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

VS Code中导入pytest失败:已用pip安装仍提示ModuleNotFoundError

解决VS Code中pytest导入失败的问题

核心原因

VS Code当前使用的Python解释器,和你执行pip/python -m pip安装pytest的环境不一致,导致模块找不到。

解决步骤

1. 确认pytest安装位置

打开终端执行:

pip show pytest

或者用python -m pip:

python -m pip show pytest

记下输出里的Location字段路径。

2. 切换VS Code的Python解释器

  • 按Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入Python: Select Interpreter。
  • 在弹出列表里,选择和刚才Location路径匹配的解释器(比如虚拟环境内的Python、对应版本的系统Python)。
  • 选择后重启VS Code终端,再尝试导入pytest。

3. 直接在VS Code终端安装pytest

若不确定对应解释器,直接在VS Code终端执行:

python -m pip install pytest

这里的python是VS Code当前终端使用的解释器,安装完成后即可直接导入。

4. 检查虚拟环境(如果使用了的话)

  • 若用了虚拟环境,先在VS Code终端激活它:
    # Windows cmd
    venv\Scripts\activate.bat
    # Windows PowerShell
    venv\Scripts\Activate.ps1
    # Mac/Linux
    source venv/bin/activate
    
  • 激活后执行pip install pytest,安装完成后即可导入。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 07:32:02