ModuleNotFoundError: No module named 'psycopg2' in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
11 min

banner

# ModuleNotFoundError: No module named 'psycopg2' in Python

The Python "ModuleNotFoundError: No module named 'psycopg2'" occurs when we forget to install the psycopg2-binary module before importing it or install it in an incorrect environment.

To solve the error, install the module by running the pip install psycopg2-binary command.

no module named psycopg2

Open your terminal in your project's root directory and install the psycopg2-binary module.

shell
# ๐Ÿ‘‡๏ธ In a virtual environment or using Python 2 pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 (could also be pip3.10 depending on your version) pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install psycopg2-binary pip install psycopg2-binary --user # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 (could also be pip3.10 depending on your version) python3 -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Using py alias (Windows) py -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Anaconda conda install -c conda-forge psycopg2-binary # ๐Ÿ‘‡๏ธ Alternative for macOS brew install postgresql # ๐Ÿ‘‡๏ธ alternative for Debian (Ubuntu) sudo apt install build-essential libpq-dev python3-dev pip3 install psycopg2 # ๐Ÿ‘‡๏ธ Alternative for Red Hat / CentOS / Fedora sudo yum install python3-devel postgresql-devel pip3 install psycopg2 # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install psycopg2-binary

The psycopg2-binary module is a stand-alone package that doesn't require a compiler or external libraries.

After you install the psycopg2-binary package, try importing it as follows.

main.py
import psycopg2 # Connect to your Postgres DB conn = psycopg2.connect("dbname=test user=postgres") # Open a cursor to perform database operations cur = conn.cursor() # Execute a query cur.execute("SELECT * FROM my_data") # Retrieve query results records = cur.fetchall()

# Common causes of the error

The error occurs for multiple reasons:

  1. Not having the psycopg2-binary package installed by running pip install psycopg2-binary.
  2. Installing the package in a different Python version than the one you're using.
  3. Installing the package globally and not in your virtual environment.
  4. Your IDE running an incorrect version of Python.
  5. Naming your module psycopg2.py which would shadow the official module.
  6. Declaring a variable named psycopg2 which would shadow the imported variable.

If the error persists, get your Python version and make sure you are installing the package using the correct Python version.

shell
python --version

get python version

For example, my Python version is 3.10.4, so I would install the psycopg2-binary package with pip3.10 install psycopg2-binary.

shell
pip3.10 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you get a permissions error use pip3 (NOT pip3.X) sudo pip3 install psycopg2-binary

Notice that the version number corresponds to the version of pip I'm using.

If the PATH for pip is not set up on your machine, replace pip with python3 -m pip:

shell
# ๐Ÿ‘‡๏ธ Make sure to use your version of Python, e.g. 3.10 python3 -m pip install psycopg2-binary

If the error persists, try restarting your IDE and development server/script.

# Check if the package is installed

You can check if you have the psycopg2-binary package installed by running the pip show psycopg2-binary command.

shell
# ๐Ÿ‘‡๏ธ Check if you have psycopg2-binary installed pip show psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip set up in PATH python -m pip show psycopg2-binary

The pip show psycopg2-binary command will either state that the package is not installed or show a bunch of information about the package, including the location where the package is installed.

# Make sure your IDE is using the correct Python version

If the package is not installed, make sure your IDE is using the correct version of Python.

If you have multiple Python versions installed on your machine, you might have installed the psycopg2-binary package using the incorrect version or your IDE might be set up to use a different version.

For example, In VSCode, you can press CTRL + Shift + P or (โŒ˜ + Shift + P on Mac) to open the command palette.

Then type "Python select interpreter" in the field.

python select interpreter

Then Select the correct Python version from the dropdown menu.

select correct python version

Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.

# Install the package in a Virtual Environment

If you are using a virtual environment, make sure you are installing psycopg2-binary in your virtual environment and not globally.

You can try creating a virtual environment if you don't already have one.

shell
# ๐Ÿ‘‡๏ธ Use the correct version of Python when creating VENV python3 -m venv venv # ๐Ÿ‘‡๏ธ Activate on Unix or MacOS source venv/bin/activate # ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ Install psycopg2-binary in your virtual environment pip install psycopg2-binary

If the python3 -m venv venv command doesn't work, try the following 2 commands:

  • python -m venv venv
  • py -m venv venv

Your virtual environment will use the version of Python that was used to create it.

If the error persists, make sure you haven't named a module in your project as psycopg2.py because that would shadow the original psycopg2-binary module.

You also shouldn't be declaring a variable named psycopg2 as that would also shadow the original module.

# Try reinstalling the package

If the error is not resolved, try to uninstall the psycopg2-binary package and then install it.

shell
# ๐Ÿ‘‡๏ธ Check if you have psycopg2-binary installed pip show psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip set up in PATH python -m pip show psycopg2-binary # ๐Ÿ‘‡๏ธ Uninstall psycopg2-binary pip uninstall psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip set up in PATH python -m pip uninstall psycopg2-binary # ๐Ÿ‘‡๏ธ Install psycopg2-binary pip install psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip set up in PATH python -m pip install psycopg2-binary

