Installation of Python

Vicente González Ruiz - Depto Informática - UAL

October 12, 2023

Contents

 1 Steps
 2 Resources

Most of the current Unix-based operating systems (Linux, FreeBSD and OSX) use Python for running some of their “daily tasks”, which means that a Python interpreter is already available. However, usually it is better to use our own interpreter because:

  1. We can chose the version of Python and have more flexibility with the packages.
  2. We can optimize the compilation of the interpreter depending on our needs (for example, including Tk support or not).
  3. By default, all the Python packages will be installed in a different repository of the system packages, which eases the system/user Python-isolation and the removal of the interpreter.

In Windows you need to install Python, yes or yes, from the official website. However, notice that this “guide” only contemplates the installation of Python in Unix-based OS machines, and specifically, in a Xubuntu computer.

As said before, it is very likely that your Unix-like OS has Python installed. However, in order to control the version of the Python interpreter, we will install a dedicated one.

1 Steps

  1. Go to YAPT/01-hello_world/02-installation.ipynb [1] and follow the instructions to install the lastest stable version CPython, and create a new virtual environment called tm with pyenv. Basically (example for Python 3.9.7):

    1. Install package dependencies for compiling Python:

           sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
    2. Download Pyenv:

           git clone https://github.com/pyenv/pyenv.git ~/.pyenv
    3. (Optional) Compile a dynamic Bash extension to speed up Pyenv.

           cd ~/.pyenv && src/configure && make -C src; cd ~
    4. Define the PYENV_ROOT environment variable to point to the path where you cloned the Pyenv repo. For this, put these lines into ~/.profile1 before the part that sources ~/.bashrc2.

           export PYENV_ROOT="$HOME/.pyenv" 
           export PATH="$PYENV_ROOT/bin:$PATH"
    5. Run:

           echo 'eval "$(pyenv init -)"' >> ~/.bashrc 
           echo 'eval "$(pyenv init --path)"' >> ~/.bashrc

      to put these lines at the bottom of ~/.bashrc to enable autocompletion and all subcommands.

    6. Restart your login session for the changes to take effect. E.g. if you’re in a GUI session, you need to fully log out and log back in.
    7. List the available Python interpreters:

           pyenv install --list
    8. Install the Python interpreter (and some basic tools such as pip):

           pyenv install -v 3.9.7
    9. Check what it is currently available:

           pyenv versions

      You should get something such as:

           * system (set by /home/<your_home_dir_here>/.pyenv/version) 
           3.9.7
    10. Select the Python interperter:

           pyenv global 3.9.7
  2. Install an IDE for programming with Python. I recommend Thonny if you are not used to any other.

       pip install thonny

2 Resources

[1]   V. González Ruiz. YAPT.

1This configuration file is sourced by the bash interperter only when a login shell is done (when the system requests your identity and you are able to privide it.

2This configuration file is sourced by the bash interpreter when you request a non-login shell, i.e., when you reinstance the interpreter, for example, running a new terminal.