1313from pandas .tools .merge import concat
1414from pandas .core .common import PandasError
1515
16- _IMPORTS = False
17- _GOOGLE_API_CLIENT_INSTALLED = False
18- _GOOGLE_API_CLIENT_VALID_VERSION = False
19- _GOOGLE_FLAGS_INSTALLED = False
20- _GOOGLE_FLAGS_VALID_VERSION = False
21- _HTTPLIB2_INSTALLED = False
22- _SETUPTOOLS_INSTALLED = False
2316
24- def _importers ():
25- # import things we need
26- # but make this done on a first use basis
27-
28- global _IMPORTS
29- if _IMPORTS :
30- return
31-
32- _IMPORTS = True
33-
34- if not compat .PY3 :
35-
36- global _GOOGLE_API_CLIENT_INSTALLED , _GOOGLE_API_CLIENT_VALID_VERSION , \
37- _GOOGLE_FLAGS_INSTALLED , _GOOGLE_FLAGS_VALID_VERSION , \
38- _HTTPLIB2_INSTALLED , _SETUPTOOLS_INSTALLED
39-
40- try :
41- import pkg_resources
42- _SETUPTOOLS_INSTALLED = True
43- except ImportError :
44- _SETUPTOOLS_INSTALLED = False
45-
46- if _SETUPTOOLS_INSTALLED :
47- try :
48- from apiclient .discovery import build
49- from apiclient .http import MediaFileUpload
50- from apiclient .errors import HttpError
51-
52- from oauth2client .client import OAuth2WebServerFlow
53- from oauth2client .client import AccessTokenRefreshError
54- from oauth2client .client import flow_from_clientsecrets
55- from oauth2client .file import Storage
56- from oauth2client .tools import run
57- _GOOGLE_API_CLIENT_INSTALLED = True
58- _GOOGLE_API_CLIENT_VERSION = pkg_resources .get_distribution ('google-api-python-client' ).version
59-
60- if LooseVersion (_GOOGLE_API_CLIENT_VERSION ) >= '1.2.0' :
61- _GOOGLE_API_CLIENT_VALID_VERSION = True
62-
63- except ImportError :
64- _GOOGLE_API_CLIENT_INSTALLED = False
65-
66-
67- try :
68- import gflags as flags
69- _GOOGLE_FLAGS_INSTALLED = True
70-
71- _GOOGLE_FLAGS_VERSION = pkg_resources .get_distribution ('python-gflags' ).version
17+ def _check_google_client_version ():
18+ if compat .PY3 :
19+ raise NotImplementedError ("Google's libraries do not support Python 3 yet" )
7220
73- if LooseVersion ( _GOOGLE_FLAGS_VERSION ) >= '2.0' :
74- _GOOGLE_FLAGS_VALID_VERSION = True
21+ try :
22+ import pkg_resources
7523
76- except ImportError :
77- _GOOGLE_FLAGS_INSTALLED = False
24+ except ImportError :
25+ raise ImportError ( 'Could not import pkg_resources (setuptools).' )
7826
79- try :
80- import httplib2
81- _HTTPLIB2_INSTALLED = True
82- except ImportError :
83- _HTTPLIB2_INSTALLED = False
27+ _GOOGLE_API_CLIENT_VERSION = pkg_resources .get_distribution ('google-api-python-client' ).version
8428
29+ if LooseVersion (_GOOGLE_API_CLIENT_VERSION ) < '1.2.0' :
30+ raise ImportError ("pandas requires google-api-python-client >= 1.2.0 for Google "
31+ "BigQuery support, current version " + _GOOGLE_API_CLIENT_VERSION )
8532
8633logger = logging .getLogger ('pandas.io.gbq' )
8734logger .setLevel (logging .ERROR )
@@ -142,6 +89,16 @@ def __init__(self, project_id, reauth=False):
14289 self .service = self .get_service (self .credentials )
14390
14491 def get_credentials (self ):
92+ try :
93+ from oauth2client .client import OAuth2WebServerFlow
94+ from oauth2client .file import Storage
95+ from oauth2client .tools import run_flow , argparser
96+
97+ except ImportError :
98+ raise ImportError ('Could not import Google API Client.' )
99+
100+ _check_google_client_version ()
101+
145102 flow = OAuth2WebServerFlow (client_id = '495642085510-k0tmvj2m941jhre2nbqka17vqpjfddtd.apps.googleusercontent.com' ,
146103 client_secret = 'kOc9wMptUtxkcIFbtZCcrEAc' ,
147104 scope = 'https://www.googleapis.com/auth/bigquery' ,
@@ -151,18 +108,41 @@ def get_credentials(self):
151108 credentials = storage .get ()
152109
153110 if credentials is None or credentials .invalid or self .reauth :
154- credentials = run (flow , storage )
111+ credentials = run_flow (flow , storage , argparser . parse_args ([]) )
155112
156113 return credentials
157114
158115 def get_service (self , credentials ):
116+ try :
117+ import httplib2
118+
119+ except ImportError :
120+ raise ImportError ("pandas requires httplib2 for Google BigQuery support" )
121+
122+ try :
123+ from apiclient .discovery import build
124+
125+ except ImportError :
126+ raise ImportError ('Could not import Google API Client.' )
127+
128+ _check_google_client_version ()
129+
159130 http = httplib2 .Http ()
160131 http = credentials .authorize (http )
161132 bigquery_service = build ('bigquery' , 'v2' , http = http )
162133
163134 return bigquery_service
164135
165136 def run_query (self , query ):
137+ try :
138+ from apiclient .errors import HttpError
139+ from oauth2client .client import AccessTokenRefreshError
140+
141+ except ImportError :
142+ raise ImportError ('Could not import Google API Client.' )
143+
144+ _check_google_client_version ()
145+
166146 job_collection = self .service .jobs ()
167147 job_data = {
168148 'configuration' : {
@@ -313,38 +293,6 @@ def _parse_entry(field_value, field_type):
313293 return field_value == 'true'
314294 return field_value
315295
316- def _test_imports ():
317-
318- _importers ()
319- _GOOGLE_API_CLIENT_INSTALLED
320- _GOOGLE_API_CLIENT_VALID_VERSION
321- _GOOGLE_FLAGS_INSTALLED
322- _GOOGLE_FLAGS_VALID_VERSION
323- _HTTPLIB2_INSTALLED
324- _SETUPTOOLS_INSTALLED
325-
326- if compat .PY3 :
327- raise NotImplementedError ("Google's libraries do not support Python 3 yet" )
328-
329- if not _SETUPTOOLS_INSTALLED :
330- raise ImportError ('Could not import pkg_resources (setuptools).' )
331-
332- if not _GOOGLE_API_CLIENT_INSTALLED :
333- raise ImportError ('Could not import Google API Client.' )
334-
335- if not _GOOGLE_FLAGS_INSTALLED :
336- raise ImportError ('Could not import Google Command Line Flags Module.' )
337-
338- if not _GOOGLE_API_CLIENT_VALID_VERSION :
339- raise ImportError ("pandas requires google-api-python-client >= 1.2.0 for Google "
340- "BigQuery support, current version " + _GOOGLE_API_CLIENT_VERSION )
341-
342- if not _GOOGLE_FLAGS_VALID_VERSION :
343- raise ImportError ("pandas requires python-gflags >= 2.0.0 for Google "
344- "BigQuery support, current version " + _GOOGLE_FLAGS_VERSION )
345-
346- if not _HTTPLIB2_INSTALLED :
347- raise ImportError ("pandas requires httplib2 for Google BigQuery support" )
348296
349297def read_gbq (query , project_id = None , index_col = None , col_order = None , reauth = False ):
350298 """Load data from Google BigQuery.
@@ -379,7 +327,6 @@ def read_gbq(query, project_id = None, index_col=None, col_order=None, reauth=Fa
379327
380328 """
381329
382- _test_imports ()
383330
384331 if not project_id :
385332 raise TypeError ("Missing required parameter: project_id" )
@@ -450,7 +397,6 @@ def to_gbq(dataframe, destination_table, project_id=None, chunksize=10000,
450397 if multiple accounts are used.
451398
452399 """
453- _test_imports ()
454400
455401 if not project_id :
456402 raise TypeError ("Missing required parameter: project_id" )
0 commit comments