How to Solve Error on Installing psycopg2 using pip by using psycopg2-binary instead

Posted on

Introduction

This is an article which has a connection with the previous articles. The first one is the article with the title of ‘How to Solve Error Message ModuleNotFoundError: No module named ‘psycopg2’ in this link. It points out the important of installing psycopg2 for connecting django application to PostgreSQL database. Moreover, it also has a connection with the article titled ‘How to Solve Error on Installing psycopg2 using pip by adding specific version’ in this link.  It has a different stressing. It is focusing on the installation of psycopg2 using pip by giving a specific version. Take a look at the installation of psycopg2 below :

(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 <Python.h>
              ^~~~~~~~~~
    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$ 

Executing the proper solution by installing psycopg2-binary module using pip tool

As in the output of the command execution above, the following is the execution of psycopg2-binary module. This step is taken because it exists as part of the output message above. The following is the message for hinting the alternative solution :

    
    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.

So, in order to solve the problem of the PostgreSQL database connection from django application for an example, the following is the step. The installation of psycopg2-binary module in the python virtual environment is the solution. It also has the additional parameter of version.

(django-env) (base) user@hostname:~/python/my-django/users$ pip install psycopg2-binary==2.7.4
Collecting psycopg2-binary==2.7.4
Downloading https://files.pythonhosted.org/packages/5f/0b/aa7078d3f6d27d951c38b6a1f4b99b71b2caecebb2921b2d808b5bf0e2e0/psycopg2_binary-2.7.4-cp36-cp36m-manylinux1_x86_64.whl (2.7MB)
100% |████████████████████████████████| 2.7MB 194kB/s
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.7.4
(django-env) (base) user@hostname:~/python/my-django/users$

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. For an example, the following command is to check whether the psycopg2-binary is available after the installation process :

(django-env) (base) user@hostname:~/python/my-django$ pip list | grep psycopg2-binary
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
psycopg2-binary (2.7.4)
(django-env) (base) user@hostname:~/python/my-django$

In the above context, the python environment variable is the ‘django-env’. It is a specific environment where the psycopg2-binary module exists after the previous installation using ‘pip’ tool.


Leave a Reply