How to Solve Error Message Error loading psycopg2 module: No module named ‘psycopg2’ in Django Application

Posted on

Introduction

Upon executing a Django-based internal service to runn a Django-based application, there is an error message appear. It is because of the error message, the Django-based internal service cannot start properly. The following are the execution of the Django-based internal service :

(env) C:\programming\python\django\myproject>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\app\python39\lib\site-packages\django\db\backends\postgresql\base.py", line 25, in <module>
import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\app\python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\app\python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\app\python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "C:\app\python39\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\app\python39\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "C:\app\python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\app\python39\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\app\python39\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\app\python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\app\python39\lib\site-packages\django\contrib\auth\models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\app\python39\lib\site-packages\django\contrib\auth\base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "C:\app\python39\lib\site-packages\django\db\models\base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\app\python39\lib\site-packages\django\db\models\base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "C:\app\python39\lib\site-packages\django\db\models\options.py", line 207, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\app\python39\lib\site-packages\django\utils\connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
File "C:\app\python39\lib\site-packages\django\utils\connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "C:\app\python39\lib\site-packages\django\db\utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "C:\app\python39\lib\site-packages\django\db\utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "C:\app\python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\app\python39\lib\site-packages\django\db\backends\postgresql\base.py", line 29, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

Actually, the error message appear in the above command execution is very clear. It cannot find module with the name of ‘psycopg2’. The reason is because of the requirement to execute it will need the module with the name of ‘psycopg2’. It is available in the ‘settings.py’ file exist in the folder of the project as follows :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'django_postgres',
        'NAME': 'sysapp',
        'PASSWORD': 'password',
    }
}

Solution

