6

I can import pyscopg2 in my python console but not in my jupyter notebook.

I installed psycopg2 through the anaconda-navigator.

Jupyter notebook:

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 console:

python3

>>> import psycopg2

No problem here.

>>> 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 list:

# 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  
...

I tried:

Adding it to sys.path but the same result is occuring.

sys.path.insert(0,'/usr/local/anaconda3/envs/env3/lib')
3
  • How did you install it with what command ? (Just making sure the official docs say the following conda install -c anaconda psycopg2 anaconda.org/anaconda/psycopg2 Commented May 6, 2019 at 20:07
  • I used the GUI in https://docs.anaconda.com/anaconda/navigator/. But I also tried conda install -c anaconda psycopg2. Commented May 6, 2019 at 20:19
  • It would have been helpful to show us the output from psycopg2.__spec__, which would tells us where it gets loaded from in the console case. Commented May 7, 2019 at 14:27

1 Answer 1

7

The solution was to just install with pip outside my virtual conda environments.

pip install psycopg2
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks - very odd but same thing happened to me and only with this module.
In a Jupyter notebook cell, !pip install psycopg2-binary worked for installing psycopg2.
Installing psycopg2 outside my venv also worked for me, but I still don't understand why I had to do that given that I started my jupyter server after activating my venv.
I don't know why, but it works. Really eager to know the reason. Besides, what singhj mentioned not work for me.
Thank you @singhj for !pip install psycopg2-binary!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.