Different Python version in a virtual environment

Hello. The current version of Python in ClearLinux is 3.11.2. I recently wanted to test OpenAI Whisper, but it requires Python >3.7 & <3.11. Can somebody enlighten me on how to create a virtual environment with Python 3.10? Should I need to install that version of Python from source?

I found the solution by myself: installing the required Python version from sources. Thankfully it was very easy and I will share the steps with you all in case somebody experience the same need as I.

Install a different version of Python than the official ClearLinux version from sources:

# Changing to root
sudo su
cd /root

# Installing some libraries to build optionals but useful modules
swupd bundle-add devpkg-openssl devpkg-zlib devpkg-bzip2 devpkg-libffi devpkg-ncurses devpkg-sqlite-autoconf devpkg-readline

# Downloading Python
curl -O 'https://www.python.org/ftp/python/3.10.11/Python-3.10.11.tar.xz'
tar xJf Python-3.10.11.tar.xz
cd Python-3.10.11

# Building and installing Python
LDFLAGS=-Wl,-rpath=/opt/python-3.10.11/lib,--disable-new-dtags ./configure --prefix=/opt/python-3.10.11 --enable-shared --enable-optimizations --enable-ipv6
make -j8
make install

# Installing PIP for the recently installed Python version
curl -O https://bootstrap.pypa.io/get-pip.py
/opt/python-3.10.11/bin/python3 get-pip.py
/opt/python-3.10.11/bin/python3 -m pip install --upgrade pip

# Install VirtualEnv module so we can create new virtual environment with the recently installed Python version
/opt/python-3.10.11/bin/python3 -m pip install virtualenv

After we executed all the previous steps successfully, we will be ready for creating new virtual environments with the installed Python version.

mkdir projectX
cd projectX

/opt/python-3.10.11/bin/python3 -m virtualenv .venv
source .venv/bin/activate

# Exec, exec, exec
# e.g.: pip install --no-cache-dir git+https://github.com/openai/whisper.git
# You can test execute the previous command with the default ClearLinux Python version (3.11.X) and you will see it will fail because of the Python version.



1 Like

Hi @leiniercs,

Nice that you found a solution that works for you :).

I would say that you should also take a look at conda (Conda — conda documentation). I believe is as a more robust and standard solution to manage different python versions.

But if your solution is working properly, just keep it.

1 Like

Found an alternate approach to solve the problem!
pyenv: Simple Python version management

It uses the same approach that build python from source. That can be installed with

curl https://pyenv.run | bash

after that we can add pyenv to path with

{ echo 'export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
command -v pyenv >/dev/null && [ -n "$(type -t pyenv)" ] && [ "$(type -t pyenv)" = "function" ] ||  eval "$(pyenv init -)"' } >> ~/.bashrc


This program has lot of python version, and it is easy to use.

1 Like