|
13 | 13 | from pandas.tools.merge import concat
|
14 | 14 | from pandas.core.common import PandasError
|
15 | 15 |
|
16 |
| - |
| 16 | +_IMPORTS = False |
17 | 17 | _GOOGLE_API_CLIENT_INSTALLED = False
|
18 | 18 | _GOOGLE_API_CLIENT_VALID_VERSION = False
|
19 | 19 | _GOOGLE_FLAGS_INSTALLED = False
|
20 | 20 | _GOOGLE_FLAGS_VALID_VERSION = False
|
21 | 21 | _HTTPLIB2_INSTALLED = False
|
22 | 22 | _SETUPTOOLS_INSTALLED = False
|
23 | 23 |
|
24 |
| -if not compat.PY3: |
| 24 | +def _importers(): |
| 25 | + # import things we need |
| 26 | + # but make this done on a first use basis |
25 | 27 |
|
26 |
| - try: |
27 |
| - import pkg_resources |
28 |
| - _SETUPTOOLS_INSTALLED = True |
29 |
| - except ImportError: |
30 |
| - _SETUPTOOLS_INSTALLED = False |
31 |
| - |
32 |
| - if _SETUPTOOLS_INSTALLED: |
33 |
| - try: |
34 |
| - from apiclient.discovery import build |
35 |
| - from apiclient.http import MediaFileUpload |
36 |
| - from apiclient.errors import HttpError |
| 28 | + global _IMPORTS |
| 29 | + if _IMPORTS: |
| 30 | + return |
37 | 31 |
|
38 |
| - from oauth2client.client import OAuth2WebServerFlow |
39 |
| - from oauth2client.client import AccessTokenRefreshError |
40 |
| - from oauth2client.client import flow_from_clientsecrets |
41 |
| - from oauth2client.file import Storage |
42 |
| - from oauth2client.tools import run |
43 |
| - _GOOGLE_API_CLIENT_INSTALLED=True |
44 |
| - _GOOGLE_API_CLIENT_VERSION = pkg_resources.get_distribution('google-api-python-client').version |
| 32 | + _IMPORTS = True |
45 | 33 |
|
46 |
| - if LooseVersion(_GOOGLE_API_CLIENT_VERSION) >= '1.2.0': |
47 |
| - _GOOGLE_API_CLIENT_VALID_VERSION = True |
| 34 | + if not compat.PY3: |
48 | 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 |
49 | 43 | except ImportError:
|
50 |
| - _GOOGLE_API_CLIENT_INSTALLED = False |
| 44 | + _SETUPTOOLS_INSTALLED = False |
51 | 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 |
52 | 51 |
|
53 |
| - try: |
54 |
| - import gflags as flags |
55 |
| - _GOOGLE_FLAGS_INSTALLED = True |
| 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 |
56 | 59 |
|
57 |
| - _GOOGLE_FLAGS_VERSION = pkg_resources.get_distribution('python-gflags').version |
| 60 | + if LooseVersion(_GOOGLE_API_CLIENT_VERSION) >= '1.2.0': |
| 61 | + _GOOGLE_API_CLIENT_VALID_VERSION = True |
58 | 62 |
|
59 |
| - if LooseVersion(_GOOGLE_FLAGS_VERSION) >= '2.0': |
60 |
| - _GOOGLE_FLAGS_VALID_VERSION = True |
| 63 | + except ImportError: |
| 64 | + _GOOGLE_API_CLIENT_INSTALLED = False |
61 | 65 |
|
62 |
| - except ImportError: |
63 |
| - _GOOGLE_FLAGS_INSTALLED = False |
64 | 66 |
|
65 |
| - try: |
66 |
| - import httplib2 |
67 |
| - _HTTPLIB2_INSTALLED = True |
68 |
| - except ImportError: |
69 |
| - _HTTPLIB2_INSTALLED = False |
| 67 | + try: |
| 68 | + import gflags as flags |
| 69 | + _GOOGLE_FLAGS_INSTALLED = True |
| 70 | + |
| 71 | + _GOOGLE_FLAGS_VERSION = pkg_resources.get_distribution('python-gflags').version |
| 72 | + |
| 73 | + if LooseVersion(_GOOGLE_FLAGS_VERSION) >= '2.0': |
| 74 | + _GOOGLE_FLAGS_VALID_VERSION = True |
| 75 | + |
| 76 | + except ImportError: |
| 77 | + _GOOGLE_FLAGS_INSTALLED = False |
| 78 | + |
| 79 | + try: |
| 80 | + import httplib2 |
| 81 | + _HTTPLIB2_INSTALLED = True |
| 82 | + except ImportError: |
| 83 | + _HTTPLIB2_INSTALLED = False |
70 | 84 |
|
71 | 85 |
|
72 | 86 | logger = logging.getLogger('pandas.io.gbq')
|
@@ -118,8 +132,10 @@ class InvalidColumnOrder(PandasError, IOError):
|
118 | 132 | """
|
119 | 133 | pass
|
120 | 134 |
|
121 |
| -class GbqConnector: |
| 135 | +class GbqConnector(object): |
| 136 | + |
122 | 137 | def __init__(self, project_id, reauth=False):
|
| 138 | + |
123 | 139 | self.project_id = project_id
|
124 | 140 | self.reauth = reauth
|
125 | 141 | self.credentials = self.get_credentials()
|
@@ -298,6 +314,8 @@ def _parse_entry(field_value, field_type):
|
298 | 314 | return field_value
|
299 | 315 |
|
300 | 316 | def _test_imports():
|
| 317 | + |
| 318 | + _importers() |
301 | 319 | _GOOGLE_API_CLIENT_INSTALLED
|
302 | 320 | _GOOGLE_API_CLIENT_VALID_VERSION
|
303 | 321 | _GOOGLE_FLAGS_INSTALLED
|
@@ -410,8 +428,8 @@ def to_gbq(dataframe, destination_table, project_id=None, chunksize=10000,
|
410 | 428 | the defined table schema and column types. For simplicity, this method
|
411 | 429 | uses the Google BigQuery streaming API. The to_gbq method chunks data
|
412 | 430 | into a default chunk size of 10,000. Failures return the complete error
|
413 |
| - response which can be quite long depending on the size of the insert. |
414 |
| - There are several important limitations of the Google streaming API |
| 431 | + response which can be quite long depending on the size of the insert. |
| 432 | + There are several important limitations of the Google streaming API |
415 | 433 | which are detailed at:
|
416 | 434 | https://developers.google.com/bigquery/streaming-data-into-bigquery.
|
417 | 435 |
|
|
0 commit comments