无法在jupyter笔记本中导入psycopg2,但可以在python3控制台中导入

时间:2019-05-06 19:07:51

标签: python jupyter-notebook ipython conda

我可以在python控制台中导入pyscopg2,但不能在jupyter笔记本中导入。

我通过anaconda-navigator安装了psycopg2

Jupyter笔记本

import psycopg2
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-3-7d2da0a5d979> in <module>
----> 1 import psycopg2

ModuleNotFoundError: No module named 'psycopg2'
import sys
sys.path
  

['/ Users / me / Anaconda / MLJSONs',   '/usr/local/anaconda3/lib/python37.zip',   '/usr/local/anaconda3/lib/python3.7',   '/usr/local/anaconda3/lib/python3.7/lib-dynload','',   '/usr/local/anaconda3/lib/python3.7/site-packages',   '/usr/local/anaconda3/lib/python3.7/site-packages/aeosa',   '/usr/local/anaconda3/lib/python3.7/site-packages/IPython/extensions',   '/Users/me/.ipython']

Python控制台:

  

python3

>>> import psycopg2

没问题。

>>> import sys
>>> sys.path
  

['','/usr/local/anaconda3/envs/env3/lib/python37.zip',   '/usr/local/anaconda3/envs/env3/lib/python3.7',   '/usr/local/anaconda3/envs/env3/lib/python3.7/lib-dynload',   '/usr/local/anaconda3/envs/env3/lib/python3.7/site-packages']

Conda列表:

# packages in environment at /usr/local/anaconda3/envs/env3:
#
# Name                    Version                   Build  
...
psycopg2                  2.7.6.1          py37ha12b0ac_0  
python                    3.7.3                h359304d_0  
...

我尝试过:

将其添加到sys.path,但结果相同。

sys.path.insert(0,'/usr/local/anaconda3/envs/env3/lib')

1 个答案:

答案 0 :(得分:0)

解决方案是仅在虚拟conda环境之外使用pip进行安装。

pip install psycopg2