我有一个Jypyter笔记本,它使用Pandas作为工具访问大型查询:
df = pd.io.gbq.read_gbq( query, project_id = 'xxxxxxx-xxxx' )
这在我的本地机器上运行得很好!(事实上,太好了!)但是,当我将相同的笔记本加载到云DataLab时,我得到:
DistributionNotFound: google-api-python-client
这似乎相当令人失望!我认为这个模块应该安装Pandas。但不知何故,谷歌并没有把它包括在内?最可取的理由是不必将代码从我们在本地机器上开发的代码更改为云DataLab中所需的代码,在这种情况下,我们对数据访问进行了大量参数化.
好吧我跑了:
!pip install --upgrade google-api-python-client
现在,当我运行笔记本时,我会得到一个无法解决的提示,因为DataLab位于远程计算机上:
Your browser has been opened to visit:
>>> Browser string>>>>
If your browser is on a different machine then exit and re-run this
application with the command-line parameter
--noauth_local_webserver
找不到明显的答案吗?
在更新笔记本中的google python-客户端之后,我使用@Anthonios在同一个笔记本中(在一个单元格块中执行它)建议的代码,我得到了以下跟踪:
TypeError Traceback (most recent call last)
<ipython-input-3-038366843e56> in <module>()
5 scope='https://www.googleapis.com/auth/bigquery',
6 redirect_uri='urn:ietf:wg:oauth:2.0:oob')
----> 7 storage = Storage('bigquery_credentials.dat')
8 authorize_url = flow.step1_get_authorize_url()
9 print 'Go to the following link in your browser: ' + authorize_url
/usr/local/lib/python2.7/dist-packages/oauth2client/file.pyc in __init__(self, filename)
37
38 def __init__(self, filename):
---> 39 super(Storage, self).__init__(lock=threading.Lock())
40 self._filename = filename
41
TypeError: object.__init__() takes no parameters
他提到需要从同一个文件夹执行笔记本,但我所知道的执行datalab笔记本的唯一方法是通过回购?
虽然新的模块使用新的朱庇特Datalab模块是一种可能的选择,能够使用完整的潘达烧烤接口不变的地方和DataLab实例将是非常有帮助的!所以请用我的手指来解决问题!
pip installed:
GCPDataLab 0.1.0
GCPData 0.1.0
wheel 0.29.0
tensorflow 0.6.0
protobuf 3.0.0a3
oauth2client 1.4.12
futures 3.0.3
pexpect 4.0.1
terminado 0.6
pyasn1 0.1.9
jsonschema 2.5.1
mistune 0.7.2
statsmodels 0.6.1
path.py 8.1.2
ipython 4.1.2
nose 1.3.7
MarkupSafe 0.23
py-dateutil 2.2
pyparsing 2.1.1
pickleshare 0.6
pandas 0.18.0
singledispatch 3.4.0.3
PyYAML 3.11
nbformat 4.0.1
certifi 2016.2.28
notebook 4.0.2
cycler 0.10.0
scipy 0.17.0
ipython-genutils 0.1.0
pyasn1-modules 0.0.8
functools32 3.2.3-2
ipykernel 4.3.1
pandocfilters 1.2.4
decorator 4.0.9
jupyter-core 4.1.0
rsa 3.4.2
mock 1.3.0
httplib2 0.9.2
pytz 2016.3
sympy 0.7.6
numpy 1.11.0
seaborn 0.6.0
pbr 1.8.1
backports.ssl-match-hostname 3.5.0.1
ggplot 0.6.5
simplegeneric 0.8.1
ptyprocess 0.5.1
funcsigs 0.4
scikit-learn 0.16.1
traitlets 4.2.1
jupyter-client 4.2.2
nbconvert 4.1.0
matplotlib 1.5.1
patsy 0.4.1
tornado 4.3
python-dateutil 2.5.2
Jinja2 2.8
backports-abc 0.4
brewer2mpl 1.4.1
Pygments 2.1.3
结束
发布于 2016-06-13 19:34:38
谷歌对熊猫的BigQuery认证通常是直接的,除非在远程服务器上执行熊猫代码。例如,在云中的Datalab上运行熊猫。在这种情况下,使用以下代码创建熊猫在Google中访问Google BigQuery所需的凭据文件。
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
flow = OAuth2WebServerFlow(client_id='<Client ID from Google API Console>',
client_secret='<Client secret from Google API Console>',
scope='https://www.googleapis.com/auth/bigquery',
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
storage = Storage('bigquery_credentials.dat')
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ')
credentials = flow.step2_exchange(code)
storage.put(credentials)
一旦您完成了这个过程,我不期望您会看到错误(只要笔记本与新创建的'bigquery_credentials.dat'
文件位于同一个文件夹中)。
您还需要安装google-api-python-client
python包,因为它是谷歌BigQuery支持的熊猫所需。您可以在笔记本中运行以下任一项来安装它。
任一
!pip install google-api-python-client --no-deps
!pip install uritemplate --no-deps
!pip install simplejson --no-deps
或
%%bash
pip install google-api-python-client --no-deps
pip install uritemplate --no-deps
pip install simplejson --no-deps
--no-deps
选项是必需的,这样您就不会意外地更新默认安装在datalab中的python包(以确保datalab的其他部分不会中断)。
注意:随着熊猫的0.19.0 (尚未发布),在中使用熊猫就容易多了。请参阅拉动请求#13608
注意:您还可以选择在jupyter中使用(新的)模块(这样,代码也将在云中的中工作)。请参见以下相关堆栈溢出答案:如何使用之外的gcp包?
https://stackoverflow.com/questions/37792709
复制