Search code examples
pythonwrds-compusat

AttributeError when using wrds library


Doing the following:

import wrds
db = wrds.Connection()

does throw this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~\anaconda3\envs\playground\lib\site-packages\sqlalchemy\engine\base.py:1410, in Connection.execute(self, statement, parameters, execution_options)
   1409 try:
-> 1410     meth = statement._execute_on_connection
   1411 except AttributeError as err:

AttributeError: 'str' object has no attribute '_execute_on_connection'

The above exception was the direct cause of the following exception:

ObjectNotExecutableError                  Traceback (most recent call last)
Cell In [2], line 2
      1 #connect to wrds api
----> 2 db = wrds.Connection()

File ~\anaconda3\envs\playground\lib\site-packages\wrds\sql.py:101, in Connection.__init__(self, autoconnect, **kwargs)
     99 if (autoconnect):
    100     self.connect()
--> 101     self.load_library_list()

File ~\anaconda3\envs\playground\lib\site-packages\wrds\sql.py:197, in Connection.load_library_list(self)
    162         print("Loading library list...")
    163         query = """
    164 WITH pgobjs AS (
    165     -- objects we care about - tables, views, foreign tables, partitioned tables
   (...)
    195 ORDER BY 1;
    196         """
--> 197         cursor = self.connection.execute(query)
    198         self.schema_perm = [x[0] for x in cursor.fetchall()]
    199         print("Done")

File ~\anaconda3\envs\playground\lib\site-packages\sqlalchemy\engine\base.py:1412, in Connection.execute(self, statement, parameters, execution_options)
   1410     meth = statement._execute_on_connection
   1411 except AttributeError as err:
-> 1412     raise exc.ObjectNotExecutableError(statement) from err
   1413 else:
   1414     return meth(
   1415         self,
   1416         distilled_parameters,
   1417         execution_options or NO_OPTIONS,
   1418     )

ObjectNotExecutableError: Not an executable object: '\nWITH pgobjs AS (\n    -- objects we care about - tables, views, foreign tables, partitioned tables\n    SELECT oid, relnamespace, relkind\n    FROM pg_class\n    WHERE relkind = ANY (ARRAY[\'r\'::"char", \'v\'::"char", \'f\'::"char", \'p\'::"char"])\n),\nschemas AS (\n    -- schemas we have usage on that represent products\n    SELECT nspname AS schemaname, pg_namespace.oid, array_agg(DISTINCT relkind) AS relkind_a\n    FROM pg_namespace\n    JOIN pgobjs ON pg_namespace.oid = relnamespace\n    WHERE nspname !~ \'(^pg_)|(_old$)|(_new$)|(information_schema)\'\n        AND has_schema_privilege(nspname, \'USAGE\') = TRUE\n    GROUP BY nspname, pg_namespace.oid\n)\nSELECT schemaname\nFROM schemas\nWHERE relkind_a != ARRAY[\'v\'::"char"] -- any schema except only views\nUNION\n-- schemas w/ views (aka "friendly names") that reference accessable product tables\nSELECT nv.schemaname\nFROM schemas nv\nJOIN pgobjs v ON nv.oid = v.relnamespace AND v.relkind = \'v\'::"char"\nJOIN pg_depend dv ON v.oid = dv.refobjid AND dv.refclassid = \'pg_class\'::regclass::oid\n    AND dv.classid = \'pg_rewrite\'::regclass::oid AND dv.deptype = \'i\'::"char"\nJOIN pg_depend dt ON dv.objid = dt.objid AND dv.refobjid <> dt.refobjid\n    AND dt.classid = \'pg_rewrite\'::regclass::oid AND dt.refclassid = \'pg_class\'::regclass::oid\nJOIN pgobjs t ON dt.refobjid = t.oid\n    AND (t.relkind = ANY (ARRAY[\'r\'::"char", \'v\'::"char", \'f\'::"char", \'p\'::"char"]))\nJOIN schemas nt ON t.relnamespace = nt.oid\nGROUP BY nv.schemaname\nORDER BY 1;\n  

You need a WRDS account in order to connect to the WRDS API. I have one and I inserted the correct information. Nonetheless I get this error. In fact, in the Jupyter Notebook I am using, it explicitly states "Loading library list..." before the error pops up. Why is that so? I have used the library for ages and this has never occurred to me.

EDIT: I am using version 3.1.2 (the latest version) of the WRDS package. This error does not occur when using version 3.1.1.


Solution

  • The default version of SQLAlchemy is 2.0.0. You need to uninstall and install the 1.4 version: pip install SQLAlchemy==1.4.46. It works for me.