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

VS Code已安装Pandas但运行时报ModuleNotFoundError求助

Pandas导入报错:ModuleNotFoundError 解决指南

问题描述

我在Visual Studio Code中安装了Pandas,通过终端执行pip show pandas确认版本为2.2.2,同时Python版本为3.11.9。编辑器中编写import pandas as pd及使用pd.DataFrame时无语法报错,但运行代码时出现如下错误:

Traceback (most recent call last):
File "d:\ProgrammingCode\Python\PYROBOT\pyrobot\robot.py", line 1, in
import pandas as pd
ModuleNotFoundError: No module named 'pandas'

终端执行pip show pandas的结果:

(myenv) (butterfly) D:\ProgrammingCode\Python\PYROBOT>pip show pandas
Name: pandas
Version: 2.2.2
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author:
Author-email: The Pandas Development Team pandas-dev@python.org
License: BSD 3-Clause License

已尝试多种方法3天未解决,怀疑Windows系统路径问题。


核心原因

VS Code运行代码时使用的Python解释器,与终端中激活的虚拟环境(myenv)不一致,导致找不到已安装的Pandas包。


解决步骤

1. 匹配VS Code的Python解释器与虚拟环境

  • 点击VS Code左下角显示的Python版本,打开解释器选择列表
  • 选择终端中激活的myenv虚拟环境对应的Python 3.11.9
  • 若列表中无该环境,按Ctrl+Shift+P打开命令面板,输入Python: Select Interpreter,手动浏览到虚拟环境的Python.exe路径(一般为D:\ProgrammingCode\Python\PYROBOT\myenv\Scripts\python.exe)

2. 验证解释器与pip的对应关系

  • 在VS Code集成终端执行where python(Windows),查看当前Python路径
  • 执行where pip,确认pip路径与Python路径同属myenv虚拟环境
  • 若路径不匹配,直接执行python -m pip install pandas,强制将包安装到当前解释器对应的环境

3. 检查运行配置文件

  • 打开运行和调试面板(Ctrl+Shift+D),点击"创建launch.json文件",选择"Python"环境
  • 修改配置中的pythonPath为虚拟环境的Python.exe路径,示例配置:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Current File",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "pythonPath": "${workspaceFolder}/myenv/Scripts/python.exe"
            }
        ]
    }
    

4. 清理VS Code环境缓存

  • 按Ctrl+Shift+P打开命令面板,输入Python: Clear Workspace Interpreter Cache
  • 重启VS Code后重新选择解释器

5. 调整系统环境变量优先级

  • 打开Windows系统环境变量,找到Path变量
  • 将myenv虚拟环境的Scripts目录(D:\ProgrammingCode\Python\PYROBOT\myenv\Scripts)移到系统Python路径的上方,确保虚拟环境优先级更高

内容的提问来源于stack exchange,提问作者Nguyễn Lê

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 23:13:14