擅长:python、mysql、java
<p>所以我也遇到了这个问题,最后成了我的路径问题。当然,我知道这有点牵强(因为每个人的开发环境都不一样),但是在这里写这篇文章是为了帮助其他人。</p>
<p><strong><em>TLDR</em></strong>确保您的<code>$PYTHONPATH</code>没有弄脏任何东西。</p>
<p>回想一下,在python中执行“导入”时,python会检查sys.path中的包。此列表具有优先顺序(即,如果在列表中的较早路径中找到包,则将使用该包)。</p>
<p>在我的例子中,看起来我的<code>$PYTHONPATH</code>在前一段时间做一些appengine的事情时被修改了。事实证明,我的appengine有自己的<code>oauth2client</code>库,这个库已经很老了。</p>
<p>结果,当python尝试<code>from oauth2client.service_account</code>时,它捕获appengine中的<code>oauth2client</code>,而不是我期望它捕获的<code>oauth2client</code>(这是<code>$PYTHONPATH</code>被修改的结果)。</p>
<p>您可以在导入调用之前打印sys.path来验证是否也发生了这种情况:</p>
<pre><code>import sys
print sys.path
from oauth2client.service_account import ServiceAccountCredentials
</code></pre>
<p>在我的例子中,我可以清楚地看到一堆优先的路径。这让我检查一下我的<code>~/.bash_profile</code>我在哪里找到了这条线:</p>
<pre><code>export PYTHONPATH=$PYTHONPATH::$LOCAL_APPENGINE_HOME/lib/:$LOCAL_APPENGINE_HOME/lib/yaml/:$LOCAL_APPENGINE_HOME:$LOCAL_APPENGINE_HOME/lib/webapp2-2.5.2/`
</code></pre>
<p>评论说,开始了一个新的外壳和一切工作的花花公子。</p>