19

I had python 3.4 in my virtualenv, but after upgrading ubuntu to 16.04 python upgraded to 3.5 so python in virtualenv crashes with these errors:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Current thread 0x00007f2f2dbcb700 (most recent call first):
fish: “python” terminated by signal SIGABRT (Abort)

How can i fix it?

  • Have you tried re-installing Python 3.5? The `encodings` module is part of the base installation. – mprat Apr 22 '16 at 14:52

5 Answers5

4

I fixed this by installing the minimum working python3.4 so that my virtualenv worked well enough to get the list of packages, then made a new one with python3.5... as follows:

Get python3.4 minimal packages:

wget http://launchpadlibrarian.net/221250032/python3.4-minimal_3.4.3-1ubuntu1~14.04.3_amd64.deb
wget http://launchpadlibrarian.net/221250033/libpython3.4-minimal_3.4.3-1ubuntu1~14.04.3_amd64.deb
sudo dpkg -i --force-breaks libpython3.4-minimal_3.4.3-1ubuntu1~14.04.3_amd64.deb
sudo dpkg -i python3.4-minimal_3.4.3-1ubuntu1~14.04.3_amd64.deb

My virtualenv is here: ~/virtualenv/example

Get the list of packages in your virtualenv (which should now work well enough for this, but might not do other things properly):

source ~/virtualenv/example/bin/activate
pip freeze > /tmp/requirements.txt
deactivate 

Get rid of python3.4, to return to Ubuntu 16.04's preferred state:

sudo dpkg --purge python3.4-minimal
sudo dpkg --force-depends --purge libpython3.4-minimal

Make a new virtualenv with the right packages:

virtualenv -p python3.5 example
source ~/virtualenv/example/bin/activate
pip install -r /tmp/requirements.txt

That should now work, with all your old packages but in python3.5. Should...

See also Upgrade python in a virtualenv

Community
  • 1
  • 1
ed.
  • 1,373
  • 8
  • 10
1

I have same issue and i solved recreating the whole virtualenv

PS: Sorry for my bad English.

1

I had the same problem today and that is how I have solved it:

Problem: Firstly, as I understand, the problem occurs because after upgrading to Ubuntu 16.04 the previous version of Python also upgrades. As a result symbolic links inside any Python3 environment are not working anymore.

Solution 1: As it was written above the straightforward solution is to remove all the Python3 environments and create them again. I don't like it because it is second time I do it after upgrading Ubuntu. Also probably I need to use multiple Python 3 versions in the future projects.

Solution 2: That is what I have tried today and it is working fine. Instead of using virtualenv + virtualenvwrapper I decided to try combination of pyenv + pyenv-virtualenv.

The main difference between two approaches is:

Pyenv actually copies an entire Python installation every time you create a new pyenv version. In contrast, virtualenv makes use of symbolic links to decrease the size of the virtualenv’s.

Howto:

  1. Install pyenv as described here together with required versions of Python 2 and 3.
  2. Have a look here on how you can work with virtualenv using pyenv.
  3. Create new environment, install all the dependencies with pip and hopefully forget about the problem of broken symlinks during next Ubuntu upgrade.
Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65
0

I experienced the same issue and I managed to "fix" is by recreating the virtualenv and reinstalling the required packages using pip.

Create a new virtualenv:

virtualenv <new-virtualenv>

Activate it:

source <new-virtualenv>/bin/activate

Install the packages:

pip install <required-packages>

And I was good to go again!

You can keep the old virtualenv by just renaming the folder:

mv <old-virtualenv> <old-virtualenv>-backup
mpkossen
  • 81
  • 5
  • 1
    Is there any way of extracting pip package list from broken virtualenv? – Lord_JABA Apr 28 '16 at 19:01
  • @Lord_JABA this is the tool _I_ want. When a virtualenv created by an older Python can't even run pip, why can't we have a tool that runs on current Python that can introspect the older virtualenv a la `pip freeze`? That could then be wrapped in a script that automatically recreates the virtualenv with current Python. I'd like to see your comment here become a whole new SO question. I'd upvote it! – Nat Goodspeed Aug 29 '22 at 15:29
0

I fixed it by creating a new virtualenv and copying the python executable into the old broken virtualenv.