Try restarting your IDE and development server/script.

You can also try to upgrade the version of the psycopg2-binary package.

shell
pip install psycopg2-binary --upgrade # ๐Ÿ‘‡๏ธ If you don't have pip set up in PATH python -m pip install psycopg2-binary --upgrade
If the error persists, follow the operating system-specific instructions on how to install psycopg2-binary.

# Table of Contents

  1. Install psycopg2 on Windows
  2. Install psycopg2 on macOS or Linux
  3. Install psycopg2 in Visual Studio Code
  4. Install psycopg2 in PyCharm
  5. Install psycopg2 in Anaconda
  6. Install psycopg2 in Jupyter Notebook

# Install psycopg2 on Windows

To install the psycopg2 module on Windows:

  1. Type CMD in the search bar and open the Command Prompt application.
  2. Type pip install psycopg2-binary and press Enter.
cmd
pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 python3 -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Using py alias py -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ If you get a permissions error pip install psycopg2-binary --user # ๐Ÿ‘‡๏ธ For Anaconda conda install -c conda-forge psycopg2-binary

pip install psycopg2-binary windows

The psycopg2-binary module is a stand-alone package that doesn't require a compiler or external libraries.

If the installation command doesn't succeed, try running CMD as an administrator.

Right-click on the search result, click on "Run as administrator" and run the pip install command.

run cmd as administrator

If you get the error 'pip' is not recognized as an internal or external command, use the python -m command when installing psycopg2-binary.

shell
python -m pip install psycopg2-binary python3 -m pip install psycopg2-binary py -m pip install psycopg2-binary

Alternatively, you can install the psycopg2-binary module in a virtual environment:

  1. Open the root directory of your project.
  2. Press Shift and right-click in Explorer.

windows open powershell window here

  1. Click on "Open PowerShell window here".
  2. Run the following commands.
PowerShell
# ๐Ÿ‘‡๏ธ Might also be: "python3 -m venv venv" python -m venv venv # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Install psycopg2-binary in your virtual environment pip install psycopg2-binary

If the python -m venv venv command doesn't work, try the following 2 commands:

  • python3 -m venv venv
  • py -m venv venv.

If you see an error message that ps1 cannot be loaded because running scripts is disabled on this system, run the following command, type "yes" when prompted and rerun the activation command.

PowerShell
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
You can verify that the psycopg2-binary module is installed by using the pip show psycopg2-binary command.
PowerShell
pip show psycopg2-binary pip3 show psycopg2-binary python -m pip show psycopg2-binary python3 -m pip show psycopg2-binary

The pip show psycopg2-binary command will either state that the package is not installed or show a bunch of information about the package, including the location where the package is installed.

# Install psycopg2-binary on macOS or Linux

To install psycopg2-binary on macOS or Linux:

  1. Search for "terminal" and start the application.
  2. Type pip install psycopg2-binary and press Enter.

search for terminal

terminal
pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 python3 -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Alternative if you get a permissions error pip install psycopg2-binary --user # ๐Ÿ‘‡๏ธ Alternative for macOS brew install postgresql # ๐Ÿ‘‡๏ธ Alternative for Debian (Ubuntu) sudo apt install build-essential libpq-dev python3-dev pip3 install psycopg2 # ๐Ÿ‘‡๏ธ Alternative for Red Hat / CentOS / Fedora sudo yum install python3-devel postgresql-devel pip3 install psycopg2 # ๐Ÿ‘‡๏ธ For Anaconda conda install -c conda-forge psycopg2-binary

macos linux install psycopg2

The psycopg2-binary module is a stand-alone package that doesn't require a compiler or external libraries.

If you get an error that pip isn't found, use the python -m command.

terminal
python -m pip install psycopg2-binary python3 -m pip install psycopg2-binary

If you get a permissions error, prefix the command with sudo.

terminal
sudo pip install psycopg2-binary sudo pip3 install psycopg2-binary

Alternatively, you can install the psycopg2-binary package in a virtual environment:

  1. Open your terminal in the root directory of your project.
  2. Run the following commands.
shell
# ๐Ÿ‘‡๏ธ Could also be "python -m venv venv" python3 -m venv venv # ๐Ÿ‘‡๏ธ Activate virtual env on macOS or Linux source venv/bin/activate # ๐Ÿ‘‡๏ธ Install psycopg2-binary in your virtual environment pip install psycopg2-binary

Your virtual environment will use the version of Python that was used to create it.

If the python3 -m venv venv command doesn't work, use python -m venv venv instead.

You can use the pip show command to verify psycopg2-binary has been installed successfully.

shell
pip show psycopg2-binary pip3 show psycopg2-binary python -m pip show psycopg2-binary python3 -m pip show psycopg2-binary

