Installing Python in the Labs

TL;DR

  1. Open a terminal
  2. type install_python.sh
  3. wait!

Can’t be bothered to read the rest?

Why do I need to install python?

The version of Python installed on the lab machines is fixed by the operating system (RHEL) it is used for many of the system tools and it needs to be locked down to stop things breaking. This means that the version of Python is often out of date and does not have the latest features. To get around this we can install our own version of Python in our home directory.

It is possible to do this manually, but as this is a common task in programming, many tools have been developed to manage this process. We will use a tool called pyenv to manage our Python installations and will install it using the pyenv-installer as this is the simplest method.

install_python.sh

The install script is a simple shell script that will install pyenv and the latest version of Python. It will also add the necessary lines to your .bash_profile file to make sure that the new version of Python is used when you open a terminal.

I default install python 3.9.7 which is the same version that maya 2023 uses. If you need a different version you can install it using the pyenv commands as shown in this lab post.

If you want to install python for machine learning, I recommend installing the latest version of anaconda by running

pyenv install anaconda3-2024.06-1

This will take a long time to install as it is a large package, you can then install pytorch or tensor flow as shown in this lab post.

How the script works

The script is a simple shell script that will install pyenv and the latest version of Python. It will also add the necessary lines to your .bash_profile file to make sure that the new version of Python is used when you open a terminal.

The full script is located in /public/bin/2024 and is shown below, read the comments to understand the logic / processes used.

#!/usr/bin/bash

# Check if the --reset option is provided
if [[ "$1" == "--reset" ]]; then
    echo "Resetting... this may take some time to remove the old install"
    rm -rf ~/.pyenv 
fi

# check to see if pyenv is installed if it is exit
# best way if things go wrong is a complete wipe of the
# sandbox and re-install it shouldn't break anything else
if [ -d "$HOME/.pyenv" ] ; then
    echo "python is already installed, if you have issued please run"
    echo "install_python.sh --reset"
    exit 1
fi
# pyenv installer is the easiest method of installing so using that
echo "downloading and installing pyenv this may take some time"
curl https://pyenv.run | bash &>/dev/null

# need to append this to the bash profile so when you login you get the python
# from pyenv. put in .bash_profile so it should work via shh and normal term
echo "updating .bash_profile"
touch ~/.bash_profile # incase it doesn't exist
# now see if we alread have the commands in the file, if not add
if ! grep -q 'pyenv init ' ~/.bash_profile; then
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >>~/.bash_profile
    echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
    echo 'export PIP_DISABLE_PIP_VERSION_CHECK=1' >> ~/.bash_profile
fi
# setup the env vars for this installer now so I can do the rest of the install
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
# at present using 3.9.7 as this is the same version as maya2023
PYTHON_VERSION="3.9.7"
pyenv install $PYTHON_VERSION
# set this as the global python
pyenv global $PYTHON_VERSION
# install nccapy which has pillow and pygame which is good for most 1st year
# teaching also disable PIP warning messages as we don't want to upgrade (this way madness lies)
export PIP_DISABLE_PIP_VERSION_CHECK=1
pip install nccapy
echo "python is now ready use, please open a new terminal to start, you don't
need to run this installer again"
Next
Previous

Related