Error installing psycopg2 in a virtualenv environment with pypy

1

I installed the pypy of the following form:

wget https://bitbucket.org/pypy/pypy/downloads/pypy3-v5.10.1-linux64.tar.bz2

tar xf pypy3-v5.10.1-linux64.tar.bz2

virtualenv -p ~/pypy3-v5.10.1-linux64/bin/pypy my-pypy-env

Activated the environment

source ~/.virtualenv/my-pypy-env/bin/activate

I installed some packages:

pip install mongo

pip install numpy

But when I install psycopg2:

pip install psycopg2

Collecting psycopg2
  Using cached psycopg2-2.7.4.tar.gz
    Complete output from command python setup.py egg_info:
    running egg_info
    creating pip-egg-info/psycopg2.egg-info
    writing pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/psycopg2.egg-
info/dependency_links.txt
    writing top-level names to pip-egg-info/psycopg2.egg-
info/top_level.txt
    writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'

Error: pg_config executable not found.

pg_config is required to build psycopg2 from source.  Please add the directory
containing pg_config to the $PATH or specify the full executable path with the
option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

If you prefer to avoid building psycopg2 from source, please install the PyPI
'psycopg2-binary' package instead.

For further information please check the 'doc/src/install.rst' file (also at
<http://initd.org/psycopg/docs/install.html>).


----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-7lSMte/psycopg2/

I tried installing with sudo:

sudo pip install psycopg2

But it generated another error:

The directory '/home/prisvo/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/prisvo/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Ok, so I should just install using -H in front of sudo.

sudo -H pip install psycopg2 :

Requirement already satisfied: psycopg2 in /usr/local/lib/python2.7/dist-packages

It looks like it worked, but by testing:

python prisvo-recommender.py

  File "prisvo-recomender.py", line 1, in <module>
    import banco
  File "/home/prisvo/prisvo-recommendation/corepypy/banco.py", line 1, in <module>
    import psycopg2
ImportError: No module named psycopg2

I do not know what I'm doing wrong or if psycopg2 does not run with pypy. Could someone give me a tip?

    
asked by anonymous 21.02.2018 / 14:54

1 answer

1
A search for packages reveals that psycopg2 is not compatible with pypy (this will happen with many Python packages that have native code together, that is, using the specific cPython API). This information is not in a single authoritative place - I found it scattered as a response in 2 or 3 places (including the Pypy site states that psycopg2 would be an exception to the compiled packet rule and that it would work with pypy. Pypy3 and it was not)

However, they created the psycopg2cffi package, which uses another technology (CFFI) to perform the native calls to the dessert libraries. This one was installed - but not before I have to do a maneuver here.

I saw that you downloaded the pypy3 already compiled independent of the distribution - from the source code. You will probably also need to download its source code - link to have the .h files, if you do not have an "include" folder there. For those of you using pypy3 from the Linux distribution, as in my case, I first needed to install the pypy3 development package - here is a fedora, then sudo dnf install pypy3-devel - I assume that in Debian and Ubuntu the command is apt-get install pypy3-dev there are variations in the package name)

Even with this, pip install psycopg2cffi still gave error - reading the messages I realized that it had not located the pythonconfig.h file in Python. On my system, this file was placed in the /usr/lib64/pypy3-5.5.0/include folder. (On other systems it will stop in a similar folder - maybe just change the version number. Since you have downloaded pypy3 independent of Linux, you may need to download its source code to have .h as well). So I created an environment variable to tell the C compiler to include this include folder. Just type in the shell:

export C_INCLUDE_PATH=/usr/lib64/pypy3-5.5.0/include

Done, so the command pip install psycopg2cffi worked. If you plan to use psycopg2 directly, that's enough - in your Python code you can try something like this:

try:
    import psycopg2
except ImportError:
    import psycopg2cffi as psycopg2

(If you're concerned with code that works in both environments, of course - otherwise just put the second import directly.)

Now, if you are going to use sqlalchemy, or some other framework that imports psycopg2 without going through your code, there is one more step: the line containing import psycopg2 is not in code under your control, and will fail when for execudata. In the case of SQLAlchemy the error occurs when trying to create an engine. The official way of not happening is to put in the connection URL what psycopg2cffi is being used - the URL takes the form:

postgresql+psycopg2cffi://user:password@host:port/dbname

(instead of just postgresql:// ). Alternatively, you can simply fool the framework (if it is not sqlalchemy, or if you want to keep the url unchanged). To do this, after importing psycopg2cffi, we created a "psycopg2" alias for it inside sys.modules - so when Python finds a import psycopg2 it will assume that the module is already imported, and use the ... cffi in place:

import psycopg2cffi
import sys
sys.modules["psycopg2"] = sys.modules["psycopg2cffi"]

(in time never do sudo pip install... - will not solve anything if you are in a virtualenv, and can crash your system if you are not - always install the Python packages with the system package manager, (apt- get in Ubuntu.) Inside a virtualenv you use the pip)

    
21.02.2018 / 18:53