【Jupyter 启动时如何指定目录】

你在 Windows 系统下运行 jupyter notebook 时,遇到了 Jupyter Notebook 打开的目录不是你想要的 E:\desktop\yolo-study ,而是其他路径。这可能是由于 Jupyter 的默认配置问题启动路径问题 导致的。


🔍 原因分析

  1. Jupyter 默认根目录设置错误

    • 你的 jupyter_notebook_config.py 文件设置了:

      python 复制代码
      c.ServerApp.root_dir = 'E:\jupyter notebook file'  # 有问题(注意"\j"错误转义)
    • 这个路径有问题,因为 \j 被识别为无效的转义字符(导致路径解析失败)。

  2. Windows 路径的反斜杠 (\) 问题

    • Windows 通常使用 \,但在 Python/Jupyter 配置中需要使用 /\\ 避免转义:

      python 复制代码
      c.ServerApp.root_dir = r'E:\jupyter notebook file'  # 用 `r` 前缀(推荐,原生字符串)
      # 或者
      c.ServerApp.root_dir = 'E:\\jupyter notebook file'  # 用 `\\` 手动转义
  3. Jupyter 启动时未指定目录

    • 你在 E:\desktop\yolo-study 下运行 jupyter notebook,但 Jupyter 仍然读取了 C:\Users\ZHY\.jupyter\jupyter_notebook_config.py 的配置,导致路径混淆。

🚀 解决方案

1. 启动时明确指定目录

直接在命令行进入 E:\desktop\yolo-study 并启动 Jupyter:

bash 复制代码
# 进入你的目标目录(E:\desktop\yolo-study)
cd /d E:\desktop\yolo-study  # Windows
# 或者
pushd E:\desktop\yolo-study

# 然后启动
jupyter notebook

如果仍跳转到其他目录,可能是 jupyter_notebook_config.py 强制改了目录。


2. 修复 jupyter_notebook_config.py 配置

找到并编辑 C:\Users\ZHY\.jupyter\jupyter_notebook_config.py

python 复制代码
# 修改这两行
c.ServerApp.external_connection_dir = r'E:\jupyter notebook file'
c.ServerApp.root_dir = r'E:\jupyter notebook file'
  • r 前缀 表示 原生字符串(raw string) ,防止 \j 被当成转义字符。

然后重启 Jupyter:

bash 复制代码
jupyter notebook

3. 临时覆盖默认目录(推荐)

如果你不想改配置文件,每次手动指定启动目录:

bash 复制代码
jupyter notebook --notebook-dir="E:\desktop\yolo-study"

或:

bash 复制代码
pushd E:\desktop\yolo-study
jupyter notebook

4. 检查 Jupyter 当前工作目录

确保 Jupyter 运行在正确的路径:

python 复制代码
import os
os.getcwd()  # 在 Jupyter Notebook 单元格里运行,查看当前目录

如果不是你想要的位置,可以 手动调整 cd 命令


⚡ 总结

  1. jupyter_notebook_config.py 里的路径格式错误,导致 Jupyter 使用了错误的工作目录\j 被解析为非法转义字符)。
  2. 解决方案
    • 方法 1 :直接在 E:\desktop\yolo-study 下启动 Jupyter。
    • 方法 2 :修复 jupyter_notebook_config.py 的路径(使用 r'E:\...')。
    • 方法 3 :使用 --notebook-dir="E:\desktop\yolo-study" 指定目录。

📌 最终建议

推荐 pushd E:\desktop\yolo-study && jupyter notebook,这样不用改配置,保证每次启动都进入正确的目录!

如果仍有问题,可以:

  • 删除 C:\Users\ZHY\.jupyter\jupyter_notebook_config.py 重新生成默认配置(jupyter notebook --generate-config)。
  • 或者直接卸载并重装 Jupyter(pip uninstall notebook + pip install notebook)。
相关推荐
全干engineer1 分钟前
Flask 入门教程:用 Python 快速搭建你的第一个 Web 应用
后端·python·flask·web
nightunderblackcat4 分钟前
新手向:Python网络编程,搭建简易HTTP服务器
网络·python·http
李昊哲小课7 分钟前
pandas销售数据分析
人工智能·python·数据挖掘·数据分析·pandas
C嘎嘎嵌入式开发29 分钟前
python之set详谈
开发语言·python
之歆1 小时前
Python-正则表达式-信息提取-滑动窗口-数据分发-文件加载及分析器-浏览器分析-学习笔记
python·学习·正则表达式
往日情怀酿做酒 V17639296381 小时前
pytorch的介绍以及张量的创建
人工智能·pytorch·python
豌豆花下猫2 小时前
Python 潮流周刊#110:JIT 编译器两年回顾,AI 智能体工具大爆发(摘要)
后端·python·ai
专注VB编程开发20年2 小时前
各版本操作系统对.NET支持情况(250707更新)
开发语言·前端·ide·vscode·.net
June bug2 小时前
【Python基础】变量、运算与内存管理全解析
开发语言·python·职场和发展·测试
蹦蹦跳跳真可爱5893 小时前
Python----OpenCV(几何变换--图像平移、图像旋转、放射变换、图像缩放、透视变换)
开发语言·人工智能·python·opencv·计算机视觉