anaconda中jupyter notebook无法导入已安装的模块

一、使用python时在不同环境中可以正常导入各个环境中存在的模块

  • 基环境及安装的包:安装了flask模块,未安装bcrypt
D:\xxxxyyyy>conda activate base (base) D:\xxxxyyyy>conda list # packages in environment at D:\Anaconda3: # # NameVersionBuildChannel ...... flask1.0.2py37_1 ...... python3.7.1h8c8aaf0_6 ......(base) D:\xxxxyyyy>python >>> import sys; sys.executable 'D:\\Anaconda3\\python.exe' >>> import flask; flask >>> import bcrypt Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'bcrypt'

  • 虚拟环境及安装的包:未安装flask模块,安装了bcrypt模块
D:\xxxxyyyy>conda activate py2(py2) D:\xxxxyyyy>conda list # packages in environment at D:\Anaconda3\envs\py2: # # NameVersionBuildChannel bcrypt3.1.6py27h0c8e037_0 ...... python2.7.16hcb6e200_0 ......(py2) D:\xxxxyyyy>python >>> import sys; sys.executable 'D:\\Anaconda3\\envs\\py2\\python.exe' >>> sys.path ['', 'D:\\Anaconda3\\envs\\py2\\python27.zip', 'D:\\Anaconda3\\envs\\py2\\DLLs', 'D:\\Anaconda3\\envs\\py2\\lib', 'D:\\Anaconda3\\envs\\py2\\lib\\plat-win', 'D:\\Anaconda3\\envs\\py2\\lib\\lib-tk', 'D:\\Anaconda3\\envs\\py2', 'D:\\Anaconda3\\envs\\py2\\lib\\site-packages']>>> import flask Traceback (most recent call last): File "", line 1, in ImportError: No module named flask >>> import bcrypt; bcrypt

  • 此时python解释器版本已变为虚拟环境中的python2版,可以正常使用该虚拟环境才有的模块bcrypt。系统路径已经变成虚拟环境的路径
二、使用jupyter notebook时只能导入基环境的模块
  • 在基环境中启动jupyter notebook
(base) D:\xxxxyyyy>jupyter notebook [I 11:40:01.172 NotebookApp] JupyterLab extension loaded from D:\Anaconda3\lib\site-packages\jupyterlab

In [1]:import sys sys.executable Out[1]:'D:\\Anaconda3\\python.exe' In [2]:import flask flask Out[2]:In [3]:import bcrypt ------ ModuleNotFoundError: No module named 'bcyrpt'

  • 在虚拟环境中启动jupyter notebook
(py2) D:\xxxxyyyy>jupyter notebook [I 11:40:01.172 NotebookApp] JupyterLab extension loaded from D:\Anaconda3\lib\site-packages\jupyterlab

In [1]:import sys sys.executable Out[1]:'D:\\Anaconda3\\python.exe' In [1]:sys.path Out[1]:['D:\\xxxxyyyy', 'D:\\Anaconda3\\python37.zip', 'D:\\Anaconda3\\DLLs', 'D:\\Anaconda3\\lib', 'D:\\Anaconda3',]In [2]:import flask flask Out[2]:In [3]:import bcrypt ------ ModuleNotFoundError: No module named 'bcyrpt'

  • 发现虚拟环境py2的python解释器仍为基环境的python3版本,虚拟环境中存在的bcrypt模块无法导入。系统路径中没有虚拟环境的路径
  • 【anaconda中jupyter notebook无法导入已安装的模块】在py2虚拟环境中也安装上jupyter notebook时,导入正常,路径列表也为py2虚拟环境各个文件夹
因为jupyter notebook是基于多个模块的,如何在虚拟环境中不安装jupyter notebook情况下正常使用该虚拟环境中各个模块,以保持虚拟环境的简洁?????

    推荐阅读