Pyton pyenv & virtualenv

List available pyenv versions
https://www.python.org/ftp/python/

pyenv install --list

virtualenv

pip install -U virtualenv --user
mkdir -vp ~/test
cd ~/test
virtualenv .venv
source .venv/bin/activate
pip install -U pip
pip install -U python-openstackclient

Pyenv and virtualenv

sudo apt install -y git build-essential libbz2-dev libreadline-dev libssl-dev zlib1g-dev libsqlite3-dev wget curl libncurses5-dev libncursesw5-dev xz-utils libffi-dev liblzma-dev
 
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
 
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init --path)"\n  eval "$(pyenv init -)"\n  eval "$(pyenv virtualenv-init -)"\nfi' >> ~/.bashrc
 
exec "$SHELL"  # or re-login
 
pyenv install 3.8.12
pyenv local 3.8.12
pyenv virtualenv 3.8.12 kolla-ansible-xena
pyenv activate kolla-ansible-xena
 
pyenv --version
pyenv virtualenvs
pyenv versions
 
pip install -U pip
pip install -U 'ansible<2.10'
pip install -r requirements.txt
# python setup.py install

Global pyenv configuration

# /etc/bash.bashrc
 
export PYENV_ROOT=/usr/local/pyenv
if command -v pyenv 1>/dev/null 2>&1; then
  export PATH="$PYENV_ROOT/bin:$PATH"
  eval "$(pyenv init - --no-rehash)"
  eval "$(pyenv virtualenv-init -)"
fi

Deploy pyenv with ansible role
https://github.com/staticdev/ansible-role-pyenv

- name: Install staticdev.pyenv role from Ansible Galaxy
  become: no
  local_action: command ansible-galaxy role install staticdev.pyenv
 
- name: Configure system pyenv
  include_role:
    name: staticdev.pyenv
  vars:
    pyenv_env: "system"
    pyenv_shellrc_file: "{% if pyenv_env == 'user' %}~/.bashrc{% else %}/etc/profile.d/pyenv.sh{% endif %}"
    pyenv_path: "{% if pyenv_env == 'user' %}{{ ansible_env.HOME }}/pyenv{% else %}/usr/local/pyenv{% endif %}"
    pyenv_owner: "root"
    pyenv_global:
      - osc
    pyenv_update_git_install: no
    pyenv_enable_autocompletion: yes
    pyenv_python_versions:
      - 2.7.18
      - 3.9.16      
    pyenv_virtualenvs:
      - venv_name: "python2"
        py_version: "2.7.18"
      - venv_name: "osc"
        py_version: "3.9.16"

Define multiple global default virtualenvs

# cat /usr/local/pyenv/version
python3
osc
kolla-ansible

Links
https://github.com/pyenv/pyenv
https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-global
https://github.com/pyenv/pyenv-virtualenv