Friday, April 8, 2022

[SOLVED] Cannot import psycopg2 from python3 on fedora 27

Issue

I've just upgraded to Fedora 27, and have been unable to get psycopg2 working.

I'd very much appreciate any help anyone can provide.

As a simple test case, I've been executing
>>> import psycopg2
at the interactive shell. This works fine for Python 2.7, but fails for Python 3.x.

With Python 3.4 and 3.5, I get the message:

Traceback (most recent call last): File "", line 1, in ImportError: No module named 'psycopg2'

With Python 3.6, I see:

Traceback (most recent call last): File "", line 1, in File "/home/jazcap53/.local/lib/python3.6/site-packages/psycopg2/init.py", line 50, in from psycopg2._psycopg import ( # noqa ImportError: /home/jazcap53/.local/lib/python3.6/site-packages/psycopg2/.libs/libresolv-2-c4c53def.5.so: symbol __res_maybe_init, version GLIBC_PRIVATE not defined in file libc.so.6 with link time reference

I installed Fedora 27 from DVD-ROM. I find psycopg2 packages located at /usr/lib64/python2.7/site-packages
and
/home/jazcap53/.local/lib/python3.6/site-packages

My Python packages were all either included with Fedora, or installed via dnf. They are:

python3-3.6.3-2.fc27.x86_64
python35-3.5.4-1.fc27.x86_64
python34-3.4.7-1.fc27.x86_64
python2-2.7.14-2.fc27.x86_64

Some packages I have installed that may be relevant are:

python2-devel-2.7.14-2.fc27.x86_64
python3-devel-3.6.3-2.fc27.x86_64
libpqxx-1:5.0.1-2.f27.x86_64
libpqxx-devel-1:5.0.1-2.f27.x86_64
libgcc-7.2.1-2.fc27.x86_64
postgresql-devel-9.6.6-1.fc27.x86_64

P.S.: If I'm asking this question in the wrong place, please direct me to the right place.

Edit: I noticed that:

/usr/lib64/python2.7/site-packages/
contains subdirectories
psycopg2 and
psycopg2-2.7.3-py2.7.egg-info

but

/usr/lib64/python3.4/site-packages/ and
/usr/lib64/python3.5/site-packages/
contain nothing related to psycopg2

and

/usr/lib64/python3.6/site-packages/
contains subdirectory
psycopg2-2.7.3-py3.6.egg-info
but not psycopg2 itself


Solution

As you may have noticed, each version of Python has its own package hierarchy. So the installation on Python 3.6 will not give you access on 3.4 and 3.5.

With that said, there seems to be some problem with the system's standard lib with Python 3.6. One solution to that could be to install without binaries, like this:

python3.6 -m pip uninstall psycopg2
python3.6 -m pip install --no-binary :all: psycopg2

To install psycopg2 on 3.4 and 3.5 you would run these with or without the --no-binary option:

python3.4 -m pip install psycopg2
python3.5 -m pip install psycopg2


Answered By - Harald Nordgren
Answer Checked By - Katrina (WPSolving Volunteer)