Jupyter无法导入库,但能在终端导入的问题

Jupyter无法导入库,但能在终端导入

❌错误问题描述:conda activate LLMs激活某个Conda的环境后,尽管已经通过conda或者pip在这个环境中安装了一些🐍Python的库,但无法在Jupyter中导入,却能在终端成功导入。

📚参考自:jupyter notebook添加python内核(windows)

1. 查找错误原因

分别在终端和Jupyter中输入以下Python代码,获取各自方式Python 解释器的可执行路径。

python 复制代码
import sys
sys.executable

1.1 ✔正确的:在终端的结果

'C:\Users\murphystar\miniconda3\envs\LLMs\python.exe'

1.2 ×错误的:在jupyter中的结果

'C:\Users\murphystar\miniconda3\python.exe'

由此,可以看出Jupyter中的kernel解释器路径并不是已经创建LLMs环境的(尽管我已经安装了ipykernel,也已经选择指定的kernel后仍然无效),而是使用了默认的Python解释器的可执行路径。因此,只需要修改LLMs环境的Python解释器的可执行路径即可。

2. 查看Jupyter kernel list

在CMD中检查该环境下是否有kernel,没有的话直接安装。

powershell 复制代码
# 激活目标环境
conda activate LLMs

# 查看kernel版本
python -m ipykernel --version

# 没有的话就安装
# conda install ipykernel -i https://pypi.tuna.tsinghua.edu.cn/simple

在CMD中查看Jupyter kernel list。

powershell 复制代码
jupyter kernelspec list

输出如下:

Available kernels:
llms C:\Users\murphystar\AppData\Roaming\jupyter\kernels\llms

oe_rdkit C:\Users\murphystar\AppData\Roaming\jupyter\kernels\oe_rdkit

pytorch C:\Users\murphystar\AppData\Roaming\jupyter\kernels\pytorch

python3 C:\Users\murphystar\miniconda3\share\jupyter\kernels\python3

base C:\ProgramData\jupyter\kernels\base

conda C:\ProgramData\jupyter\kernels\conda

假如你的目标环境是LLMs ,则可以去到该kernel目录(C:\Users\murphystar\AppData\Roaming\jupyter\kernels\llms)

下修改kernel.json 文件内容中python解释器的路径为1.1中正确路径:C:\Users\murphystar\miniconda3\envs\LLMs\python.exe。

json 复制代码
{
 "argv": [
  "C:\\Users\\ayao5\\miniconda3\\envs\\LLMs\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "LLMs",
 "language": "python",
 "metadata": {
  "debugger": true
 }
}

重启Jupyter后,即可成功生效。🎉🎉🎉🎉

相关推荐
Web3&Basketball4 小时前
决策模型vs GPT-5实测:便宜400倍
人工智能·python·gpt·大模型·ai落地·推理优化·决策模型
ljt27249606614 小时前
Python笔记(四)--模式匹配与正则
笔记·python
小羊没烦恼!4 小时前
T-SQL查询高级—SQL Server索引中的碎片和填充因子
开发语言·前端·javascript·数据库·后端·python·web安全
the局外人4 小时前
学会 SQLAlchemy 搭建 Python 与 MySQL 之间的数据访问桥梁 🚀
后端·python·sql
zh路西法4 小时前
【生成式模型入门】(四):Flow Matching——从速度场学习到生成路径
python·深度学习·神经网络·生成式模型·diffusionpolicy·flowmatching
把我格子衫拿来4 小时前
PnP 位姿估计:从二维像素求三维姿态,为什么 tvec 不是相机位置?
python·opencv·计算机视觉·pnp·相机位姿
benchmark_cc4 小时前
实时行情系统如何避免无意义的重复请求?从请求去重到缓存设计
大数据·开发语言·python·pandas·量化交易·股票数据·quantdash
田里的水稻4 小时前
Python_两大法宝dir() 与 help() 的妙用
开发语言·python
郝学胜-神的一滴4 小时前
Python浏览器自动化:Selenium与Playwright实战对比,网页爬虫&自动操作一站式指南
开发语言·爬虫·python·selenium·程序人生·自动化
Zip-List4 小时前
工作小记-pytorch的算子优化: 转gpu核函数 python调c
c语言·c++·pytorch·python·cuda