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

Pylance报MissingModuleSource,requests等第三方库无法导入如何解决

修复方案
  • 第一步:确认第三方库已安装
    该报错为Pylance无法定位第三方库的源文件,内置库可正常运行说明Python基础环境无异常,首先执行以下命令安装缺失的第三方库:
pip install requests flask flask-restful pandas

如果设备同时安装了Python2和Python3,将pip替换为pip3执行命令。如果使用虚拟环境/conda环境,需先激活对应环境再执行安装命令。

  • 第二步:匹配编辑器Python解释器
    安装完成后仍有相同报错的话,是编辑器(以VS Code为例)选中的Python解释器和你安装库的解释器不匹配:
    按下Ctrl+Shift+P(Mac系统为Cmd+Shift+P),输入Python: Select Interpreter,在弹出的列表中选择你安装了上述第三方库的对应Python版本,等待Pylance重载完成即可消除报错。
代码修正说明

你提交的代码中文件关闭逻辑存在错误,close(x, "r")为无效写法,正确写法是调用文件对象的close方法:f.close()。更推荐使用with上下文管理器自动管理文件生命周期,避免手动关闭遗漏导致的资源占用问题,修正后的参考代码如下:

import requests 

x = input("write a file copy path: ")
with open(x, "r") as f:
    lines = f.readlines()

fixed_websites = []
for website in lines:
    fixed_website = website.strip("\n")
    fixed_websites.append(fixed_website)
    print(fixed_websites)

for site in fixed_websites:
    r = requests.get(f"https://{site}")
    print(f"the content of: {site} is {r.content}")
    if r.status_code != 200:
        print(f"Error: bad status_code for site: {site}, status_code {r.status_code}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 00:54:02