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

已用pip安装Python库,Visual Studio无法识别的问题咨询

解决Visual Studio共享Python环境无法识别已安装库的问题

问题场景

已通过pip安装termcolor、pyfiglet等Python库,代码导入示例:

import termcolor
print(dir(termcolor))

但在Windows PowerShell中使用Visual Studio共享的Python39_64环境运行代码时,反复触发ModuleNotFoundError,报错信息如下:

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS F:\5\moh.py> & "C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64/python.exe" f:/5/moh.py/modules.py
  File "f:\5\moh.py\modules.py", line 36, in <module>
    import pyfiglet
ModuleNotFoundError: No module named 'pyfiglet'
PS F:\5\moh.py> & "C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64/python.exe" f:/5/moh.py/modules.py
Traceback (most recent call last):
  File "f:\5\moh.py\modules.py", line 36, in <module>
    import termcolor
ModuleNotFoundError: No module named 'termcolor'

解决方法

1. 用目标Python环境的pip安装库

问题本质是pip和运行代码的Python环境不匹配,直接调用Visual Studio共享环境的pip安装依赖:

& "C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64/python.exe" -m pip install termcolor pyfiglet

此命令会将库直接安装到Python39_64环境中,避免路径错位。

2. 确认Visual Studio项目的环境配置

  • 打开Visual Studio并加载目标项目
  • 点击顶部菜单栏 工具 → Python → Python环境
  • 在环境列表中,确保当前项目绑定的是路径为C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64/的环境
  • 若绑定错误,切换到该环境后重新安装库

3. 验证库的安装状态

运行以下命令查看目标环境的已安装库,确认termcolor和pyfiglet存在:

& "C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python39_64/python.exe" -m pip list

4. 重启环境后重试

  • 关闭Visual Studio和PowerShell窗口
  • 重新打开PowerShell运行代码,确保环境加载无误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 09:11:53