0

I have a fresh debian install with :
+ default 2.6.6 python installed
+ also installed python3 at /opt/python3/
+ installed psycopg2

Problem :
>>> import psycopg2
works with standard python
but not the alternate python /opt/python3/bin/python3.
I presume it is an import path problem, but I don't know how to solve it being a newbie on python.

Mille Bii
  • 185
  • 1
  • 2
  • 11

3 Answers3

1

You need to install psycopg2 separately for your Python 3 installation.

You need to follow the installation instructions for installing from source, using pip or easy_install will be easiest (provided you have the libpq-dev debian package installed).

You cannot reuse the system-installed psycopg2 because that'll only work on Python 2.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I did a separate install for psycopg2 though... from package rather then source. Does it make a difference ? – Mille Bii Feb 22 '13 at 13:45
  • @MilleBii: Yes, if you installed the debian package then that will not be seen by or usable to the Python 3 installation. – Martijn Pieters Feb 22 '13 at 14:03
  • Getting into trouble with installing `libpq-dev`... with the following error `libpq-dev : Depends: libpq5 (= 8.4.13-0squeeze1) but 9.1.7-1~bpo60+1 is to be installed`. So I'm stuck. – Mille Bii Feb 22 '13 at 14:19
  • @MilleBii: If you have a mix of Debian package versions you have a different problem, I'm afraid. To a certain extend using [apt pinning](http://www.argon.org/~roderick/apt-pinning.html) can help there. You could try and get un-stuck at http://unix.stackexchange.com/ perhaps. – Martijn Pieters Feb 22 '13 at 14:23
  • used `-t squeeze-backports` on the apt-get command to get `libpq-dev` which worked... afterwards the `pip install psycopg2` fails – Mille Bii Feb 22 '13 at 14:27
  • fails *how* exactly? are you using the correct `pip` command (one installed with your Python 3 setup)? – Martijn Pieters Feb 22 '13 at 14:29
  • 1. created the `python3` virtualenv as suggested 2. apt-get install pip 3. apt-get -t squeeze-backports libpq-dev 4. pip install psycopg2 – Mille Bii Feb 22 '13 at 14:35
  • @MilleBii: The `apt-get install pip` command installs `pip` for your *system python only*. You need to install pip for the Python 3 installation. – Martijn Pieters Feb 22 '13 at 14:38
  • getting compilation error the first one is : `In file included from psycopg/psycopgmodule.c:27: ./psycopg/psycopg.h:30:20: error: Python.h: No such file or directory ` – Mille Bii Feb 22 '13 at 14:38
  • @MilleBii: That's because it is compiling for the system python version (for which the headers need to be installed separately). Use tools that come with your Python 3 installation *only*. – Martijn Pieters Feb 22 '13 at 14:41
  • I did activate python3 virtualenv though... so I thought the `apt-get pip` was going to install it on python3 – Mille Bii Feb 22 '13 at 14:44
  • 1
    @MilleBii: `apt-get` is not a python tool, it knows nothing about python virtualenv; it installs *only* for python versions that are themselves managed by `apt`. You need to install `pip` by hand; use [`get_pip.py`](http://www.pip-installer.org/en/1.3.X/installing.html#using-get-pip) and run that with your python 3 binary. – Martijn Pieters Feb 22 '13 at 15:06
  • got PIP installed at the right place. But `/opt/python3/bin/pip install psycopg2` still give the same results... now the very first log is ` Running setup.py egg_info for package psycopg2 no previously-included directories found matching 'doc/src/_build' `. Looks like there is still an env. pb – Mille Bii Feb 22 '13 at 15:25
  • I looked at the details in pip.log and there is an error in the gcc commands its taking the headers from the wrong python ie 2.6 and even at the wrong location. Where it should be `include/python3.3m` it uses `include/site/python6` – Mille Bii Feb 22 '13 at 15:51
  • solution to install pip is [here](http://stackoverflow.com/questions/10763440/how-to-install-python3-version-of-package-via-pip). Then use `pip-3.3 install psycopg2` – Mille Bii Feb 22 '13 at 16:13
1

I had the same issue and installing the python3 include files sorted the issue (while in the active virtualenv)

sudo apt-get install python3-dev as per solution provided in this post

Psycopg2 fails to install on python 3 with pip issuing a fatal error

Community
  • 1
  • 1
campervancoder
  • 1,579
  • 2
  • 11
  • 15
0

So I recap for the record and any one stumbing on this
1. Install virtualenv. instructions here
2. Install pip for your version, in my case it was pip-3.3 instructions here in order to get distribute_setup.py and get-pip.py
3. enjoy > pip-3.3 install psycopg2

Community
  • 1
  • 1
Mille Bii
  • 185
  • 1
  • 2
  • 11
  • 1
    I've done upgrading pip to `1.4.1` and installed pip-3.2, but still can't `pip-3.2 install psycopg2` with error: `./psycopg/psycopg.h:30:20: fatal error: Python.h: No such file or directory compilation terminated. error: command 'gcc' failed with exit status 1`. Using Ubuntu 11.10. – boldnik Oct 15 '13 at 22:28