gpt4 book ai didi

python - 无法为Python 2.7创建虚拟环境: setuptools fails with UnicodeEncodeError

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:30 25 4
gpt4 key购买 nike

我使用的是 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 测试

其次是

/home/leo/tmp/test/bin/pip install setuptools # 在没有 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 解决方法,这是次优的。

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

最佳答案

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

你这边奇怪的事情是,你似乎将系统的 python 与你的 python 混合在一起(/usr/lib 是系统,$HOME/mypython 应该是你的本地构建)。为了确保您不会踩到系统的脚,在构建之前,您应该在运行“./configure”步骤时添加“--prefix=$HOME/mypython”。那么你所有的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 版本。

关于python - 无法为Python 2.7创建虚拟环境: setuptools fails with UnicodeEncodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37873500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com