How to Solve Error on Installing psycopg2 using pip by adding specific version

Posted on

Introduction

This is an article where the main focus is mainly on how to solve an error occurs. The error appears during the psycopg2 module installation. The installation process is using pip tool. Actually, this article has a direct connection with the previous article. That article has a title of ‘How to Solve Error Message ModuleNotFoundError : No module named psycopg2’. It exists in this link location. In that article, there is a problem where the normal installation of psycopg2 is not working. Furthermore, that module is actually an important module for connecting to PostgreSQL database server. For an example connecting django application to PostgreSQL database server. Moreover, the failed installation process of psycopg2 module is available in the following output :

(django-env) (base) user@hostname:~/python/my-django/users$ pip install psycopg2
Collecting psycopg2
  Downloading https://files.pythonhosted.org/packages/5c/1c/6997288da181277a0c29bc39a5f9143ff20b8c99f2a7d059cfb55163e165/psycopg2-2.8.3.tar.gz (377kB)
    100% |████████████████████████████████| 378kB 1.4MB/s 
Building wheels for collected packages: psycopg2
  Running setup.py bdist_wheel for psycopg2 ... error
  Complete output from command /home/user/python/django-env/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nf3652og/psycopg2/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpb8vrk172pip-wheel- --python-tag cp36:
  usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: -c --help [cmd1 cmd2 ...]
     or: -c --help-commands
     or: -c cmd --help
  error: invalid command 'bdist_wheel'
  ----------------------------------------
  Failed building wheel for psycopg2
  Running setup.py clean for psycopg2
Failed to build psycopg2
Installing collected packages: psycopg2
  Running setup.py install for psycopg2 ... error
    Complete output from command /home/user/python/django-env/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nf3652og/psycopg2/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-zxhvlzhf-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/user/python/django-env/include/site/python3.6/psycopg2:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/psycopg2
    copying lib/_json.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/compat.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/tz.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/_range.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/extras.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/errorcodes.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/extensions.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/__init__.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/sql.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/pool.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/_lru_cache.py -> build/lib.linux-x86_64-3.6/psycopg2
    copying lib/errors.py -> build/lib.linux-x86_64-3.6/psycopg2
    running build_ext
    building 'psycopg2._psycopg' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/psycopg
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DPSYCOPG_VERSION=2.8.3 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=100009 -DHAVE_LO64=1 -I/home/user/python/django-env/include -I/usr/include/python3.6m -I. -I/usr/include/postgresql -I/usr/include/postgresql/10/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.6/psycopg/psycopgmodule.o -Wdeclaration-after-statement
    In file included from psycopg/psycopgmodule.c:27:0:
    ./psycopg/psycopg.h:34:10: fatal error: Python.h: No such file or directory
     #include 
              ^~~~~~~~~~
    compilation terminated.
    It appears you are missing some prerequisite to build the package from source.
    You may install a binary package by installing 'psycopg2-binary' from PyPI.
    If you want to install psycopg2 from source, please install the packages
    required for the build and try again., 
    For further information please check the 'doc/src/install.rst' file (also at
    <http://initd.org/psycopg/docs/install.html>).
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    ----------------------------------------
Command "/home/user/python/django-env/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nf3652og/psycopg2/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-zxhvlzhf-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/user/python/django-env/include/site/python3.6/psycopg2" failed with error code 1 in /tmp/pip-build-nf3652og/psycopg2/
(django-env) (base) user@hostname:~/python/my-django/users

Apparently, installing psycopg2 module failed according to the output above. The solution for solving the failed installation of psycopg2 module is simple. Just continue on reading to find the proper solution

Solution for the described problem in the introduction

Actually, there is a simple solution for solving the failed psycopg2 module installation. Just specify the psycopg2 installation version. In the previous part, the instlallation of psycopg2 proceed without specifying a version. The issue compatibility might be the root cause for the failed psycopg2 installation. Just install psycopg2 using the additional version as the following command execution :

(django-env) (base) user@hostname:~/python/my-django/users$ pip install psycopg2==2.7.4
Collecting psycopg2==2.7.4
Downloading https://files.pythonhosted.org/packages/92/15/92b5c363243376ce9cb879bbec561bba196694eb663a6937b4cb967e230e/psycopg2-2.7.4-cp36-cp36m-manylinux1_x86_64.whl (2.7MB)
100% |████████████████████████████████| 2.7MB 378kB/s
Installing collected packages: psycopg2
Successfully installed psycopg2-2.7.4
(django-env) (base) user@hostname:~/python/my-django/users$

In the above output execution, there is an additional parameter specifying the version of psycopg2. The psycopg2 installation as in the above output is a psycopg2 with the version of ‘2.7.4’. Despite of the main purpose for installing the psycopg2 module, the above installation is a success. The installation of the psycopg2 module is using the pip tool. Basically, the psycopg2 module is available in the current active python virtual environment. In the context of the above output, the python virtual environment is the ‘django-env’.

Leave a Reply