Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL- ModuleNotFoundError: No module named 'psycopg2'

I can confirm psycopg2 is install (using conda install -c anaconda psycopg2) but the it seems psycopg2 cannot be imported to my python script or the interpreter is unable to locate it. I also tried installing using pip3, requirements are satisfied, meaning psycopg2 is already istalled, but cannot understand why I script isn't able to import it. Using Mac (OS v10.14.4)

$ python create_tables.py
Traceback (most recent call last):
  File "create_tables.py", line 1, in <module>
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

$ pip3 install psycopg2
Requirement already satisfied: psycopg2 in /usr/local/lib/python3.7/site-packages (2.8.2)
$ pip3 install psycopg2-binary
Requirement already satisfied: psycopg2-binary in /usr/local/lib/python3.7/site-packages (2.8.2)
python -V
Python 3.7.0

Any idea why this happen?

EDIT: create_table.py

import psycopg2
from config import config


def create_tables():
    """ create tables in the PostgreSQL database"""
    commands = (
        """
        CREATE TABLE vendors (
            vendor_id SERIAL PRIMARY KEY,
            vendor_name VARCHAR(255) NOT NULL
        )
        """,
        """ CREATE TABLE parts (
                part_id SERIAL PRIMARY KEY,
                part_name VARCHAR(255) NOT NULL
                )
        """,
        """
        CREATE TABLE part_drawings (
                part_id INTEGER PRIMARY KEY,
                file_extension VARCHAR(5) NOT NULL,
                drawing_data BYTEA NOT NULL,
                FOREIGN KEY (part_id)
                REFERENCES parts (part_id)
                ON UPDATE CASCADE ON DELETE CASCADE
        )
        """,
        """
        CREATE TABLE vendor_parts (
                vendor_id INTEGER NOT NULL,
                part_id INTEGER NOT NULL,
                PRIMARY KEY (vendor_id , part_id),
                FOREIGN KEY (vendor_id)
                    REFERENCES vendors (vendor_id)
                    ON UPDATE CASCADE ON DELETE CASCADE,
                FOREIGN KEY (part_id)
                    REFERENCES parts (part_id)
                    ON UPDATE CASCADE ON DELETE CASCADE
        )
        """)
    conn = None
    try:
        # read the connection parameters
        params = config()
        # connect to the PostgreSQL server
        conn = psycopg2.connect(**params)
        cur = conn.cursor()
        # create table one by one
        for command in commands:
            cur.execute(command)
        # close communication with the PostgreSQL database server
        cur.close()
        # commit the changes
        conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()


if __name__ == '__main__':
    create_tables()
like image 518
arilwan Avatar asked Apr 19 '19 00:04

arilwan


People also ask

How do you fix ModuleNotFoundError No module named psycopg2?

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.

How do I know if psycopg2 is installed on Windows?

Check psycopg2-binary Version Windows To check which version of psycopg2-binary is installed, use pip show psycopg2-binary or pip3 show psycopg2-binary in your Windows CMD, command line, or PowerShell.

How do I install psycopg2 on Windows 10?

To install this module follow the below steps. If python is not installed in your system, then you can install it running the given command in your command prompt. Step 2: Open the command prompt and run the below command to install psycopg2-binary. If it shows successfully installed then you are good to go.

How to fix the Python modulenotfounderror “no module named “psycopg2”?

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.

Why won’t ‘psycopg2’ work on incorrect virtual environments?

Working On Incorrect Virtual Enviornment? Many “No module named psycopg2” errors occur due to working on incorrect virtual environments and installing the ‘psycopg2’ on a different environment. Suppose you have two versions of python3 installed, and how will you install ‘psycopg2’ to a specific python?

What is modulenotfounderror in Django?

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’.

Why can't I import psycopg2 in Python?

run python and try to import if you insist on installing it on your systems python try: If psycopg2 is getting installed but you are unable to import it in your .py file then the problem is libpq, its linkages, and the library openssl, on which libpq depends upon.


1 Answers

Yes, found a solution,

python -m pip install psycopg2-binary 

does the trick!

like image 155
arilwan Avatar answered Oct 09 '22 20:10

arilwan