Pyenv
Pyenv is a simple Python version management tool that allows users to install multiple Python versions and keeps track of all instances.
Pyenv intercepts Python commands using shim executables introduced to your path and redirects your command to the correct version of Python. An added benefit is that Pyenv does not depend on Python itself, as it is made up of shell scripts.
Pyenv vs. Python Management Tools
Pyenv is a tool for managing multiple versions of Python on a single machine. However, it is often mistaken for other Python packages that offer similar functionalities, such as installing Python versions and creating and managing virtual environments.
This section will describe some of these essential tools and their main features. This overview will help you understand the differences between these tools and decide which tool to use based on your specific needs. Some additional tools we will cover include pip, virtualenv, pipenv, and venv.
Pip
The “pip” package manager enables users to easily fetch, install, upgrade, remove packages, and manage dependencies. It integrates with the Python Package Index (PyPI) and provides a way to connect to other package repositories. Pip will install packages into the active Python environment. To install a package, use the following command:
pip install <package_name>
Replace “<package_name>” with the package name you want to install. You can find more information about the pip package manager in this article.
Virtualenv
The “virtualenv” package (for Python 2) enables users to create isolated virtual environments, allowing “pip” to install packages that are not shared with other “virtualenv” environments.
Pipenv
The “pipenv” package aims to solve some common problems associated with “Pipfile,” “pip,” and “virtualenv.” It facilitates the management of virtual environments by allowing users to install and uninstall packages from the Pipfile. The way to install “pipenv” on most platforms is by using the command:
pip install --user pipenv
Venv
The “venv” package (It is included in Python 3.3 or newer) enables users to create a virtual environment on top of an existing Python installation, known as the virtual environment’s “base” Python. Although there are a few minor distinctions between “venv” and “virtualenv,” they operate similarly. To create a virtual environment on windows, open the command prompt and choose a folder.
PS C:\Users\LuisQ\Desktop>
Create a new virtual environment.
PS C:\Users\LuisQ\Desktop> python -m venv <Enviroment_Name>;
Replace “<Enviroment_Name>” with the name of the environment you want to define. To activate this new environment:
PS C:\Users\LuisQ\Desktop> .\<Enviroment_Name>\Scripts\activate
Then, the command prompt will show itself as:
(<Enviroment_Name>) PS C:\Users\LuisQ\Desktop>
Note: To avoid errors during writing scripts, you need first to run the following command (run as administrator):
Set-ExecutionPolicy Unrestricted -Force
If you are a Conda user, please refer to this article for more information about virtual environments.
Pyenv
The “pyenv” package manager enables users to install, use, and isolate specific Python versions (e.g., 3.8.6 and 3.9.0) and their dependencies. In other words, you can install multiple versions of Python on the same system and easily switch between them. To do this, “pyenv” makes use of environment variables to provide a Python version to execute a Python file in a specific directory. Go to the next section to see the installation procedure.
To Recap: What is the Difference Between Pyenv and Venv?
The “venv” package is included in the Python standard library. It serves to isolate your project’s dependencies into virtual environments. In contrast, the “pyenv” package is a tool that allows you to easily install and use Python versions that you want to have on your system.
For example, users can use “pyenv” to install interpreters like Miniconda, Anaconda, pypy, and various Python versions to test their code; In contrast, “Venv” is recommended to create multiple virtual environments working with multiple projects or Python applications.
Quick Installation Guide
Use the command “pyenv” to install this package. For Mac users, install first homebrew and run:
brew update brew install pyenv
Once the installation process has finished, you can verify it using the command:
pyenv --version
In addition, create an environment variable in the “.zshrc file” by adding the following lines:
#Pyenv export PYENV_ROOT=”$HOME/.pyenv”; export PATH=”PYENV_ROOT/bin:$PATH”; eval”$(pyenv init -)”
To verify the current Python version installed on your system:
python --version
And to visualize all the versions installed:
pyenv versions
Pyenv Fundamentals
Installing Python Versions
Pyenv makes the process of installing multiple Python versions easier. Use the command “list” to see all Python versions available in your system.
pyenv install --list
Now, to install Python versions 3.8.6 and 3.9.0, use the following lines one at a time:
pyenv install 3.8.6 pyenv install 3.9.0
Once finalized, you can use the “pyenv versions” command to verify them.
Configuring Directories
When you have multiple Python versions installed on your system, you can select and define one of them as the default version in the OS.
pyenv global <Python_version>
Replace “<Python_version>” with the previous version installed.
You can also define a Python version to work in a specific directory. For our example above, consider the following directories containing “different” projects requiring Python 3.8.6 and 3.9.0, respectively.
~/workspace/Python_project1 ~/workspace/Python_project2
For that, you must access the first folder and type:
pyenv local 3.8.6
And, for the second directory:
pyenv local 3.9.0
Finally, you can verify the Python version in each folder using the “python –version” command.
Highlights
Project Background
- Project: Pyenv
- Author: Joao Moreira
- Initial Release: 2017
- Type: Python Interpretation System
- License: MIT
- Contains: Directory of shims
- Language: Roff, Shell, Python
- GitHub: /pyenv/pyenv
- Runs On: Linux, MacOS
- Twitter: –
Main Features
- It enables the configuration of Python as a global setting for the entire system and a local setting for specific directories.
- It allows users to work with projects that support different Python versions.
- It provides a long list of multiple Python versions that could be installed.
Prior Knowledge Requirements
- Users should understand how to navigate the file system and run commands from the terminal or command prompt.
- Users should have a basic understanding of Python.
- Users should know how to create and manage virtual environments and how they are used in Python.
Community Benchmarks
- 30,700 Stars
- 2,700 Forks
- 370+ Code contributors
- 68+ releases
- Source: GitHub
Releases
- v2.3.13 (2-2023): Update and Fixes: e.g., Show symlink contents in non-bare `pyenv versions’.
- v2.3.12 (1-2023): Update and Fixes: e.g., Fix wrong libpython being linked to in MacOS.
- v2.3.11 (1-15-2023): Update and Fixes: e.g., Add a script to add the latest miniforge and mambaforge versions.
- v2.3.10 (1-10-2023): Update and Fixes: e.g., Remove stray newline after python-build installation.
- v2.3.9 (12-19-2022): Update and Fixes: e.g., Add -latest suffix to miniforge3.
- V2.3.8 (12-8-2022): Update and Fixes: e.g., Export detected shell environment in pyenv-init.
- Source: Releases.
References
[1] GitHub.
GIOELE: Installing Python packages in 2019: pyenv and pipenv
PyPA: Installing packages using pip and virtual environments