pyenv let you manage multiple versions of Python easily. It's simple, unobtrusive, and follows
the UNIX tradition of single-purpose tools do one thing well.
pyenv-virtualenv is a pyenv plugin that
provides features to manage Python virtual environments for Python on UNIX-like systems.
As pyenv installs Python by building from source, which means it needs OS-specific dependencies. Before installing pyenv, you will need to install its build dependencies first.
For Ubuntu/Debian,
$ sudo apt-get install -y build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev \
liblzma-dev python3-openssl makeFor Fedora/CentOS/RHEL,
$ sudo yum install gcc zlib-devel bzip2 bzip2-devel readline-devel \
sqlite sqlite-devel openssl-devel xz xz-devel libffi-develFor openSUSE,
$ zypper in zlib-devel bzip2 libbz2-devel libffi-devel \
libopenssl-devel readline-devel sqlite3 sqlite3-devel xz xz-develFor MacOS,
$ brew install openssl readline sqlite3 xz zlibFor Linux/Unix, use pyenv-installer to install pyenv,
$ curl https://pyenv.run | bashIt would install pyenv application, as well as several other plugins, including pyenv-virtualenv.
For MacOS, you can run this command,
$ brew install pyenv pyenv-virtualenvAfter the installation, then need to config .bashrc, bash_profile, or .zshrc file,
depending on what OS system and shell script you are using.
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"Next to source the config file, or restart your shell environment to make the configuration taking effect.
To validate the installtion,
$ pyenv --version$ pyenv virtualenv --versionMore information about the installation? Please refer to the documentation
Let's run the following command to create a virtual environment,
$ pyenv virtualenv 3.9.9 testenv
pyenv-virtualenv: `3.9.9' is not installed in pyenv.
Run `pyenv install 3.9.9' to install itApparently, it does not run successfully, because I don't have Python 3.9.0 available on my machine as base to create such a virtual environment.
What should we do? As shown above, we need to use pyenv to install according Python version first, then to create the virtualenv.
Use the following command to check available Python versions for installation:
$ pyenv install --listThen install a specific version like this:
$ pyenv install 3.9.9$ pyenv install 3.12.0As pyenv builds Python from the source, it may take a couple of minutes to install.
Where the Python version is installed?
$ ls ~/.pyenv/versions/
3.12.0 3.9.9To uninstall a specific Python version, run this command:
$ pyenv uninstall 3.9.9Therefore, pyenv could help you manage multiple Python versions on the machine easily. Based on the available Python versions, you can create desired Python virtual environment easily as well.
Use the following command to create a virtualenv,
$ pyenv virtualenv 3.12.0 testenvWhere this testenv virtual environment installed?
$ ls ~/.pyenv/versions
3.12.0 testenvWhat's inside of testenv directory?
testenv
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── Activate.ps1
│ ├── pip
│ ├── pip3
│ ├── pip3.12
│ ├── pydoc
│ ├── python -> python3.12
│ ├── python3 -> python3.12
│ ├── python3.12 -> ~/.pyenv/versions/3.12.0/bin/python3.12
│ ├── python3.12-config -> ~/.pyenv/versions/3.12.0/bin/python3.12-config
│ ├── python3-config -> ~/.pyenv/versions/3.12.0/bin/python3-config
│ └── python-config -> ~/.pyenv/versions/3.12.0/bin/python-config
├── include
│ └── python3.12
├── lib
│ └── python3.12
├── lib64 -> lib
└── pyvenv.cfg
6 directories, 15 filesUse this command to list your available virtualenvs,
$ pyenv virtualenvsTo activate the virtual environment, use this command:
$ pyenv activate testenvOr you can use the following command,
$ pyenv local testenvTo install or uninstall a single package within virtual environment, run
$ pip install <package-name>To install a list of Python packages in requirements.txt, run
$ pip install -r requirements.txtTo uninstall the Python package,
$ pip uninstall <package-name>To deactivate the virtual environment,
$ pyenv deactivateIf you use pyenv local activate your virtual environment, then
$ pyenv local --unsetTo remove the virtual environment,
$ pyenv virtualenv-delete testenvMore information about these packages
pyenv- https://github.com/pyenv/pyenvpyenv-virtualenv- https://github.com/pyenv/pyenv-virtualenv
Happy Coding!