简体   繁体   English

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

[英]Unable to create a virtual environment for Python 2.7: setuptools fails with UnicodeEncodeError

I am using Ubuntu 14.04LTS, with system Python as well as Anaconda Python installed. 我正在使用Ubuntu 14.04LTS,安装了系统Python和Anaconda Python。 After failing to get virtualenv to work with either, I decided to have a separate install of Python 2.7.11 that I built from source, for the purposes of using virtualenv. 在未能让virtualenv与其中任何一个一起工作之后,我决定单独安装我从源代码构建的Python 2.7.11,以便使用virtualenv。 Unfortunately, it is still not quite in a working state. 不幸的是,它还没有完全处于工作状态。 When I attempt to create a virtualenv, it throws UnicodeEncodeError. 当我尝试创建virtualenv时,它会抛出UnicodeEncodeError。 I've isolated the source of the error to the part when virtualenv setup trying to install setuptools. 当virtualenv设置尝试安装setuptools时,我已将错误的来源隔离到该部分。 That is, if I first throw in a --no-setuptools switch: 也就是说,如果我第一次投入--no-setuptools开关:

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

followed by 其次是

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

I get the following traceback: 我得到以下回溯:

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)

However, running this under sudo works fine. 但是,在sudo下运行它可以正常工作。 However, this means that I have to do the sudo workaround every time I set up a virtual environment, which is suboptimal. 但是,这意味着每次设置虚拟环境时都必须执行sudo解决方法,这是次优的。

Things that I've tried: made sure that I built python with UCS4 support, made sure that LC_ALL and LANG variables are en_US.UTF-8. 我尝试过的事情:确保我使用UCS4支持构建python,确保LC_ALL和LANG变量是en_US.UTF-8。

Yay !! 好极了 !! Found the answer !! 找到答案!! I got the same issue and it struck me ! 我遇到了同样的问题,这让我很震惊! my pip cache was corrupted. 我的pip缓存已损坏。 So I removed the following folders and reinstalled virtualenv :) 所以我删除了以下文件夹并重新安装了virtualenv :)

The strange thing on your side is that it seems you are mixing your system's python with your python (/usr/lib is system, $HOME/mypython should be your local build). 您身边的奇怪之处在于,您似乎正在将系统的python与您的python混合(/ usr / lib是系统,$ HOME / mypython应该是您的本地构建)。 to make sure you don't step on the sysem's foot, before building you should add "--prefix=$HOME/mypython" in the when running the "./configure" step. 为了确保你没有踩到sysem的脚,在构建之前你应该在运行“./configure”步骤时添加“--prefix = $ HOME / mypython”。 Then all of your python installation will be in $HOME/mypython, and should not require any sudo privileges (writing on /usr requires sudo privileges) 然后你的所有python安装都将在$ HOME / mypython中,并且不应该要求任何sudo权限(写在/ usr上需要sudo权限)

If you built you python from source I recommend installing pip and virtualenv from it 如果你从源代码构建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

Also I am using Ubuntu 14.04 with a version of python 2.7.13 built from sources. 此外,我正在使用Ubuntu 14.04,其中包含从源代码构建的python 2.7.13版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM