問題描述
Pandas / Google Analytics API 身份驗證嘗試給我帶來了一個奇怪的 python 錯誤 (Pandas / Google Analytics API authentication attempt throws me a weird python error)
在我的 pandas/Google Analytics API 設置過程中,我基本上按照此鏈接中的說明進行了所有操作:
http://blog.yhathq.com/posts/pandas‑google‑analytics.html
client_secrets.json 在 pandas/io文件夾。當我現在嘗試執行格式為
>>>from pandas.io import ga
>>>df = ga.read_ga(metrics, dimensions, start_date)
的語句時,出現以下錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "\Anaconda\lib\site‑packages\pandas\io\ga.py", line 110, in read_ga
reader = GAnalytics(**reader_kwds)
File "\Anaconda\lib\site‑packages\pandas\io\ga.py", line 179, in __init__
self._service = self._init_service(secrets)
File "\Anaconda\lib\site‑packages\pandas\io\ga.py", line 191, in _init_service
http = self.authenticate(secrets)
File "\Anaconda\lib\site‑packages\pandas\io\ga.py", line 151, in authenticate
return auth.authenticate(flow, self.token_store)
File "\Anaconda\lib\site‑packages\pandas\io\auth.py", line 108, in authenticate
credentials = tools.run(flow, storage)
AttributeError: 'module' object has no attribute 'run'
根據yhat鏈接,我的瀏覽器應該打開以進行身份驗證。
注意:我沒有為“已安裝的應用程序”創建客戶端 ID,因為我在創建 ID 時在菜單中沒有這個選項。相反,我選擇了“其他”。不過,這不應該是錯誤的原因。
第二個提示:我最近將我的 pandas 更新到了 0.17.1。導入 pandas.io.ga 時,我收到 .ga 模塊已棄用的消息。此外,我手動安裝了 gflags 模塊,因為當我第一次嘗試導入 .io.ga 時需要它。
參考解法
方法 1:
Either file a ticket with the owners of Pandas to change (currently) line 108 of pandas/io/auth.py from run()
to run_flow()
, or make the fix yourself and file a PR. (Yes, it would've been nice if Google had just made run_flow()
and alias of run()
, but as you can imagine, this is not how this change evolved, so we have to live with it.)
For other developers running into this error: If you have the latest version (as of Feb 2016) of the Google APIs Client Library for Python, just rename your call from tools.run()
to tools.run_flow()
, and you should be good‑to‑go. More about this change in a PSA (public service announcement) blogpost I wrote back in mid‑2015 but update periodically to stay current.
The fastest way to upgrade your Client Library is with: pip install ‑U google‑api‑python‑client # or pip3 for 3.x