Skip to content

BUG: Raise ImportError when provided URI, but sqlalchemy is not installed. #12022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

anirudhjoshi
Copy link

Rather than presume that it is an sqlite database and provide a confusing error.

Running (without sqlalchemy installed):

import pandas

db_uri = "postgresql://user:pass@localhost/db"
sql_query = "SELECT * FROM test"

pandas.read_sql(sql_query, db_uri)

Produces:

pandas/io/sql.pyc in execute(self, *args, **kwargs)
   1532             cur = self.con
   1533         else:
-> 1534             cur = self.con.cursor()
   1535         try:
   1536             if kwargs:

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

Which links to:

https://github.com/pydata/pandas/blob/master/pandas/io/sql.py#L1534

Zooming into the problem:

  1. https://github.com/pydata/pandas/blob/master/pandas/io/sql.py#L631-L637
  2. https://github.com/pydata/pandas/blob/master/pandas/io/sql.py#L612-L619
  3. https://github.com/pydata/pandas/blob/master/pandas/io/sql.py#L64-L68

So it looks like the URI is passed through and simply assumed to be a sqlite/mysql database object (using the old method), even if it isn't.

Raising an import error when a URI string is passed back by _engine_builder should solve the issue.

…lled.

Rather than presume that it is an sqlite database and provide a confusing error.
@jorisvandenbossche
Copy link
Member

@anirudhjoshi Thanks a lot for your contribution!
However, there is already a recent PR that tries to solve the same issue: #11920. That is not merged yet, but I will let the author of that PR the chance first to update it.

@jorisvandenbossche jorisvandenbossche added Duplicate Report Duplicate issue or pull request IO SQL to_sql, read_sql, read_sql_query labels Jan 12, 2016
@anirudhjoshi
Copy link
Author

Sure thing. I did a search, must have missed it.

Although I believe his error raise is in the wrong area.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants