4

I recently installed psycopg2 to my computer using macport. I followed the instructions on the Psycopg2 website:

sudo port install py27-psycopg2

When I used

port install 

, psycopg2 was listed as successfully installed by macport.

However, whenever I type

import psycopg2

in python, it also give me the errors that there is no such modules in python. I am a novice in computer science. I looked up some questions kind of remsemble mine, but I could figure out what is going on. I hope everyone can help me out with this. I really appreciate your help and your time.

Ha Vu
  • 89
  • 1
  • 8
  • You have more than one python2 installed? what does `which -a python` output? – Padraic Cunningham Sep 15 '15 at 18:32
  • Dear Padraic: My output is: – Ha Vu Sep 17 '15 at 17:10
  • /opt/local/bin/python /opt/local/bin/python /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/local/bin/python /usr/bin/python – Ha Vu Sep 17 '15 at 17:10
  • I am so sorry I am new to stackoverflow so I am rather late in replying – Ha Vu Sep 17 '15 at 17:10
  • ok you have multiple versions of python 2 installed so that is where the problem lies, currently I imagine your system python is the default so when you type python in your shell or run a script with python you are using that interpreter, when you port install you are installing for a different interpreter. If I were you i would just keep your system python and use a virtualenv, having multiple versions installed can be a pain in the ass especially if you are beginning with python. – Padraic Cunningham Sep 17 '15 at 17:15

2 Answers2

1

do

$ sudo port select --list python

This will list all the python installations. e.g:

none
python26-apple
python27 (active)
python27-apple
python33
python34

Now select the python27 that is installed by MacPorts:

sudo port select --set python python27

Now do

which python

For me, macports installed python at

/opt/local/bin/python
kmad1729
  • 1,484
  • 1
  • 16
  • 20
0

Try installing psycopg2 with python's native package manager 'pip', you've already got python installed on your mac, so typing :

 pip install psycopg2

directly into your terminal's command line will install the module in the same python stack you are using to run the rest of your program.

If pip is not working see this answer to install it :

How do I install pip on macOS or OS X?

Community
  • 1
  • 1
NiallJG
  • 1,881
  • 19
  • 22