3

I have a confusing situation with my python installation on ubuntu.

$ python --version
Python 3.4.0
$ python2 --version
Python 3.4.0
$ python3 --version
Python 3.4.0
$ ls -la /usr/bin/python2
lrwxrwxrwx 1 root root 9 Dez 21  2013 /usr/bin/python2 -> python2.7
$ ls -la /usr/bin/python3
lrwxrwxrwx 1 root root 9 Mär 23  2014 /usr/bin/python3 -> python3.4

All I wanted was to have python3.4 executed under the command "python". So I copies /usr/bin/python3 to /usr/bin/python, because python --version returned 2.7 and now it returns 3.4. I don't know if this was a mistake.

I tried to install a package with pip and that failed. Then I tried to reinstall python (sudo apt-get install --reinstall python) and the terminal gave me the hint to run "apt-get -f install" without package name.

$ sudo apt-get -f install
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
8 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up python2.7 (2.7.6-8) ...
  File "/usr/lib/python2.7/py_compile.py", line 114
    except Exception,err:
                    ^
SyntaxError: invalid syntax
dpkg: error processing package python2.7 (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of python:
 python depends on python2.7 (>= 2.7.5-1~); however:
  Package python2.7 is not configured yet.

dpkg: error processing package python (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-dateutil:
 python-dateutil depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-dateutil depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-dateutil depends on python:any (>= 2.7.1-0ubuntu2); however:
  Package python is not configured yet.

dpkg: error processing package python-dateutil (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-gst-1.0:
 python-gst-1.0 depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-gst-1.0 depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-gst-1.0 depends oNo apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                                                   No apport report written because the error message indicates its a followup error from a previous failure.
                                              No apport report written because MaxReports is reached already
                                                                                                            No apport report written because MaxReports is reached already
                                                                                                                                                                          No apport report written because MaxReports is reached already
                                         No apport report written because MaxReports is reached already
                                                                                                       No apport report written because MaxReports is reached already
                                                                                                                                                                     n python:any (>= 2.7.1-0ubuntu2); however:
  Package python is not configured yet.

dpkg: error processing package python-gst-1.0 (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-pyparsing:
 python-pyparsing depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-pyparsing depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-pyparsing depends on python:any (>= 2.7.1-0ubuntu2); however:
  Package python is not configured yet.

dpkg: error processing package python-pyparsing (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-tz:
 python-tz depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-tz depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-tz depends on python:any (>= 2.7.1-0ubuntu2); however:
  Package python is not configured yet.

dpkg: error processing package python-tz (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-support:
 python-support depends on python (>= 2.5); however:
  Package python is not configured yet.

dpkg: error processing package python-support (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of python-matplotlib:
 python-matplotlib depends on python-dateutil; however:
  Package python-dateutil is not configured yet.
 python-matplotlib depends on python-pyparsing; however:
  Package python-pyparsing is not configured yet.
 python-matplotlib depends on python-tz; however:
  Package python-tz is not configured yet.
 python-matplotlib depends on python (<< 2.8); however:
  Package python is not configured yet.
 python-matplotlib depends on python (>= 2.7); however:
  Package python is not configured yet.
 python-matplotlib depends on python-support (>= 0.90.0); however:
  Package python-support is not configured yet.

dpkg: error processing package python-matplotlib (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 python2.7
 python
 python-dateutil
 python-gst-1.0
 python-pyparsing
 python-tz
 python-support
 python-matplotlib
E: Sub-process /usr/bin/dpkg returned an error code (1)
$ 

I already tried to change the links back:

$ sudo cp /usr/bin/python2.7 /usr/bin/python
$ python --version
Python 3.4.0
$ sudo cp /usr/bin/python2 /usr/bin/python
$ python --version
Python 3.4.0
$

What am I supposed to do to get python2 and python3? How can I reinstall python using apt-get?

  • It used to be that if you changed python links, that it was a total reinstall as even Internet required correct version of python. Now you should just change link back the same way you changed it in the first place. Just use python2 version to be link to python. Ubuntu has been planning on converting fully to python3, but not just yet. – oldfred May 27 '15 at 13:51
  • I updated my question. After your answer I don't dare to reboot... – Sadik May 27 '15 at 13:59
  • 2
    The python link has to be python2. And to run python3 you have to just specify python3. And you can change all python2 only code to use python2, So default python would not normally be used, but lots of older not yet updated code still needs python to be python2 link. When conversion to python3 occurs, you may have similar issues running legacy python2 code unless it is updated to refer specifically to python2 link not python link. – oldfred May 27 '15 at 14:03
  • I see. But it seems that I don't have python2 anymore. Look at the version! python2.7 --version gives me 3.4 and I can't reinstall it. – Sadik May 27 '15 at 14:11
4

Pythons 2 and 3 live very happily next to each other on most Ubuntu installations. What you're describing (python3 mapping to a Python 2 binary) is not normal at all.

python needs to be mapped through to Python 2 by default. There are various scripts that aren't Python 3 compliant (it's not backwards compatible) so if you break the mapping from python, you break the system.

You're already seeing that from packages' postinst scripts that expect Python 2.

Here's how my python is mapped (a 14.04 install):

$ readlink -f $(which python)
/usr/bin/python2.7

So let's just remap /usr/bin/python back there:

sudo rm /usr/bin/python
sudo ln -s /usr/bin/python{2.7,}

Then run your sudo apt-get -f install which should now be able to run without error.


If you've knackered things so badly, you might find yourself in the position where you need to manually unpack the Python packages into your system (they're just zips with header data).

If you've just munched /usr/bin/python2.7 (which should be the Python 2 binary) you can replace this by downloading the python2.7-minimal package and extracting the binary to the right place:

apt-get download python2.7-minimal
ar x python2.7-minimal_*.deb data.tar.xz
sudo tar xJf data.tar.xz -C / './usr/bin/python2.7'
rm data.tar.xz

That's based on the current 14.04 package. If you're on another release you might have to tweak paths. Or copy from a live system running the same version of Ubuntu.


More broadly speaking, if you want a Python development environment, I seriously suggest you look at the venv module. This is as with Py2's VirtualEnv except that it's built-in. You can install whatever you like in a non-root, non-system way and that includes mapping python to the Python binary you pick (including Pypy).

On 14.04 there's a bug in Python 3 that needs fixing before you can create a venv, but it that can be worked-around quite easily at the moment. We just need to install ensurepip:

wget -qO- http://d.pr/f/YqS5+ \
| sudo tar xzf - -C $(python3 -c "import sys; print(sys.path[1])") --no-same-owner

Then create and activate the venv:

python3 -m venv myvenv
source ./myvenv/bin/activate

Now you're in your own playground. You'll need to call the activate in the future (or call the myvenv/bin/python binary explicitly) to load the right Python path.

  • $ readlink -f $(which python) /usr/bin/python2.7 $ python --version Python 3.4.0 $ apt-get -f install still doesn't work. – Sadik May 27 '15 at 14:15
  • @Sadik Then you've done something to the /usr/bin/python2.7 file that you haven't advertised in your question. Download the python2.7-minimal package for your distribution and extract out a fresh copy over your broken version. – Oli May 27 '15 at 14:19
  • If I only knew what I've done to python2.7 I would have mentioned it. Ok, I'll try... – Sadik May 27 '15 at 14:19
  • I downloaded python2.7 and python2.7-minimal and installed them with dpkg -i *.deb. That gave me an error, however after that apt-get install -f worked! Thanks. – Sadik May 27 '15 at 14:28
  • Heh, just been working on a few commands that could extract the binary for you. I've added them for future reference. – Oli May 27 '15 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.