9

If I double click on a .py file it opens "IDLE" (is there another way to open "IDLE"?), but the default python version is 2 when I select "run". I want to PERMANENTLY change the default version to 3 so that I don't have to switch versions every time I switch on my Raspberry Pi.

I have searched for this without success. Most answers predate the inclusion of Python 3 and explain how to install 3 but NOT how to switch the default, hence this question.

Greenonline
  • 2,740
  • 4
  • 23
  • 36
John Foggitt
  • 91
  • 1
  • 1
  • 3
  • Did you try uninstalling python 2? – Bex Feb 19 '16 at 07:19
  • 7
    @Bex, I would not uninstall py2 unless you're certain that it is not needed elsewhere. There are still many scripts out there that run on 2.7. – Ghanima Feb 19 '16 at 07:41

2 Answers2

5

IDLE is usually installed along with python, so you should have idle3 somewhere. Here's what you should do:

  1. Try to run idle3, idle3.3, idle3.4 etc. in the terminal to make sure you have it. Let's assume you have idle3

  2. Change the file association of .py files, so they are opened with idle3 when double-clicked. This depends on your file manager, usually righ-click -> "Open With..." does the trick.

Again, playing with symlinks is not a good idea, because scripts starting with #!/usr/bin/python expect to be executed with python2.

Dmitry Grigoryev
  • 27,928
  • 6
  • 53
  • 144
-2

a proper way to do it is to use Debain's update-alternatives

UPD: people are too lazy to jyst Read The Fine Manual :

update-alternatives --remove-all python
update-alternatives --install /usr/bin/python python /usr/python2/bin/python2 10
update-alternatives --install /usr/bin/python python /usr/python3/bin/python3 20

That's the way it should be. After that to switch the things up :

update-alternatives --set python "/usr/python2/bin/python2"

to select the one you need. It won't break PEP-394, because python2 and python3 binaries will be where they must be in multi-versioned install : in the installations' prefixes(usr/python2 and /usr/python3 respectively)

Alexey Vesnin
  • 926
  • 9
  • 16
  • 3
    There is no link group for Python in update-alternatives. – n.st Feb 19 '16 at 16:25
  • 1
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – Steve Robillard Feb 19 '16 at 18:34
  • 1
    update-alternatives are just a nice way to track symlinks in your system, so in the end your solution will hit the same problem as @Milliways': you'll replace /usr/bin/python, which is a violation of PEP-394. – Dmitry Grigoryev Feb 19 '16 at 19:19
  • 1
    @n.st So you need to create it =) That's the idea : no package mess-up, standard switching mode – Alexey Vesnin Feb 19 '16 at 19:24
  • I don't know the internals of update-alternatives all that well, but wouldn't creating a custom link group mess up with packages which are unaware of it? When you update python2, wouldn't this package overwrite the symlink update-alternatives created? – Dmitry Grigoryev Feb 19 '16 at 19:34
  • @DmitryGrigoryev if you don't need to maintain multiple versions of gen-2 and gen-3 inside a branch, for example you need v2.7 and v3.0, not v2.6, v2.7 and v3.0,v3.1 - you don't need to touch a python2 and python3 links AT ALL : just add the corresponding dirs in your PATH and delete a python symlink in the xxx/bin folders – Alexey Vesnin Feb 19 '16 at 19:37
  • 1
    PEP-394 specifically says that /usr/bin/python should point to python2, because many legacy scripts starting with #!/usr/bin/python only run correctly with python2. – Dmitry Grigoryev Feb 19 '16 at 19:39
  • @DmitryGrigoryev legacy scripts are DOOMED OUT : they're not working with a modern libraries/components. It should not bother you unless you're a musemum sysadmin =) – Alexey Vesnin Feb 19 '16 at 19:41
  • OK, point taken. BTW, thanks for expanding your answer, I'm happy to upvote it now even if I don't totally agree. – Dmitry Grigoryev Feb 19 '16 at 19:44