家庭PC搭建Python与Jupyter遇Failed to Fetch错误求助
问题解决与配置指南
环境信息
Selected Jupyter core packages... IPython : 8.26.0 ipykernel : 6.29.5 ipywidgets : not installed jupyter_client : 8.6.2 jupyter_core : 5.7.2 jupyter_server : 2.14.2 jupyterlab : 4.2.4 nbclient : 0.10.0 nbconvert : 7.16.4 nbformat : 5.10.4 notebook : 7.2.1 qtconsole : not installed traitlets : 5.14.3
一、修复Jupyter "Failed to Fetch" 错误
- 清理Chrome缓存与Cookie:按下
Ctrl+Shift+Delete,勾选「缓存的图片和文件」「Cookie及其他网站数据」,选择「所有时间」后执行删除,重启浏览器再打开Jupyter - 重启Jupyter服务:终端按
Ctrl+C终止当前服务,重新执行jupyter notebook - 检查端口占用:
- Windows:运行
netstat -ano | findstr :8888,找到PID后打开任务管理器结束对应进程,再重启Jupyter;或指定端口启动jupyter notebook --port 8889 - Mac/Linux:运行
lsof -i :8888,用kill -9 <PID>终止进程后重启
- Windows:运行
- 重装核心依赖:在Anaconda终端执行
conda install jupyter notebook ipykernel --force-reinstall - 禁用浏览器扩展:临时关闭Chrome所有扩展程序,再访问Jupyter页面测试
二、Python与Jupyter基础操作
- 环境隔离:用Anaconda创建独立环境
conda create -n py312 python=3.12.4,激活环境conda activate py312 - 内核配置:激活环境后执行
python -m ipykernel install --user --name py312 --display-name "Python 3.12.4 (Anaconda)",在Notebook的「Kernel」菜单切换内核 - Notebook常用操作:
Shift+Enter:运行当前单元格并跳转至下一个Ctrl+S:保存Notebook文件%run script.py:在单元格中运行外部Python脚本- 用
#添加注释,print()输出调试信息
三、Jupyter中配置Quarto
- 安装Quarto:在Anaconda环境执行
conda install quarto -c conda-forge - 创建Quarto Notebook:在Jupyter界面点击
File > New > Quarto Notebook,或终端执行quarto create notebook gmail_auto.ipynb - 配置输出格式:在Notebook顶部的YAML块中定义渲染参数,示例:
--- title: "Gmail清理自动化练习" format: html: toc: true code-fold: true theme: cosmo ---
- 渲染文档:终端执行
quarto render gmail_auto.ipynb --to html,或在Jupyter界面点击「Render」按钮生成HTML报告
四、Gmail清理自动化开发
- 安装依赖:在环境中执行
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib - API配置:在Google Cloud控制台创建项目,启用Gmail API,下载
credentials.json文件到Notebook工作目录 - 核心代码示例:
from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request import pickle import os.path # 权限范围,modify允许修改邮件 SCOPES = ['https://www.googleapis.com/auth/gmail.modify'] def get_gmail_service(): creds = None # 读取已保存的令牌 if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # 无有效令牌则重新授权 if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) creds = flow.run_local_server(port=0) # 保存令牌 with open('token.pickle', 'wb') as token: pickle.dump(creds, token) return build('gmail', 'v1', credentials=creds) def delete_spam_emails(service): # 获取垃圾邮件列表 results = service.users().messages().list(userId='me', labelIds=['SPAM']).execute() messages = results.get('messages', []) if not messages: print("无垃圾邮件") return # 批量删除垃圾邮件 for msg in messages: service.users().messages().delete(userId='me', id=msg['id']).execute() print(f"已删除{len(messages)}封垃圾邮件") if __name__ == '__main__': service = get_gmail_service() delete_spam_emails(service)
- Jupyter调试技巧:将代码拆分到多个单元格,逐块运行并检查输出;用
service.users().labels().list(userId='me').execute()查看所有邮件标签,确认目标标签ID
内容的提问来源于stack exchange,提问作者RAMO46
相关产品推荐
相关产品推荐

