GSpread ImportError:没有名为oauth2client.service_account的模块

时间:2022-03-16 15:18:19

Ok I'm following along with the Become A Technical Marketer course and I'm trying to learn how to manage Google Spreadsheets with GSpread. I've followed along with the documentation at http://gspread.readthedocs.io/en/latest/oauth2.html. I've followed the steps in the second URL above and ran a document with the following code:

好的,我正在跟随成为一名技术营销人员的课程,我正在尝试学习如何使用GSpread管理谷歌电子表格。我已经跟踪了http://gspread.readthedocs.io/en/latest/oauth2.html的文档。我已经按照上面第二个URL中的步骤运行了一个包含以下代码的文档:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('DFS Google Sheets Data Imports-7205de852ff7.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open("Authority Scraper").sheet1
wks.update_cell(1,2,"Hello World!")

From that I get the error in my terminal: from oauth2client.service_account import ServiceAccountCredentials ImportError: No module named oauth2client.service_account

从这里我得到了我的终端的错误:来自oauth2client。service_account import ServiceAccountCredentials ImportError:没有名为oauth2client.service_account的模块

terminal error printout

终端打印错误

Someone please help me. The answers with other No module named oath2client.service_account are not working for me. Thanks!

请帮我的人。与其他没有模块的oath2client的答案。service_account对我不起作用。谢谢!

4 个解决方案

#1


3  

According to this discussion,

根据这一讨论,

This is because OS X El Capitan ships with six 1.4.1 installed already and when it attempts to uninstall it, it doesn't have permission to do so because System Integrity Protection doesn't allow even root to modify those directories.

这是因为OS X El Capitan已经安装了6个1.4.1,当它试图卸载时,它没有权限这样做,因为系统完整性保护甚至不允许root用户修改这些目录。

Amongst the few workarounds mentionned in the answers, it may be worth trying pip install --ignore-installed six to avoid the attempted uninstall of the system's six package.

在答案中提到的几个解决方案中,可能值得尝试安装pip安装——忽略安装的6个,以避免试图卸载系统的六个包。

#2


1  

Running this command worked for me - sudo pip install --upgrade oauth2client

运行这个命令对我- sudo pip安装-升级oauth2client有效

Got this from the oauth2client library github repo

这是来自oauth2client库github repo的

#3


1  

You can fix the error by checking if there is more than one folder in your library.

您可以通过检查库中是否有多个文件夹来修复错误。

C:\Python27\Lib\site-packages\oauth2client
C:\Program Files (x86)\Google\Google_Appengine\lib\google-api-python-client\oauth2client

you just need to delete one of the folders or change the library path in the compiler.

您只需删除其中一个文件夹或更改编译器中的库路径。

Here is the link to a video that I made on the topic:

这是我为这个主题制作的视频链接:

How to fix: "No module named service_account" - Python

如何修复:“没有名为service_account的模块”——Python ?

#4


0  

