How to Solve Error Message ModuleNotFoundError: No module named ‘psycopg2’

Posted on

Introduction

This is an article with a specific topic about how to solve error message as it exists in the title of the article. It occurs when a django application runs. It happens after adding the following content in the settings.py script inside the project folder :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'xxxx',
        'USER': 'postgres',
        'PASSWORD' : '',
        'HOST' : 'xxx.xxx.xxx.xxx',
        'PORT' : 'xxxx',
    }
}

The above line configurations is a specific script for connecting the django application to PostgreSQL database server. Suddenly, after saving the file, the runserver application execution is generating error. The error message is ModuleNotFoundError: No module named ‘psycopg2’. The following is the complete output error message :

user@hostname:~/python/my-django/users$ python3 manage.py makemigrations
Traceback (most recent call last):e
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 20, in 
    import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "manage.py", line 21, in 
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 994, in _gcd_import
  File "", line 971, in _find_and_load
  File "", line 955, in _find_and_load_unlocked
  File "", line 665, in _load_unlocked
  File "", line 678, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in 
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in 
    class AbstractBaseUser(models.Model):
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/db/models/base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/db/models/base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/db/models/options.py", line 204, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "v/python/django-env/lib/python3.6/site-packages/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/db/utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/user/python/django-env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 24, in 
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

Solving the Problem

To solve the above problem, there are several solutions and those solutions have a connection with one another. But the most important thing is the focus of the solution is in the existance of the ‘psycopg2’. How can the module with the name of ‘psycopg2’ can be available for further usage in the django application. There are several steps or solution to solve the problem. The following is the list of the alternative steps or solution :

1. Installing the psycopg2 package.

2. Install the psycopg2 package to the virtual environment.

In the following section, there will be a further detail explanation about the above steps for solving the problem.

 

Installing psycopg2 package to the operating system using apt tool

Since it is the Linux Ubuntu distribution, execute the following command to install the ‘psycopg2’ module :

(django-env) (base) user@hostname:~/python/my-django/users$ sudo apt-get -y install python3-psycopg2
[sudo] password for user: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  python-psycopg2-doc
The following NEW packages will be installed:
  python3-psycopg2
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 152 kB of archives.
After this operation, 838 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-psycopg2 amd64 2.7.4-1 [152 kB]
Fetched 152 kB in 2s (85.6 kB/s)                    
Selecting previously unselected package python3-psycopg2.
(Reading database ... 328999 files and directories currently installed.)
Preparing to unpack .../python3-psycopg2_2.7.4-1_amd64.deb ...
Unpacking python3-psycopg2 (2.7.4-1) ...
Setting up python3-psycopg2 (2.7.4-1) ...
(django-env) (base) user@hostname:~/python/my-django/users$ 

The above command execution is relying on the apt tool program. Just make sure to choose the correct package name since the chosen package for the installation above is using python3. So, the package name for installation is ‘python3-psycopg. Make sure to search the compatible package using ‘apt search’ command.

Apparently, in this case, after executing the above command for installing psycopg2 to the operating system, the error message still occurs. So, if the error message is still occurs, just do the following step as another action to complete the solution.

 

Installing psycopg2 module in the python virtual environment using pip

The following is the command for executing psycopg2 to the running python virtual environment. The python virtual environment is ‘django-env’. The command for installing psycopg2 module is ‘pip install psycopg2’.

(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$

Installation of psycopg2-binary module in the python virtual environment using pip

Apparently, the solution in the previous step failed. There is an information about how to proceed the installation. It gives a solution to install a binary package with the name of ‘psycopg2-binary’. The following is the command execution :

(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$

In the output above, the python virtual environment is ‘django-env’. According to the output message of the running django web server, after the execution of the above step ends in a success, the error message disappear and the problem solved.

Leave a Reply