0

I'm trying to pull data into a pandas dataframe from a pgadmin database using psycopg2. My .py file runs normally in terminal, but when I run it in python interactive (vs code) or jupyter notebooks I get the following error.

Code:

with psycopg2.connect(conn_string) as conn:
    sql = "SELECT * FROM table"
    df = pd.read_sql_query(sql, conn)

Error:

OperationalError                          
---> 19 with psycopg2.connect(conn_string) as conn:
     20     sql = "SELECT * FROM table"
     21     df = pd.read_sql_query(sql, conn)

//anaconda3/lib/python3.7/site-packages/psycopg2/__init__.py in connect(dsn, connection_factory, cursor_factory, **kwargs)
    124 
    125     dsn = _ext.make_dsn(dsn, **kwargs)
--> 126     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    127     if cursor_factory is not None:
    128         conn.cursor_factory = cursor_factory

OperationalError: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.

Does anyone know why this could be the case? When I open python interactive in vs code, I see this error as well:

Jupyter kernel cannot be started from 'Python 3.7.4 64-bit'. 
Using closest match Python 3.7.3 64-bit ('anaconda3': conda) instead.
Error starting original kernel: Error: Module 'notebook' not installed.
Ethan
  • 534
  • 5
  • 21

1 Answers1

0

I'm a developer on this extension and I can address what I believe is the issue here. Currently in VSCode we start the local Jupyter server using the Python interpreter that you have selected in the bottom left corner of VSCode. It looks to me like you have a Python 3.7.4 interpreter selected and that interpreter doesn't have Juypter notebooks installed in it. So our code then falls back to find another interpreter / environment that does have jupyter (3.7.3 in this case). If 3.7.3 Anaconda doesn't have the full environment that you need to run then that might be why you are seeing errors here. If that 3.7.4 environment is the one that you want to run the interactive window in make sure that you can run jupyter notebooks from the command line in that environment first.

Ian Huff
  • 2,699
  • 1
  • 17
  • 17
  • I'm able to run jupyter notebooks from the command line, how can I make sure that jupyter notebooks is installed in python 3.7.4? – Ethan Oct 16 '19 at 14:37
  • So when you run from the command line you must be picking up a specific python version somehow. From your results above you must have both 3.7.4 and 3.7.3 installed on your system. But from my end I can't really tell what you are using for those environments. Most folks use something like virtualenvs or conda envs to manage this. You'll just need to get a command prompt with that 3.7.4 and install jupyter into that. – Ian Huff Oct 17 '19 at 15:50