Resolve: "No module named psycopg2" in Python

Jan 01, 2023 · 1 min read · Post a comment

Even though the usual ModuleNotFoundError are much obvious, sometimes there is an extra package or in this case a different package to be installed in order to get the required module. The error this time being:

ModuleNotFoundError: No module named 'psycopg2'

Package and module are used interchangeably.


  • Python 3
  • pip


Step 1. cd to your Python project virtual environment directory. For instance:

Related: Create, manage and remove virtual environments in Python.

Step 2. Install the psycopg2 module.

pip install psycopg2-binary --user


  • If the above command doesn’t work try replacing pip with pip3.

This way we are running the module’s binary version. However, one could argue that using a binary packages in production is discouraged.

If you still want to install the psycopg2 package the old-fashioned way (pip install psycopg2), first you need to install few “build”, dependency packages. For instance:

apt-get install libpq-dev -y

Not necessarily, but there might be other one or more dependency packages that “just” works based on your working environment, of course, including: build-dep, python-psycopg2, python3-psycopg2, python-dev, python3-dev, python-setuptools, python-dev-is-python3, python-devel, postgresql-devel, postgresql-dev, django-psycopg2-extension.


Related posts:

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.