1

Python beginner here. Don't understand why intellisense for some psycopg2 objects doesn't work in PyCharm.

import psycopg2
from config import db_config
from datetime import datetime

conn = None
cursor = None

try:
    params = db_config
    conn = psycopg2.connect(**params)
    cursor = conn.cursor()


except (Exception, psycopg2.DatabaseError) as error:
    print(error)

finally:
    if conn:
        cursor.close()
        conn.close()
        print("connection closed")

cursor() method not offered:

enter image description here

cannot step into the definition here:

enter image description here

Do I have to somehow specify the types of the objects during declaration e.g.: cursor: cursor = None

mishap
  • 8,176
  • 14
  • 61
  • 92

1 Answers1

1

This is what seems to be fixing the issue, but I'm not sure if this is the right way to go about this issue every time one needs intellisense from imported packages:

from psycopg2.extensions import cursor, connection
import psycopg2
from config import db_config
from datetime import datetime

conn: connection = None
cursor: cursor = None
mishap
  • 8,176
  • 14
  • 61
  • 92
  • I took a look at this, `psycopg2.extensions` package holds class definitions that the IDE might not be able to introspect otherwise. The reason being that `psycopg2` is in part implemented in C there are no Python class definitions in the bare `psycop2` package. This is usually not the case, most packages choose to bundle the definitions by default or are implemented in native Python. Whenever such isn't the case, I think your solution is in fact the correct approach to solving this type of problem. – bad_coder Apr 12 '21 at 07:30
  • Perhaps the correct solution for this is simply including `psycopg2.extensions` in the `PYTHONPATH`. Maybe as in [this thread](https://stackoverflow.com/questions/28326362). – bad_coder Apr 12 '21 at 08:07