Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psycopg2 module cannot be found by Python2.7

I installed psycopg2 via pip, but my programs are having trouble finding it. So, I tried to install psycopg2 via pip again:

user@ubuntu:~/Desktop/progFolder$ sudo pip install psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 in /usr/local/lib/python2.7/dist-packages
Cleaning up...

Then I tried to use a program that imports it:

user@ubuntu:~/Desktop/progFolder$ python myProg.py
Traceback (most recent call last):
  File "myProg.py", line 6, in <module>
    import psycopg2
ImportError: No module named psycopg2

And I have tried just importing directly in python:

user@ubuntu:~/Desktop/progFolder$ python
Python 2.7.5 (default, Nov  9 2014, 14:14:12) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named psycopg2

So I printed my python path.

>>> import sys
>>> print sys.path
['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']

And noticed that the path does contain the path to psycopg2.

psycopg2 in /usr/local/lib/python2.7/dist-packages

So, I have no idea on why this is happening. Any help would be appreciated.

UPDATE: I have done

>>>help()
>>>modules

And psycopg2 was not listed among the other modules. (this does not help me but may help you help me)

like image 517
Rorschach Avatar asked Jul 14 '26 00:07

Rorschach


2 Answers

Your pip looks ok (that is, it's the system/default one). Your Python executable, however, is something that didn't come by default with 14.04 LTS (e.g., on my 14.04 system, it's /usr/bin/python). Did you install that Python yourself? Then you need to install (and use) the corresponding pip as well. (Normally, Python would have come with a pip installation, but apparently in this case, it didn't.)

pip can be fairly simple installed from its installation instructions.

Though first, verify that

  • you did install /usr/local/bin/python yourself. That is, it didn't come with some other piece of software that you installed and that, along the way, decided to install Python there.

  • you want to use /usr/local/bin/python (I guess it is a more recent version of Python 2.7; the default 14.04 LTS one appears to be 2.6.7 as of 2015-08-03).

From your python path print, it looks like it doesn't have /usr/local/lib/python2.7/dist-packages included in it. You can add it in one way by:

sys.path.insert(0, "/usr/local/lib/python2.7/dist-packages")

like image 21
FTA Avatar answered Jul 15 '26 12:07

FTA