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

终端启动Jupyter Notebook时Python环境不符导致导入报错咨询

问题描述

在装有Python 3.8的conda环境中启动jupyter notebook,导入包时出现以下报错:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-7-2b...8b8> in <module>
----> 1 from blah import reference

~/work/blah/blah/__init__.py in <module>
     64 
     65 # Private methods for use here
---> 66 from ._internals.api import configure as _configure
     67 from ._internals.api.utils.warnings import PatchWarning as _PatchWarning
     68 

~/work/blah/blah/_internals/api/configure.py in <module>
     10 from typeguard import typechecked
     11 
---> 12 from blah._internals.rest import user
     13 from blah._internals.api.utils.authentication import get_token
     14 from blah._internals.api.utils.exceptions import BmllError

~/work/blah/blah/_internals/rest.py in <module>
     13 from functools import wraps
     14 from http import HTTPStatus
---> 15 from typing import Dict, Optional, TypedDict
     16 
     17 from requests import request

ImportError: cannot import name 'TypedDict' from 'typing' (/home/pchong/anaconda3/lib/python3.7/typing.py)

从报错最后一行可知,Jupyter Notebook实际调用的是Python 3.7,与当前conda环境的Python 3.8配置不符。

原因分析
  • 系统全局安装的Jupyter Notebook未关联到当前conda环境,启动时默认调用全局的Python 3.7解释器
  • 当前conda环境未安装Jupyter Notebook,导致启动时自动使用系统全局版本
解决办法

方法一:在conda环境内安装Jupyter Notebook

  1. 激活目标conda环境:
conda activate your_env_name
  1. 在环境内安装Jupyter Notebook:
conda install jupyter notebook
  1. 重新启动Jupyter Notebook:
jupyter notebook

此时启动的Notebook会使用当前环境的Python 3.8解释器。

方法二:为conda环境创建Jupyter内核

如果已全局安装Jupyter,可通过添加内核关联当前环境:

  1. 激活目标conda环境:
conda activate your_env_name
  1. 安装ipykernel:
conda install ipykernel
  1. 将当前环境添加为Jupyter内核:
python -m ipykernel install --user --name your_env_name --display-name "Python (your_env_name)"
  1. 启动Jupyter Notebook后,新建Notebook时选择名为Python (your_env_name)的内核即可。

验证步骤

启动Notebook后,在单元格中运行以下代码,确认当前使用的Python版本:

import sys
print(sys.version)

输出应为3.8.x系列版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 16:10:27