AttributeError when using wrds library

Question:

Doing the following:

import wrds
db = wrds.Connection()

does throw this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~anaconda3envsplaygroundlibsite-packagessqlalchemyenginebase.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 ~anaconda3envsplaygroundlibsite-packageswrdssql.py:101, in Connection.__init__(self, autoconnect, **kwargs)
     99 if (autoconnect):
    100     self.connect()
--> 101     self.load_library_list()

File ~anaconda3envsplaygroundlibsite-packageswrdssql.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 ~anaconda3envsplaygroundlibsite-packagessqlalchemyenginebase.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 tablesn    SELECT oid, relnamespace, relkindn    FROM pg_classn    WHERE relkind = ANY (ARRAY['r'::"char", 'v'::"char", 'f'::"char", 'p'::"char"])n),nschemas AS (n    -- schemas we have usage on that represent productsn    SELECT nspname AS schemaname, pg_namespace.oid, array_agg(DISTINCT relkind) AS relkind_an    FROM pg_namespacen    JOIN pgobjs ON pg_namespace.oid = relnamespacen    WHERE nspname !~ '(^pg_)|(_old$)|(_new$)|(information_schema)'n        AND has_schema_privilege(nspname, 'USAGE') = TRUEn    GROUP BY nspname, pg_namespace.oidn)nSELECT schemanamenFROM schemasnWHERE relkind_a != ARRAY['v'::"char"] -- any schema except only viewsnUNIONn-- schemas w/ views (aka "friendly names") that reference accessable product tablesnSELECT nv.schemanamenFROM schemas nvnJOIN 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::oidn    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.refobjidn    AND dt.classid = 'pg_rewrite'::regclass::oid AND dt.refclassid = 'pg_class'::regclass::oidnJOIN pgobjs t ON dt.refobjid = t.oidn    AND (t.relkind = ANY (ARRAY['r'::"char", 'v'::"char", 'f'::"char", 'p'::"char"]))nJOIN schemas nt ON t.relnamespace = nt.oidnGROUP BY nv.schemanamenORDER 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.

Asked By: shenflow

||

Answers:

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.

Answered By: Whale
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.