在运行python脚本时使用比1.4.1更高版本的更高版本

时间:2017-02-27 23:57:04

标签: python macos google-api google-api-python-client

尝试使用google api客户端,我收到了其他许多人的错误:

AttributeError: 'Module_six_moves_urllib_parse' object has no attribute 'urlencode'

我已尝试在StackOverflow,GitHub和其他地方找到的每个解决方案,包括:

1)来自this thread,更改实际代码中的路径:

import sys
sys.path.insert(1, '/Library/Python/2.7/site-packages')

2)来自this thread,更改.bashrc和.bash_profile文件中的python路径:

pip show six | grep "Location:" | cut -d " " -f2
export PYTHONPATH=$PYTHONPATH:<pip_install_path>
source ~/.bashrc

3)并从this thread将我的google api客户端降级为1.3.2(或者至少尝试)。

我是编程的新手,所以这可能是一个基本问题,但我花了几天时间尝试进行故障排除,但无济于事。似乎无论我做什么,六个旧的1.4版本正在使用。您可以提供任何帮助,我们将非常感激!

编辑:完整追溯:

Traceback (most recent call last):
  File "/Users/zachgoldfine/PycharmProjects/FirstTry/GetAroundRentalSpreadsheetRead.py",     line 71, in <module>
    spreadsheetId=spreadsheetId, range=rangeName1).execute()
  File "/Library/Python/2.7/site-packages/oauth2client/util.py", line 129, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/googleapiclient/http.py", line 836, in execute
    method=str(self.method), body=self.body, headers=self.headers)
  File "/Library/Python/2.7/site-packages/googleapiclient/http.py", line 162, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/oauth2client/transport.py", line 186, in new_request
    credentials._refresh(orig_request_method)
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 761, in _refresh
    self._do_refresh_request(http)
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 774, in _do_refresh_request
    body = self._generate_refresh_request_body()
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 716, in _generate_refresh_request_body
    body = urllib.parse.urlencode({
AttributeError: 'Module_six_moves_urllib_parse' object has no attribute     'urlencode'

1 个答案:

答案 0 :(得分:0)

假设六个版本确实存在问题,这里有一种安装六个的方法可以使用新安装的版本。

重要说明:此功能仅适用于您的用户帐户;不是来自任何其他帐户 在安全方面:这不会改变系统Python环境,也就是说,可能使用Python的系统脚本将继续使用旧的pip版本。

首先,颠倒上面的三个步骤。特别是,在脚本中手动更改sys.path应该很少非常

然后,使用安装本地版本的--user选项,Python(当由该用户运行时)将首先自动获取。 要确保您使用的Python可执行文件对应于pip模块和(稍后)已安装的六个模块,请使用以下命令:

python -m pip install six --user

其中python可能稍微不同,如果您碰巧不使用系统Python(例如,z / usr / local / bin / python , or python3 , etc).
There is no need for
sudo`或类似。

如果pip抱怨该要求已经是最新的(它不应该,或者您不会遇到上述问题),请尝试:

python -m pip install six --user --upgrade --force

完成后,您可以查看$HOME/Library/Python/x.y/lib/python/site-packages以查看是否在那里看到正确的六个版本。这是您的本地用户库目录,而不是系统目录。 x.y可能是2.7,但请检查python实际上是否为该版本。

问题也可能出在google api客户端上。我不知道是否有pip安装,否则你可以尝试类似于六的东西:

python -m pip install <google-api-client> --user (--upgrade --force)