The pip show psycopg2-binary command will either state that the package is not installed or show a bunch of information about the package.

# Install psycopg2 in Visual Studio Code

To install psycopg2 in Visual Studio Code:

  1. Press CTRL + ` (Backtick) on your keyboard to open the terminal.
  2. Run the pip install psycopg2-binary command to install the psycopg2-binary module.
terminal
pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 python3 -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Using py alias py -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Alternative if you get a permissions error pip install psycopg2-binary --user

vscode pip install psycopg2

You can also open the terminal in Visual Studio Code by pressing CTRL+Shift+P and then type "View: Toggle Terminal".

The psycopg2-binary module is a stand-alone package that doesn't require a compiler or external libraries.

When installing Python modules in Visual Studio code, make sure that your IDE is configured to use the correct Python version.

Press CTRL+Shift+P or (โŒ˜ + Shift + P on Mac) to open the command palette.

Then type "Python select interpreter" in the field.

python select interpreter

Then Select the correct Python version from the dropdown menu.

select correct python version

Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.

You can use the python --version command if you need to get your version of Python.

terminal
python --version python3 --version

get python version

You can also try creating a virtual environment if you don't already have one.

terminal
# ๐Ÿ‘‡๏ธ Could also be "python -m venv venv" or "py -m venv venv" python3 -m venv venv # ๐Ÿ‘‡๏ธ Activate on Unix or MacOS source venv/bin/activate # ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ Install psycopg2-binary in your virtual environment pip install psycopg2-binary

Your virtual environment will use the version of Python that was used to create it.

# Install psycopg2 in PyCharm

To install psycopg2 in PyCharm:

  1. Press Alt+F12 on your keyboard to open the terminal.
  2. Run the pip install psycopg2-binary command to install the psycopg2-binary module.
terminal
pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 python3 -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Using py alias py -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Alternative if you get a permissions error pip install psycopg2-binary --user

pycharm pip install psycopg2

The psycopg2-binary module is a stand-alone package that doesn't require a compiler or external libraries.

Alternatively, you can use the IDE itself to install the module.

  1. Click on "File" > "Settings" > "Project" > "Python Interpreter".
  2. Click on the + icon and type psycopg2-binary.
  3. Click on "Install Package".

pycharm interpreter install psycopg2

When installing Python modules in PyCharm, make sure that your IDE is configured to use the correct version of Python.

Click on "File" > "Settings" > "Project" > "Python Interpreter".

pycharm select correct interpreter

Then Select the correct Python version from the dropdown menu.

Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.

You can use the python --version command if you need to get your version of Python.

terminal
python --version python3 --version

get python version

# Install psycopg2 in Anaconda

You can install the psycopg2-binary package with a command.

If you are on Windows, search for "Anaconda Prompt" and open the application.

If you are on macOS or Linux, open your terminal.

Run the following command to install the psycopg2-binary package.

shell
# ๐Ÿ‘‡๏ธ Using conda conda install -c conda-forge psycopg2-binary # ๐Ÿ‘‡๏ธ Alternatively use `pip` pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 python3 -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Using py alias py -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Alternative if you get a permissions error pip install psycopg2-binary --user

The psycopg2-binary module is a stand-alone package that doesn't require a compiler or external libraries.

Click on the following article if you need to install a specific version of the package using Anaconda.

# Install psycopg2 in Jupyter Notebook

To install psycopg2 in Jupyter Notebook:

  1. Open your terminal and type "jupyter notebook".

open jupyter notebook

  1. Click on "New" and then "Terminal" in the browser tab.

jupyter notebook click new terminal

  1. Type pip install psycopg2-binary and press Enter.
shell
# ๐Ÿ‘‡๏ธ Using pip pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install psycopg2-binary # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ For Python 3 python3 -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Using py alias py -m pip install psycopg2-binary # ๐Ÿ‘‡๏ธ Using conda conda install -c conda-forge psycopg2-binary # ๐Ÿ‘‡๏ธ Alternative if you get a permissions error pip install psycopg2-binary --user

The psycopg2-binary module is a stand-alone package that doesn't require a compiler or external libraries.

Alternatively, you can use the Python ipykernel.

  1. Open your terminal and type "jupyter notebook".

open jupyter notebook

  1. Click on "New" and then click on "Python 3 (ipykernel)". jupyter notebook click new ipykernel

  2. Type !pip install psycopg2-binary and click on "Run".

jupyter notebook install module

Note that the pip install command must be prefixed with an exclamation mark if you use this approach.

shell
!pip install psycopg2-binary

Once you type the command, click "Run" to install the psycopg2-binary module.

If you get a permissions error, e.g. "[WinError: 5] Access is denied", add the --user option to the installation command.

shell
!pip install psycopg2-binary --user

jupyter notebook install with user option

If the error persists, try to restart the Jupyter Kernel and rerun the command.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev