0

I have

#!/usr/bin/python3
import psycopg2

in my first couple lines of the code. However, it shows that

ImportError: No module named psycopg2

even after I install psycopg2

Below is the detail errors from the ternimal.

 432p1:/vagrant> pip3 install psycopg2
Collecting psycopg2
  Downloading psycopg2-2.7.3.1-cp35-cp35m-manylinux1_x86_64.whl (2.6MB)
    100% |████████████████████████████████| 2.6MB 526kB/s 
Installing collected packages: psycopg2
Successfully installed psycopg2-2.6.1
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
432p1:/vagrant> ./test-example.py pys.py
Traceback (most recent call last):
  File "./test-example.py", line 2, in <module>
    import psycopg2
ImportError: No module named psycopg2
432p1:/vagrant> ./clean-example.py
Traceback (most recent call last):
  File "./clean-example.py", line 2, in <module>
    import psycopg2
ImportError: No module named psycopg2
432p1:/vagrant> 

In addition i tried to run import in my python console and there is no error, however,when i run the test it has error

432p1:/vagrant> python3
Python 3.5.2 (default, Aug 18 2017, 17:48:00) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> ./test-example.py pys.py 
  File "<stdin>", line 1
    ./test-example.py pys.py 
    ^
SyntaxError: invalid syntax
>>> 

Thanks

Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
overloading
  • 145
  • 9
  • what is your default python version ? – Reza Sep 23 '17 at 15:41
  • likely that the location where psycopg2 is installed (by pip3) is different from the location where python3's packages are located. you would need to resolve the two - personally resolved this by using the correct path for my environment's pip3 (e.g. /User/vidyut/anaconda2/envs/py36/bin/pip install psycopg2) – vsdaking Jan 04 '18 at 06:44

1 Answers1

0

Check if it was actually installed where you expected it to be -- you can compare other packages that have been imported successfully. Run pip show psycopg2 vs pip show pandas or some other package and compare if they're in the right place.

In my case, only the psycopg2 dist files had been installed in that directory for some reason, and so I uninstalled and reinstalled and it worked.

answerzilla
  • 181
  • 2
  • 4