So I just encountered this issue as well and it ended up being a path issue for me. Granted, I know this is a little far-fetched (since everyone's dev environment is different), but writing this here in case it helps someone else.

所以我也遇到了这个问题它最终成为了我的路径问题。当然,我知道这有点牵强(因为每个人的开发环境都不一样),但是在这里写这篇文章是为了帮助其他人。

The TLDR make sure something isn't mucking with your $PYTHONPATH.

TLDR确保您的$PYTHONPATH没有出错。

Recall that when you do an "import" in python, python checks your sys.path for packages. This list has a precedence order (i.e. if a package is found in an earlier path in the list, then that package will be used).

回想一下,在python中执行“导入”时,python检查系统。包的路径。这个列表具有优先顺序(例如,如果在列表中较早的路径中找到一个包,那么将使用该包)。

In my case, it looks like my $PYTHONPATH had been modified when doing some appengine stuff a while ago. As it turns out, my appengine had its own oauth2client lib that's pretty old.

在我的例子中,我的$PYTHONPATH在不久前做一些appengine的时候已经被修改了。事实证明,我的appengine有自己的oauth2client lib,它非常古老。

As a result, when python attempted from oauth2client.service_account it was grabbing the oauth2client in appengine rather than the oauth2client I was expecting it to grab (a result of the $PYTHONPATH having been modified).

因此,当python尝试从oauth2client访问时。service_account它在appengine中获取oauth2client,而不是我期望它获取的oauth2client(由于修改了$PYTHONPATH)。

You can verify if this is happening to you as well by printing the sys.path before your import call:

您可以通过打印sys来验证这种情况是否也发生在您身上。导入调用前的路径:

import sys
print sys.path
from oauth2client.service_account import ServiceAccountCredentials

In my case I could clearly see a bunch of appengine paths that were taking precedence. This lead me to check my ~/.bash_profile where wala I found this line:

在我的例子中,我可以清楚地看到一堆优先级较高的appengine路径。这导致我检查我的~。我在bash_profile中找到了这一行:

export PYTHONPATH=$PYTHONPATH::$LOCAL_APPENGINE_HOME/lib/:$LOCAL_APPENGINE_HOME/lib/yaml/:$LOCAL_APPENGINE_HOME:$LOCAL_APPENGINE_HOME/lib/webapp2-2.5.2/`

Commented that out, started a new shell and everything worked dandy.

说出来了,开始一个新的壳,一切都很好。

#1


3  

According to this discussion,

根据这一讨论,

This is because OS X El Capitan ships with six 1.4.1 installed already and when it attempts to uninstall it, it doesn't have permission to do so because System Integrity Protection doesn't allow even root to modify those directories.

这是因为OS X El Capitan已经安装了6个1.4.1,当它试图卸载时,它没有权限这样做,因为系统完整性保护甚至不允许root用户修改这些目录。

Amongst the few workarounds mentionned in the answers, it may be worth trying pip install --ignore-installed six to avoid the attempted uninstall of the system's six package.

在答案中提到的几个解决方案中,可能值得尝试安装pip安装——忽略安装的6个,以避免试图卸载系统的六个包。

#2


1  

Running this command worked for me - sudo pip install --upgrade oauth2client

运行这个命令对我- sudo pip安装-升级oauth2client有效

Got this from the oauth2client library github repo

这是来自oauth2client库github repo的

#3


1  

You can fix the error by checking if there is more than one folder in your library.

您可以通过检查库中是否有多个文件夹来修复错误。

C:\Python27\Lib\site-packages\oauth2client
C:\Program Files (x86)\Google\Google_Appengine\lib\google-api-python-client\oauth2client

you just need to delete one of the folders or change the library path in the compiler.

您只需删除其中一个文件夹或更改编译器中的库路径。

Here is the link to a video that I made on the topic:

这是我为这个主题制作的视频链接:

How to fix: "No module named service_account" - Python

如何修复:“没有名为service_account的模块”——Python ?

#4


0  

So I just encountered this issue as well and it ended up being a path issue for me. Granted, I know this is a little far-fetched (since everyone's dev environment is different), but writing this here in case it helps someone else.

所以我也遇到了这个问题它最终成为了我的路径问题。当然,我知道这有点牵强(因为每个人的开发环境都不一样),但是在这里写这篇文章是为了帮助其他人。

The TLDR make sure something isn't mucking with your $PYTHONPATH.

TLDR确保您的$PYTHONPATH没有出错。

Recall that when you do an "import" in python, python checks your sys.path for packages. This list has a precedence order (i.e. if a package is found in an earlier path in the list, then that package will be used).

回想一下,在python中执行“导入”时,python检查系统。包的路径。这个列表具有优先顺序(例如,如果在列表中较早的路径中找到一个包,那么将使用该包)。

In my case, it looks like my $PYTHONPATH had been modified when doing some appengine stuff a while ago. As it turns out, my appengine had its own oauth2client lib that's pretty old.

在我的例子中,我的$PYTHONPATH在不久前做一些appengine的时候已经被修改了。事实证明,我的appengine有自己的oauth2client lib,它非常古老。

As a result, when python attempted from oauth2client.service_account it was grabbing the oauth2client in appengine rather than the oauth2client I was expecting it to grab (a result of the $PYTHONPATH having been modified).

因此,当python尝试从oauth2client访问时。service_account它在appengine中获取oauth2client,而不是我期望它获取的oauth2client(由于修改了$PYTHONPATH)。

You can verify if this is happening to you as well by printing the sys.path before your import call:

您可以通过打印sys来验证这种情况是否也发生在您身上。导入调用前的路径:

import sys
print sys.path
from oauth2client.service_account import ServiceAccountCredentials

In my case I could clearly see a bunch of appengine paths that were taking precedence. This lead me to check my ~/.bash_profile where wala I found this line:

在我的例子中,我可以清楚地看到一堆优先级较高的appengine路径。这导致我检查我的~。我在bash_profile中找到了这一行:

export PYTHONPATH=$PYTHONPATH::$LOCAL_APPENGINE_HOME/lib/:$LOCAL_APPENGINE_HOME/lib/yaml/:$LOCAL_APPENGINE_HOME:$LOCAL_APPENGINE_HOME/lib/webapp2-2.5.2/`

Commented that out, started a new shell and everything worked dandy.

说出来了,开始一个新的壳,一切都很好。