4

On a fresh install of Raspbian Jessie+PIXEL (1/11/17 release), I installed from the source Python 3.6.0.

I downloaded the tgz file from Python.org and built from source. I used a couple of sources to research install to ensure I didn't do anything abnormal or wrong in process and they match with what is answered in the Raspberry Pi SE regarding install of Python 3.6.0 (the only thing I did different was using sudo -s as a separate command in the terminal before any other command.)

When downloading, the tgz file defaults to the /home/pi/Downloads folder and extracting creates the folder /Python-3.6.0 inside of that. After running the commands for install I can see the python executable which will open LX-Terminal and state use of python 3.6

When I browse other packaged versions of Python (2.7, 3, 3.4), each of the have an IDLE folder with the associated IDLE IDE executable but the Python 3.6 folder after install does not have an IDLE folder nor does Python 3.6 have a separate folder in /usr/lib as the other versions. Additionally, if I enter python on the command line, it starts Python 2.7. If I run IDLE (3), it starts up IDLE for Python 3.4.

Update:

I blew away the SD card and reapplied the Raspbian image and went through the steps again, making certain I created a directory in path.

I can set the terminal to run Python 3.6 if I start the terminal app from the desktop but it still defaults to 2.7 from command line, and IDLE still defaults to 3.4.2.

How do I get Raspbian to use Python 3.6 as the default version from the terminal and when starting IDLE 3?

Shawn Gordon
  • 143
  • 1
  • 8
  • It's likely not appearing because it's being installed in /usr/local/bin, and the system Python is in /usr/bin, ahead of it in your PATH statement. You risk making your system hard to administer by altering the system Python interpreter. Use something like pyenv instead. – scruss Jan 14 '17 at 23:58
  • So would then I blow it away (nothing on it) and use pyenv? Or does pyenv let me keep what is there and just make global settings, further controlling it from there. I wasnt clear on that at the main PyEnv site – Shawn Gordon Jan 15 '17 at 00:35
  • don't blow away the system Python interpreter, as it will take unspecified bits of your system with it. pyenv should be able to allow you to run 3.6 for your local stuff while keeping whatever the system has as its version of Python. This is a good thing. You also need to keep Python 2.7 around as 'python' for system stability – scruss Jan 15 '17 at 00:49
  • OK. I'll give that a shot. Thanks for the advice. I'll let you know if that worked. – Shawn Gordon Jan 15 '17 at 00:58
  • Solved. When I stopped over-thinking pyenv, it was wonderful. Thank you – Shawn Gordon Jan 15 '17 at 19:56

1 Answers1

1

System Default

As you may be aware, there are many directories in which Linux places binaries, or executable files. These are the commands executed on the terminal.

One of the common directories is /usr/bin. You can learn which binary will be run when you type in a command by using which:

$ which python
/usr/bin/python

You'll find that many applications install binaries to this, or other directories. You may also notice that Python installs here, but the binaries have the version as a suffix:

$ ls /usr/bin | grep python
python
python2.7
python3.7

Ah, then what is python?

0 lrwxrwxrwx 1 root toot 7 Oct 12 12:13 /usr/bin/python -> python3

It's a symbolic link, pointing to whichever version of Python is the default version for the system. You could play with this manually, but on Debian systems we can take advantage of update-alternatives, and using the right tools will prevent future issues.

$ update-alternatives --config python

You will be asked to select from the available versions. If you start to compile binaries of your own, or have another situation where you want to add your own alternatives, you can do that using update-alternatives as well.

Python Default

Python scripts, and scripts of many other languages are designed with a shebang at the top of the file, which is used to determine which program is used to load the script. You're probably familiar with the bash shebang, #!/bin/bash.

For Python, the 'default' or common example is #!/usr/bin/env python. You can adjust this to your needs: #!/usr/bin/env python3. This directly maps to the names of the files of the python binaries. You can choose python, python3, or python3.7 and get as specific or ambiguous as you wish, though you'll probably want to specify only the major version, so python3 or python2.

This is used when the file is executable (+x) and you run the file as an executable:

$ ./path/to/script

Whereas you can ignore the shebang, or specify which program runs the script:

$ python ./path/to/script
$ python3 ./path/to/script
$ python3.7 ./path/to/script

This means that you can either take advantage of specifying the Python version at the top of each file, or when calling the file.


Making a choice

Normally, update-alternatives is the ideal solution for having binaries of the same package name at different versions. With Python, though, I'd recommend keeping the defaults which comes with the distribution, and using the correct Python version in the shebang/environment loaded with each Python script. This will help prevent issues with system upgrades, pip2 and pip3, etc, and will also help keep your upgrade path clear so that you can convert your Python 2 scripts into Python 3 over time.

If you're not familiar, take a look at virtualenv and how you can isolate Python and any dependencies you install even further and in a way that is easier to replicate on different systems.

Consider Sublime Text 3 and ipython for a lot of freedom and flexibility.

earthmeLon
  • 1,404
  • 1
  • 11
  • 23