Lab 1 Python Setup

The aim of this lab is to setup our python environment in the lab and give you the ability to setup something similar at home. I will video it live and upload afterwards as this will mainly be an interactive session.

We will cover:

  1. Installing a specific Python version globally and locally
  2. Creating a virtual environment
  3. Managing dependencies with pyproject.toml
  4. Running tests with pytest

Default Python Version

Why not use the default python version that comes with the OS?

RHEL comes shipped with python installed however they are quite old versions, in addition to this the system python is used by the OS and should not be changed.

They are also quite restricted in what you can install as a lot of the header files and core dependencies are not installed as they would clash with each other.

PyEnv

For the last few year I have been using pyenv to manage my python environments. This allows me to have multiple versions of python installed on my machine and switch between them easily. Which can be useful when working with different software packages that require different versions of python as well as testing for the different DCC python versions.

In the last year however a new tool called UV and I have now decided we should switch to this as it is more modern and has a few more features.

Installing UV

To install UV, run the following command in your terminal

curl -LsSf https://astral.sh/uv/install.sh | sh

Alternatively, use Homebrew on macOS:

brew install astral-sh/uv/uv

If using windows at home you can use

powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

This is going to install the uv command line tool into your home directory .local/bin/ uv.

Setting a Python Version

There are two ways to set a Python version with UV: globally and locally. I typically set one global version then per project I can set a local version if required. But what version to choose?

DCC Python version

In the labs at present we have the following version

ToolPython Versiontool name
Maya3.9.7mayapy
Houdini3.11.7hython
Nuke3.9.10python3
Renderman3.7.13rmanpy3

To add to the confusion Renderman also has import libraries for 3.7 3.9 3.10 and 3.11 (more on that in a later lab).

So which version to use? We are mainly using the vfx reference platform CY2023 because of out maya usage, so we should use python 3.9.7 as our global version mainly due to the version of PySide / PyQt we are using. However if we use the qtpy library we can use a slightly higher version of python for tools and revert to 3.9.7 for maya. Lets install 3.11 for now.

Installing and Managing Python Versions

With uv, you can install and manage Python versions efficiently. To install a specific Python version, run:

uv python install 3.11

To list installed versions:

uv python list

To switch between installed Python versions:

uv python pin 3.11

To confirm the active Python version:

uv run python -V
Python 3.11.11

Setting this as default python

Unlike pyenv uv does not have a global setting for python so we need to set this in our .bashrc file as an alias

alias python='uv run python'

source the .bashrc file and you should be able to run python.

adding a different python

If you want to add a different version of python you can do this with the following command

uv python install 3.10

now we can test this by running

mkdir p10
cd p10
python -V 
3.11.11
uv python pin 3.10
python -V
3.10.11

You will notice a file called .python-version in the directory which is used to store the version of python for that directory.

Virtual Environments

A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages. This allows you to have multiple projects on the same machine, each with its own dependencies.

The main issue with virtual environments is that they can be quite large and take up a lot of space on your hard drive. The advantage of using uv is that it uses a shared cache for all your virtual environments so you only have one copy of each package on your machine.

By default, uv uses global, shared environments for dependencies. This means:

  • Packages are not duplicated across virtual environments.
  • Different projects using the same package/version reference a single shared copy.
  • This significantly reduces disk space compared to traditional venvs.

A new project

The real power of uv is when you start a new project. You can create a new project with the following command

uv init PyProject 
cd PyProject
ls
hello.py  pyproject.toml  README.md

We can now run the hello.py file with the following command

uv run  hello.py
Hello, World!

Adding some tests

In the project we can add a directory called tests

mkdir tests
touch tests/test_add.py

In the test_add.py file we can add the following code

from hello import add

def test_add() :
    assert add(2,4) == 6

We can now use the uvx command to run the tests (this will install pytest if it is not already installed) we also need to add the following to the pyproject.toml file

dependencies = [
    "pytest"
    ]
uvx pytest

You will see the test fails as we have not written the add function yet.

def add(a,b) :
    return a+b

ruff

Ruff is a tool that can be used to check your code for PEP8 compliance. As well are formatting and other issues, this replaces various other tools such as flake8, black, isort etc.

uvx ruff check

On fist run this will install ruff and then run the check. We can also format the code with

uvx ruff format

Some useful commands

show where python is installed

uv python dir
/home/jmacey/.local/share/uv/python

show where any tools are installed

uv tool dir
/home/jmacey/.local/share/uv/tool

mayapy

We can setup an alias to mayapy in our .bashrc

alias mayapy='/opt/autodesk/maya/bin/mayapy'

In particular the maya symbolic link in /opt/autodesk always points to the latest version of maya installed. At present under linux we are using maya2020 which is still using python2.7.11 which is not ideal. Also beware of your userSetup.py file

Previous
Next