diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 47642c2e2bc28..c9f8d32e1b504 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -507,10 +507,11 @@ def _engine_builder(con): if isinstance(con, string_types): try: import sqlalchemy - con = sqlalchemy.create_engine(con) - return con except ImportError: _SQLALCHEMY_INSTALLED = False + else: + con = sqlalchemy.create_engine(con) + return con return con diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index af8989baabbc0..e9d19bbd8be66 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -944,7 +944,7 @@ def test_sqlalchemy_type_mapping(self): self.assertTrue(isinstance( table.table.c['time'].type, sqltypes.DateTime)) - def test_to_sql_read_sql_with_database_uri(self): + def test_database_uri_string(self): # Test read_sql and .to_sql method with a database URI (GH10654) test_frame1 = self.test_frame1 @@ -963,6 +963,12 @@ def test_to_sql_read_sql_with_database_uri(self): tm.assert_frame_equal(test_frame1, test_frame3) tm.assert_frame_equal(test_frame1, test_frame4) + # using driver that will not be installed on Travis to trigger error + # in sqlalchemy.create_engine -> test passing of this error to user + db_uri = "postgresql+pg8000://user:pass@host/dbname" + with tm.assertRaisesRegexp(ImportError, "pg8000"): + sql.read_sql("select * from table", db_uri) + def _make_iris_table_metadata(self): sa = sqlalchemy metadata = sa.MetaData()