I've installed psycopg2 with
pip install psycopg2
and it worked just fine. The install output had a couple of warning along the lines of
In file included from ./psycopg/psycopg.h:33:
./psycopg/config.h:71:13: warning: unused function 'Dprintf' [-Wunused-function]
static void Dprintf(const char *fmt, ...) {}
^
1 warning generated.
but in the end it says
Successfully installed psycopg2
and it also appears when I run pip list
.
Now when I try to import it in python I get an error:
$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
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
Why can't Python import the module if it was successfully installed?
(Python 2.7.5 was installed with Homebrew. psycopg2 was installed with pip.)
The installation of psycopg2-binary module in the python virtual environment is the solution. It also has the additional parameter of version. If the above installation process is a success, execute the following command to check the available module. Execute the command ‘pip list’ in the command line.
Many “No module named psycopg2” errors occur due to working on incorrect virtual environments and installing the ‘psycopg2’ on a different environment. Suppose you have two versions of python3 installed, and how will you install ‘psycopg2’ to a specific python?
Copy the code from get-pip.py or save the file from the link. Then simply run the file with python. This should install pip for you and get it working. Make sure to try using pip3 if needed. If all else fails, this has been a reliable way to get pip working on your python install.
This may work if you have multiple versions of python installed, one of them being Python3. One special case you may find yourself in is that the python module used to exist and, for whatever reason, no longer is part of PIP. In this case there is but one solution I have found. This is to try and find a .whl or wheel package for the python module.
OS X comes with Python 2.7.5 already; when you install Python with Homebrew, it puts a newer version in a different place without touching the built-in one. Homebrew's Python also comes with Pip, whereas OS X's doesn't. What's going on here is, you're using Homebrew's pip
to install psycopg2, then running OS X's python
and trying to import it.
Run /usr/local/bin/python
(the full path to Homebrew's Python), and try import psycopg2
from there.
If that works, you need to put /usr/local/bin
before /usr/bin
in your PATH
variable, so that your shell finds Homebrew's Python before the OS X one every time. If you use Bash (the default shell in OS X), you can do this by putting the following in your .bash_profile
:
export PATH=/usr/local/bin:$PATH
To make sure that you're running the right Python in scripts, use the following shebang line:
#!/usr/bin/env python
env
will search PATH
for Python and run the script with the first one it finds, the same as typing python
from your shell. Bash scripts and such should inherit the PATH
variable and find the right python without changes.
You can also hardcode the path to Homebrew Python in your shebang line (#!/usr/local/bin/python
), but this means your script will only work on OS X machines with a Homebrew Python installed, and is best avoided.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With