The application can run and receive request for further process by running a Django-based internal service. But it fail to run because it lack of a certain module needed. That module name is ‘psycopg2’. It is a module acts as a module or library for PostgreSQL database connection. The following are steps to handle the error message :

  1. This is the first step by checking the available module or library exist in the current active python virtual environment. Just execute the following command :

    (env) C:\programming\python\django\myproject>pip list
    Package Version
    ---------------------- ---------
    asgiref 3.4.1
    autopep8 1.5.7
    beautifulsoup4 4.10.0
    bs4 0.0.1
    certifi 2021.10.8
    charset-normalizer 2.0.6
    colorama 0.4.4
    commonmark 0.9.1
    dj-database-url 0.5.0
    Django 3.2.6
    django-ckeditor 6.1.0
    django-crispy-forms 1.13.0
    django-js-asset 1.2.2
    docutils 0.17.1
    gunicorn 20.1.0
    idna 3.2
    mysql-client 0.0.1
    mysql-connector-python 8.0.26
    mysqlclient 2.0.3
    Pillow 8.3.2
    pip 21.2.4
    pip-search 0.0.7
    pycodestyle 2.7.0
    Pygments 2.10.0
    python-decouple 3.4
    pytz 2021.1
    requests 2.26.0
    rich 10.12.0
    setuptools 57.4.0
    soupsieve 2.2.1
    sqlparse 0.4.1
    toml 0.10.2
    Unipath 1.1
    urllib3 1.26.7
    whitenoise 5.3.0
    
    (env) C:\programming\python\django\myproject>

    Since there is no psycopg2 module available in the output of the above command, just install it. But first of all, search it whether the module or library with the name of ‘psycopg2’ available or not.

  2. Continue on the first step, just execute the following command to check whether the module or library with the name of ‘psycopg2’ available or not. But make sure to install pip_search module first. It is a module for searching module or packages. The following is the command execution for searching ‘psycopg2’ module :

    (env) C:\programming\python\django\myproject>pip_search psycopg2
    🐍 https://pypi.org/search/?q=psycopg2 🐍
    ┌───────────────────────────────────────────┬─────────┬──────────────┬────────────────────────────────────────────────┐
    │ Package                                   │ Version │ Released     │ Description                                    │
    ├───────────────────────────────────────────┼─────────┼──────────────┼────────────────────────────────────────────────┤
    │ 📂 psycopg                                 3.0.1     Oct 14, 2021   PostgreSQL database adapter for Python 
    │ 📂 psycopg2                                2.9.1     Jun 17, 2021   psycopg2 - Python-PostgreSQL Database Adapter 
    │ 📂 psycopg-binary                          3.0.1     Oct 14, 2021   PostgreSQL database adapter for Python -- C 
    │                                           │         │              │ optimisation distribution                      │
    │ 📂 psycopg2-pgevents                       0.2.2     Apr 10, 2021   PostGreSQL LISTEN/NOTIFY functionality, via 
    │                                           │         │              │ psycopg2                                       │
    │ 📂 psycopg2-utils                          1.0.3     Oct 8, 2021    Utils to use with psycopg2 such as a 
    │                                           │         │              │ connection pool and cursor                     │
    │ 📂 awslambda-psycopg2                      1.0.2     Mar 6, 2020    A aws psycopg2 package from psycopg2. 
    │ 📂 psycopg2-pool                           1.1       Oct 8, 2019    Proper pooling of psycopg2 connections 
    │ 📂 tornado-psycopg2                        0.3.1     Aug 24, 2015   Tornado driver for support asynchronous mode 
    │                                           │         │              │ for psycopg2.                                  │
    │ 📂 psycopg-pool                            3.0       Oct 12, 2021   Connection Pool for Psycopg 
    │ 📂 gevent-psycopg2                         0.0.3     Feb 20, 2012   pip-installable package for patching psycopg2 
    │                                           │         │              │ to use gevent                                  │
    │ 📂 cherrypy-psycopg2                       1.2       Jul 11, 2016   CherryPy tool to manage Psycopg 2 database 
    │                                           │         │              │ connections                                    │
    │ 📂 psycopg-postgis2                        0.0.2     Nov 28, 2016   Pyscopg helpers to work with PostGIS. 
    │ 📂 psycopg2-contextmanager                 1.0.3     Feb 14, 2018   Minimal psycopg2 wrapper for easier 
    │                                           │         │              │ interfacing with databases                     │
    │ 📂 serverless-psycopg2                     0.0.2     Dec 7, 2019    psycopg2 wrapper intended to be used in aws 
    │                                           │         │              │ lambda                                         │
    │ 📂 psycopg2da                              2.0.9     Mar 29, 2009   Python psycopg2 database adapter for Zope 
    │ 📂 vault-psycopg2                          0.1.11    Jun 26, 2019   Helper classes to integrate psycopg2 with 
    │                                           │         │              │ Vault                                          │ 
    │ 📂 aws-psycopg2                            1.2.1     Feb 12, 2020   A aws psycopg2 package from psycopg2. 
    │ 📂 psycopg2-dateutils                      0.1       Aug 16, 2011   Use dateutils.relativedelta to represent 
    │                                           │         │              │ PostgreSQL interval types                      │
    │ 📂 psycopg2-connect                        0.0.2     Jul 14, 2021   A lightweight package for connecting to        
    │                                           │         │              │ postgres servers.                              │
    │ 📂 types-psycopg2                          2.9.1     Oct 12, 2021   Typing stubs for psycopg2 
    │ 📂 db-psycopg2                             0.0.7     Aug 6, 2013    psycopg2 driver for db 
    │ 📂 psycopg2-mq                             0.5.7     Mar 8, 2021    A message queue written around PostgreSQL. 
    │ 📂 psycopg2cffi                            2.9.0     Jan 27, 2021   .. image:: 
    │                                           │         │              │ https://travis-ci.org/chtd/psycopg2cffi.svg?b… │
    │ 📂 psycopg2-connection                     0.1.1.1   Mar 6, 2021    Abstract away common patterns you might use in 
    │                                           │         │              │ psycopg2.                                      │
    │ 📂 Psycopg2Database                        0.2.0     Sep 2, 2010    psycopg2 driver for the DatabasePipe package 
    │ 📂 psycopg-c                               3.0.1     Oct 14, 2021   PostgreSQL database adapter for Python -- C 
    │                                           │         │              │ optimisation distribution                      │
    │ 📂 psycopg2-wrapper                        1.0       Jan 7, 2020    Simple wrapper for executing commands and 
    │                                           │         │              │ queries                                        │
    │ 📂 psycopg2transaction                     0.1.1     Mar 18, 2017   psycopg2 integration with transaction           
    │ 📂 psycopg2-iam                            1.1.0     Aug 13, 2021   Custom Connection Factory class (RDS, 
    │                                           │         │              │ Redshift) with build-in IAM authentication and │
    │                                           │         │              │ SSL bundle downloader support.                 │
    │ 📂 Flask-Psycopg2                          1.3       Mar 16, 2014   postgresql adapter for Flask   
    │ 📂 psycopg2ct                              2.4.4     Feb 11, 2012   An implementation of the psycopg2 module using 
    │                                           │         │              │ ctypes.                                        │
    │ 📂 psycopg2-binary                         2.9.1     Jun 17, 2021   psycopg2 - Python-PostgreSQL Database Adapter 
    │ 📂 psycopg-postgis                         0.4.0     Jun 23, 2017   Deprecated. Use postgis package instead. 
    │ 📂 cherrypy-psycopg2-crud                  1.3       Mar 20, 2017   CherryPy page handler base-class to export 
    │                                           │         │              │ psycopg2 database tables in a RESTful way      │
    │ 📂 psycopg2-managed-connection             1.0.0     Jul 2, 2015    Thread-safe connection manager for psycopg2 
    │                                           │         │              │ connections.                                   │
    │ 📂 hou-flask-psycopg2                      0.0.7     May 3, 2020    Highly Opinionated Utils: Flask Psycopg2 
    │ 📂 g5-lambda-psycopg2                      0.1.7     Oct 1, 2019    Fork of awslambda-psycopg2 
    │ 📂 jquery-querybuilder-psycopg2            1.4.1     Jul 15, 2019   Parse a jQuery QueryBuilder style rule set 
    │                                           │         │              │ into a psycopg2.sql.Composable                 │
    │ 📂 opentelemetry-instrumentation-psycopg2  0.25b0    Oct 13, 2021    OpenTelemetry psycopg2 instrumentation 
    │ 📂 django-psycopg2-extension               0.0.5     Oct 20, 2020   Library contains django commands which helps     
    │                                           │         │              │ to prepare and manage PostgreSQL database.     │
    └───────────────────────────────────────────┴─────────┴──────────────┴────────────────────────────────────────────────┘
    
    (env) C:\programming\python\django\myproject>
  3. Install it by executing the following command :

    (env) C:\programming\python\django\myproject>pip install psycopg2
    Collecting psycopg2
    Downloading psycopg2-2.9.1-cp39-cp39-win_amd64.whl (1.2 MB)
    |████████████████████████████████| 1.2 MB 930 kB/s
    Installing collected packages: psycopg2
    Successfully installed psycopg2-2.9.1
    
    (env) C:\programming\python\django\myproject>
    
  4. Start to execute the Django-based internal service once more. If there are no more error appear, it will run the Django-based internal service properly.

Leave a Reply