3

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)

11
  • Which version of ubuntu is this? Commented Aug 3, 2015 at 17:36
  • 1
    Do you get the same results when you use python as root: sudo python -c "import sys; print sys.path"? Commented Aug 3, 2015 at 17:38
  • Yep: user@ubuntu:~$ sudo python -c "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'] Commented Aug 3, 2015 at 17:39
  • 2
    You say that psycopg2 is in your sys.path, i.e., sys.path contains /usr/local/lib/python2.7/dist-packages. However, I don't see it in the sys.path you show here. I see /usr/local/lib/python2.7/site-packages, perhaps you mixed that one up? Commented Aug 3, 2015 at 17:40
  • 1
    I think the problem is deeper (I've found 14.04 to be botched w.r.t. to Python at times): can you check which pip you are using, and where Python and pip are installed? E.g., the outputs of pip --version, type pip and type python. Commented Aug 3, 2015 at 17:54

2 Answers 2

1

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

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

1 Comment

using: "python get-pip.py" solved everything, thank you.
1

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")

4 Comments

I wouldn't do it this way. This is a one-off solution, but with any next script or use of the Python prompt, you'll have to do the same again. The issue here is with pip installing things in the wrong place, or Python not picking this automatically: pip and Python should be intertwined. If one installs something, the other should be able to find it without a problem.
I removed this as the answer because while it did get me past one error, it caused some more.
Though did it solve this specific question/error? I'd also type which python and make sure that path points to the same distro your pip is installing for.
Are these pointing to the same distro? $ which pip /usr/bin/pip $ which python /usr/local/bin/python.

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.