Skip to content

Commit dbcf18a

Browse files
committed
Merge pull request #8409 from jacobschaer/issue-8107
Allow importing pandas without setuptools
2 parents 0c2c2ef + 27afaeb commit dbcf18a

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

doc/source/install.rst

+2
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ Optional Dependencies
302302
* Google's `python-gflags <http://code.google.com/p/python-gflags/>`__
303303
and `google-api-python-client <http://github.com/google/google-api-python-client>`__
304304
* Needed for :mod:`~pandas.io.gbq`
305+
* `setuptools <https://pypi.python.org/pypi/setuptools/>`__
306+
* Needed for :mod:`~pandas.io.gbq` (specifically, it utilizes `pkg_resources`)
305307
* `httplib2 <http://pypi.python.org/pypi/httplib2>`__
306308
* Needed for :mod:`~pandas.io.gbq`
307309
* One of the following combinations of libraries is needed to use the

pandas/io/gbq.py

+40-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import uuid
77

88
import numpy as np
9-
import pkg_resources
109

1110
from distutils.version import LooseVersion
1211
from pandas import compat
@@ -20,46 +19,54 @@
2019
_GOOGLE_FLAGS_INSTALLED = False
2120
_GOOGLE_FLAGS_VALID_VERSION = False
2221
_HTTPLIB2_INSTALLED = False
22+
_SETUPTOOLS_INSTALLED = False
2323

2424
if not compat.PY3:
25-
25+
2626
try:
27-
from apiclient.discovery import build
28-
from apiclient.http import MediaFileUpload
29-
from apiclient.errors import HttpError
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
3037

31-
from oauth2client.client import OAuth2WebServerFlow
32-
from oauth2client.client import AccessTokenRefreshError
33-
from oauth2client.client import flow_from_clientsecrets
34-
from oauth2client.file import Storage
35-
from oauth2client.tools import run
36-
_GOOGLE_API_CLIENT_INSTALLED=True
37-
_GOOGLE_API_CLIENT_VERSION = pkg_resources.get_distribution('google-api-python-client').version
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
3845

39-
if LooseVersion(_GOOGLE_API_CLIENT_VERSION >= '1.2.0'):
40-
_GOOGLE_API_CLIENT_VALID_VERSION = True
46+
if LooseVersion(_GOOGLE_API_CLIENT_VERSION >= '1.2.0'):
47+
_GOOGLE_API_CLIENT_VALID_VERSION = True
4148

42-
except ImportError:
43-
_GOOGLE_API_CLIENT_INSTALLED = False
49+
except ImportError:
50+
_GOOGLE_API_CLIENT_INSTALLED = False
4451

4552

46-
try:
47-
import gflags as flags
48-
_GOOGLE_FLAGS_INSTALLED = True
53+
try:
54+
import gflags as flags
55+
_GOOGLE_FLAGS_INSTALLED = True
4956

50-
_GOOGLE_FLAGS_VERSION = pkg_resources.get_distribution('python-gflags').version
57+
_GOOGLE_FLAGS_VERSION = pkg_resources.get_distribution('python-gflags').version
5158

52-
if LooseVersion(_GOOGLE_FLAGS_VERSION >= '2.0.0'):
53-
_GOOGLE_FLAGS_VALID_VERSION = True
59+
if LooseVersion(_GOOGLE_FLAGS_VERSION >= '2.0.0'):
60+
_GOOGLE_FLAGS_VALID_VERSION = True
5461

55-
except ImportError:
56-
_GOOGLE_FLAGS_INSTALLED = False
62+
except ImportError:
63+
_GOOGLE_FLAGS_INSTALLED = False
5764

58-
try:
59-
import httplib2
60-
_HTTPLIB2_INSTALLED = True
61-
except ImportError:
62-
_HTTPLIB2_INSTALLED = False
65+
try:
66+
import httplib2
67+
_HTTPLIB2_INSTALLED = True
68+
except ImportError:
69+
_HTTPLIB2_INSTALLED = False
6370

6471

6572
logger = logging.getLogger('pandas.io.gbq')
@@ -296,10 +303,14 @@ def _test_imports():
296303
_GOOGLE_FLAGS_INSTALLED
297304
_GOOGLE_FLAGS_VALID_VERSION
298305
_HTTPLIB2_INSTALLED
306+
_SETUPTOOLS_INSTALLED
299307

300308
if compat.PY3:
301309
raise NotImplementedError("Google's libraries do not support Python 3 yet")
302310

311+
if not _SETUPTOOLS_INSTALLED:
312+
raise ImportError('Could not import pkg_resources (setuptools).')
313+
303314
if not _GOOGLE_API_CLIENT_INSTALLED:
304315
raise ImportError('Could not import Google API Client.')
305316

0 commit comments

Comments
 (0)