Software – Python Virtual Environments
Python is available across all systems, The version of Python will be different depending on the system’s OS.
This enables you to build your python environment without root access, as well as making it much more portable across systems, plus it’s really simple to do.
Python Versions
System | Version | Notes |
Rocky 9 | Python 3.9 | Default |
Rocky 9 | Python 3.12 | /usr/bin/python3.12 |
Fedora 38 | Python 3.11 | Default |
Fedora 40 | Python 3.12 | Default |
Any | Any | BYOP (See below) |
Create Environment
Python 3
To create a Python3 virtual environment
python3 -m virtualenv <environment_name>
or if that doesn’t work
python3 -m venv <environment_name>
Use the Environment
Source the environment for your shell
source <environment_name>/bin/activate or source <environment_name>/bin/activate.csh or source <environment_name>/bin/activate.fish
then you can run pip/pip3 etc as you wish.
Upgrade the Environment
If the version of Python has updated on the system and is causing your envrionment to not load try this command in the shell
python3 -m venv --upgrade <environment_name>
Leave the Environment
deactivate
Known Issues with Virtual Environments
People with large 1GB+ environments especially when it’s lots of little files sometimes find that the activation can take a long time.
BYOP – Bring Your Own Python
Please note this compiles on the system you’re running so it may not be interoperable between operating systems.
If you want to use pyenv in its standard method:
curl -fsSL https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
pyenv update
pyenv install <VERSION>
pyenv global <VERSION>
To get a list of all supported version:
pyenv install -l
If you wish to store your custom python in alternative storage do this first (change for your storage location)
export PYENV_ROOT=/mt/user-batch/$USER/.pyenv
echo 'export PYENV_ROOT="/mt/user-batch/$USER/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
To remove a version
pyenv uninstall <version>
If you wish to do some weird python magic, you can modify the python interpreter via the shebang on the first line to specify a pyenv compiled version for example:
#!/mt/home/<username>/.pyenv/versions/<version>/bin/python
See more at PyEnv on Github.