Skip to content

Commit 2655dae

Browse files
partheajorisvandenbossche
authored andcommitted
In gbq, use googleapiclient instead of apiclient #13454 (#13458)
closes #13454
1 parent cc0a188 commit 2655dae

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

doc/source/whatsnew/v0.18.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,5 @@ Bug Fixes
528528

529529
- Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`)
530530
- Bug in ``groupby`` with ``as_index=False`` returns all NaN's when grouping on multiple columns including a categorical one (:issue:`13204`)
531+
532+
- Bug where ``pd.read_gbq()`` could throw ``ImportError: No module named discovery`` as a result of a naming conflict with another python package called apiclient (:issue:`13454`)

pandas/io/gbq.py

+30-8
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ def _test_google_api_imports():
4646

4747
try:
4848
import httplib2 # noqa
49-
from apiclient.discovery import build # noqa
50-
from apiclient.errors import HttpError # noqa
49+
try:
50+
from googleapiclient.discovery import build # noqa
51+
from googleapiclient.errors import HttpError # noqa
52+
except:
53+
from apiclient.discovery import build # noqa
54+
from apiclient.errors import HttpError # noqa
5155
from oauth2client.client import AccessTokenRefreshError # noqa
5256
from oauth2client.client import OAuth2WebServerFlow # noqa
5357
from oauth2client.file import Storage # noqa
@@ -266,7 +270,10 @@ def sizeof_fmt(num, suffix='b'):
266270

267271
def get_service(self):
268272
import httplib2
269-
from apiclient.discovery import build
273+
try:
274+
from googleapiclient.discovery import build
275+
except:
276+
from apiclient.discovery import build
270277

271278
http = httplib2.Http()
272279
http = self.credentials.authorize(http)
@@ -315,7 +322,10 @@ def process_insert_errors(self, insert_errors):
315322
raise StreamingInsertError
316323

317324
def run_query(self, query):
318-
from apiclient.errors import HttpError
325+
try:
326+
from googleapiclient.errors import HttpError
327+
except:
328+
from apiclient.errors import HttpError
319329
from oauth2client.client import AccessTokenRefreshError
320330

321331
_check_google_client_version()
@@ -420,7 +430,10 @@ def run_query(self, query):
420430
return schema, result_pages
421431

422432
def load_data(self, dataframe, dataset_id, table_id, chunksize):
423-
from apiclient.errors import HttpError
433+
try:
434+
from googleapiclient.errors import HttpError
435+
except:
436+
from apiclient.errors import HttpError
424437

425438
job_id = uuid.uuid4().hex
426439
rows = []
@@ -474,7 +487,10 @@ def load_data(self, dataframe, dataset_id, table_id, chunksize):
474487
self._print("\n")
475488

476489
def verify_schema(self, dataset_id, table_id, schema):
477-
from apiclient.errors import HttpError
490+
try:
491+
from googleapiclient.errors import HttpError
492+
except:
493+
from apiclient.errors import HttpError
478494

479495
try:
480496
return (self.service.tables().get(
@@ -765,7 +781,10 @@ class _Table(GbqConnector):
765781

766782
def __init__(self, project_id, dataset_id, reauth=False, verbose=False,
767783
private_key=None):
768-
from apiclient.errors import HttpError
784+
try:
785+
from googleapiclient.errors import HttpError
786+
except:
787+
from apiclient.errors import HttpError
769788
self.http_error = HttpError
770789
self.dataset_id = dataset_id
771790
super(_Table, self).__init__(project_id, reauth, verbose, private_key)
@@ -865,7 +884,10 @@ class _Dataset(GbqConnector):
865884

866885
def __init__(self, project_id, reauth=False, verbose=False,
867886
private_key=None):
868-
from apiclient.errors import HttpError
887+
try:
888+
from googleapiclient.errors import HttpError
889+
except:
890+
from apiclient.errors import HttpError
869891
self.http_error = HttpError
870892
super(_Dataset, self).__init__(project_id, reauth, verbose,
871893
private_key)

pandas/io/tests/test_gbq.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ def _test_imports():
7373

7474
if _SETUPTOOLS_INSTALLED:
7575
try:
76-
from apiclient.discovery import build # noqa
77-
from apiclient.errors import HttpError # noqa
76+
try:
77+
from googleapiclient.discovery import build # noqa
78+
from googleapiclient.errors import HttpError # noqa
79+
except:
80+
from apiclient.discovery import build # noqa
81+
from apiclient.errors import HttpError # noqa
7882

7983
from oauth2client.client import OAuth2WebServerFlow # noqa
8084
from oauth2client.client import AccessTokenRefreshError # noqa
@@ -280,6 +284,17 @@ class GBQUnitTests(tm.TestCase):
280284
def setUp(self):
281285
test_requirements()
282286

287+
def test_import_google_api_python_client(self):
288+
if compat.PY2:
289+
with tm.assertRaises(ImportError):
290+
from googleapiclient.discovery import build # noqa
291+
from googleapiclient.errors import HttpError # noqa
292+
from apiclient.discovery import build # noqa
293+
from apiclient.errors import HttpError # noqa
294+
else:
295+
from googleapiclient.discovery import build # noqa
296+
from googleapiclient.errors import HttpError # noqa
297+
283298
def test_should_return_bigquery_integers_as_python_floats(self):
284299
result = gbq._parse_entry(1, 'INTEGER')
285300
tm.assert_equal(result, float(1))

0 commit comments

Comments
 (0)