Leo Alekseyev
Leo Alekseyev

Reputation: 13483

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. 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. Unfortunately, it is still not quite in a working state. When I attempt to create a virtualenv, it throws UnicodeEncodeError. I've isolated the source of the error to the part when virtualenv setup trying to install setuptools. That is, if I first throw in a --no-setuptools switch:

/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. However, this means that I have to do the sudo workaround every time I set up a virtual environment, which is suboptimal.

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.

Upvotes: 1

Views: 1023

Answers (1)

Azurtree
Azurtree

Reputation: 379

Yay !! Found the answer !! I got the same issue and it struck me ! my pip cache was corrupted. So I removed the following folders and reinstalled 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). 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. Then all of your python installation will be in $HOME/mypython, and should not require any sudo privileges (writing on /usr requires sudo privileges)

If you built you python from source I recommend installing pip and virtualenv from it

# 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.

Upvotes: 1

Related Questions