无法为Python 2.7创建虚拟环境:setuptools因UnicodeEncodeError而失败

时间:2016-06-17 05:03:31

标签: python python-2.7 unicode virtualenv

我正在使用Ubuntu 14.04LTS,安装了系统Python和Anaconda Python。在未能让virtualenv与其中任何一个一起工作之后,我决定单独安装我从源代码构建的Python 2.7.11,以便使用virtualenv。不幸的是,它还没有完全处于工作状态。当我尝试创建virtualenv时,它会抛出UnicodeEncodeError。当virtualenv设置尝试安装setuptools时,我已将错误的来源隔离到该部分。也就是说,如果我第一次投入--no-setuptools开关:

/usr/local/lib/python2.7.11/bin/virtualenv --no-setuptools test

接着是

/home/leo/tmp/test/bin/pip install setuptools # fails without sudo

我得到以下追溯:

Exception:
Traceback (most recent call last):
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/commands/install.py", line 310, in run
    wb.build(autobuilding=True)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/wheel.py", line 750, in build
    self.requirement_set.prepare_files(self.finder)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/req/req_set.py", line 370, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/req/req_set.py", line 522, in _prepare_file
    finder, self.upgrade, require_hashes)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/req/req_install.py", line 268, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/index.py", line 442, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/index.py", line 400, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/index.py", line 545, in _get_pages
    page = self._get_page(location)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/index.py", line 648, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/index.py", line 757, in get_page
    "Cache-Control": "max-age=600",
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/_vendor/requests/sessions.py", line 487, in get
    return self.request('GET', url, **kwargs)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/download.py", line 378, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/_vendor/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/_vendor/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/_vendor/cachecontrol/adapter.py", line 36, in send
    cached_response = self.controller.cached_request(request)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/_vendor/cachecontrol/controller.py", line 111, in cached_request
    resp = self.serializer.loads(request, cache_data)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/_vendor/cachecontrol/serialize.py", line 114, in loads
    return getattr(self, "_loads_v{0}".format(ver))(request, data)
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/_vendor/cachecontrol/serialize.py", line 176, in _loads_v2
    cached["response"]["body"]
  File "/home/leo/tmp/test/lib/python2.7/site-packages/pip/_vendor/cachecontrol/serialize.py", line 26, in _b64_decode_bytes
    return base64.b64decode(b.encode("ascii"))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-28790: ordinal not in range(128)

但是,在sudo下运行它可以正常工作。但是,这意味着每次设置虚拟环境时都必须执行sudo解决方法,这是次优的。

我尝试过的事情:确保我使用UCS4支持构建python,确保LC_ALL和LANG变量为en_US.UTF-8。

1 个答案:

答案 0 :(得分:0)

耶!找到答案!! 我遇到了同样的问题,这让我很震惊!我的pip缓存已损坏。 所以我删除了以下文件夹并重新安装了virtualenv:)

您身边的奇怪之处在于,您似乎正在将系统的python与您的python混合使用(/ usr / lib是系统,$ HOME / mypython应该是您的本地版本)。为了确保你不会踩到sysem脚,在建造之前你应该添加" - prefix = $ HOME / mypython"在运行" ./ configure"步。然后你的所有python安装都将在$ HOME / mypython中,并且不应该要求任何sudo权限(写在/ usr上需要sudo权限)

如果你从源代码构建python我建议从它安装pip和virtualenv

# let's assume your python installation is in this variable
my_python_path=/home/leo/tmp/test/

rm -Rf $HOME/.cache/pip
# not sure of the deletion of this one, but did it to be sure
rm -Rf $HOME/.pip
# remove of the pip and virtualenv
rm $my_python_path/bin/pip
rm $my_python_path/bin/virtualenv

# reinstall pip
$my_python_path/bin/python -m ensure pip
$my_python_path/bin/pip install virtualenv
$my_python_path/bin/virtualenv myenv

此外,我正在使用Ubuntu 14.04,其中包含从源代码构建的python 2.7.13版本。