Skip to content

Commit 7f5a45c

Browse files
BUG/ERR: raise correct error when sql driver is not installed (#14527)
When the driver was not installed, but sqlalchemy itself was, when passing a URI string, you got an error indicating that SQLAlchemy was not installed, instead of the driver not being installed. This was because the import error for the driver was captured as import error for sqlalchemy.
1 parent 096d886 commit 7f5a45c

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pandas/io/sql.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,11 @@ def _engine_builder(con):
507507
if isinstance(con, string_types):
508508
try:
509509
import sqlalchemy
510-
con = sqlalchemy.create_engine(con)
511-
return con
512510
except ImportError:
513511
_SQLALCHEMY_INSTALLED = False
512+
else:
513+
con = sqlalchemy.create_engine(con)
514+
return con
514515

515516
return con
516517

pandas/io/tests/test_sql.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ def test_sqlalchemy_type_mapping(self):
944944
self.assertTrue(isinstance(
945945
table.table.c['time'].type, sqltypes.DateTime))
946946

947-
def test_to_sql_read_sql_with_database_uri(self):
947+
def test_database_uri_string(self):
948948

949949
# Test read_sql and .to_sql method with a database URI (GH10654)
950950
test_frame1 = self.test_frame1
@@ -963,6 +963,12 @@ def test_to_sql_read_sql_with_database_uri(self):
963963
tm.assert_frame_equal(test_frame1, test_frame3)
964964
tm.assert_frame_equal(test_frame1, test_frame4)
965965

966+
# using driver that will not be installed on Travis to trigger error
967+
# in sqlalchemy.create_engine -> test passing of this error to user
968+
db_uri = "postgresql+pg8000://user:pass@host/dbname"
969+
with tm.assertRaisesRegexp(ImportError, "pg8000"):
970+
sql.read_sql("select * from table", db_uri)
971+
966972
def _make_iris_table_metadata(self):
967973
sa = sqlalchemy
968974
metadata = sa.MetaData()

0 commit comments

Comments
 (0)