3

So I got my Raspberry Pi with NOOBS and decided to use Raspbian. Then I wanted to test a python script I wrote with python 3, and it threw me an error, because I used an unknown import.

Then I checked the python version and found out I've got Python 2.7.3 installed. I've read about others who want to install python 3, but that it isn't suggested as default version, so I don't want to mess around with different python versions, since I'm new to python.
Then I looked for a 2.7.3 download for Windows, but it suggested me to use a higher 2.7 version, since those include bugfixes.

But why is Python 2.7.3 preinstalled (and doesn't get updated by apt-get update/upgrade)?
How do I upgrade to 2.7.9 (and newer 2.x versions, as soon as they are released), or why shouldn't I?

EDIT: Since Python 3 (3.2.3) is also preinstalled (as I now know), why doesn't it get updated, and how do I update it? (or why shouldn't I)

  • Try running it with python3. Both Python 2.73 and some version of 3.x are installed by default. As for the older version of the 2.3 branch. You will find that the version included with the distro can sometimes be quite far behind the bleeding edge (it takes some time for the package to go from maintainer to distribution). Though in this case it is only a few bug revisions behind. I would not worry about the difference in this case unless I encountered one of the bugs (in production) addressed by the update. One advantage of this is that updates will still be handled by the system (apt-get). – Steve Robillard Dec 26 '14 at 1:07
  • My problem isn't that the script doesn't run, that's just background information. My Problem is, that I can't update to higher 2.7.x versions. But thanks for that, since it solves the initial problem. Sill my other questions are standing there... – XoMEX Dec 26 '14 at 1:12
  • To get the bleeding edge package you will need to either find a precompiled binary or build from source.This will mean that future upgrades etc. will need to be handled by you as a manual process. As I mentioned the value here is not likely worth the effort. The distros version while a few bug versions behind is tested to meet the requirements of the other packages on your Pi. The same cannot be said for a non distro package. This testing is one of the reasons that the distro package is behind the bleeding edge version. The new versions add no new features just bug fixes ref: semver.org – Steve Robillard Dec 26 '14 at 1:23
  • 1
    Raspbian is based in debian which is a very conservative Linux distribution. This is generally a good thing, but if you must have the latest there are other distributions available. – Milliways Dec 26 '14 at 3:49
5

Steve is wrong when he says that 2.7.9 includes no new features; it is a very unusual bug-fix release in that it does. Mainly the added features have to do with enhanced network security, but 2.7.9 is also the first Python 2 release to include SNI, which is required for accessing some websites such as Google Docs.

At the moment I think you have to download the source and compile and install it yourself. Here's what I did: first, download a source distribution tarball; I got mine from https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz. Place it in a directory you want to work in (I used ~/src) and be in that directory. Then:

gunzip Python-2.7.9.tgz
tar -xvf Python-2.7.9.tar
cd Python-2.7.9/
./configure
make
sudo make install

The configure and make steps will take some time; the whole process lasted about 26 minutes for me on a Pi 2B. You can do "python -V" afterwards to check that you are getting the right version.

  • A couple fine points. Python 3 has always had SNI support, so if you're considering 2.7.9 because of the openssl features, you could also consider 3. (I had legacy 2 code so 3 wasn't an option.) Also, on my system, the sudo make install step put Python 2.7.9 in /usr/local/bin/python, but left /usr/bin/python as 2.7.5. If you have a Python script that starts with #!/usr/bin/python, it won't be getting the new version unless you change it. – Howard A. Landman Feb 1 '16 at 6:41
1

Two options:

  1. You will be fine with 2.7.3; no, really you will. If there are security issues, Debian often applies patches so that older versions run safely. I'd caution against upgrading the system python interpreter, as 2.7.3 might be required for system stability. The other advantage of sticking with the standard Raspbian interpreter is that you know you can easily share that cool python thing you wrote with all the other users. If you upgrade, not so much.

  2. If you want to run a newer python environment, install virtualenv. This leaves your system python alone, but allows you to go off and do your own thing with whatever version of python you wish to run.

  • +1 for virtualenv. Since python3 is also installed, there's really no reason to change the system version. – Jacobm001 Feb 3 '15 at 23:02
0

I think you'll find answers here

http://achinghead.com/installing-multiple-versions-python.html

or in french http://www.rasadacrea.com/fr/cours-informatiques/comment-installer-python

in my case I used "sudo make install" instead of altinstall because I upgraded from 2.7.3 to 2.7.9 (a minor upgrade) but if you are not sure use "altinstall"

0

Updating Python is not very easy and can break some of your system functionality (and that's why it doesn't updates on itself), so you should use pyenv (or virtualenv) - that is like "sandbox" for Python, in which you can install any version of Python without breaking something. If you just want to launch your script with Python3, just launch your script with python3 (don't forget to get to script directory in terminal):

$ python3 yourscript.py
  • lol, didn't see the date... :P – Kerbiter Aug 24 '16 at 15:58

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.