Jupyter显示已安装module is not defined的问题

发布于 2023-09-18  284 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

问题描述

Python已经安装过的包在jupyter中无法import,例如显示:
NameError: name 'openpyxl' is not defined

原因

jupyter的sys.path是基于整个Anaconda的路径,例如:
C:\Program Files\Python39\
C:\Program Files\Python39\Scripts\
如果需要import的包安装在虚拟环境下,基于Anaconda路径的jupyter是找不到在虚拟环境下的包的,因为虚拟环境的路径更底层。于是jupyter无法直接import,会报ModuleNotFoundError: No module named 'xxx'的错误。
在这种情况下,jupyter import失败,但虚拟环境中的python命令行可以正常import,就是因为python的路径是基于Anaconda的。
查看方法:

import sys
print(sys.path)

安装修正

安装时使用管理员权限

解决方案

This PC->Properties->Advanced system settings

Advanced-Environment Variables

System variables and User variables for AQ84510->Path->Edit->append C:\Program Files\Python39\Lib\site-package(路径取决于自己安装python的位置)
*由于此处我使用的电脑属于学校特有的账号,所以上方用户的环境变量同样需要添加此路径,如果是自己的笔记本则不需要。

届ける言葉を今は育ててる
最后更新于 2024-01-23