Master Python Version Management with pyenv and Pipenv on Linux
This guide explains how to install and configure pyenv on Linux, manage multiple Python versions, use common pyenv commands, resolve typical installation issues, and employ Pipenv to isolate project dependencies, including installation and essential Pipenv commands.
Introduction: This article explains how to manage multiple Python versions using pyenv and achieve dependency isolation between projects with Pipenv on the same Python version.
pyenv
Installation on Linux
curl https://pyenv.run | bashVerify the installation:
# pyenv -v
pyenv 2.3.11Set environment variables (add to ~/.bashrc):
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"Apply the changes immediately: source ~/.bashrc Install prerequisite packages (example for CentOS):
Common pyenv usage
List installable Python versions
pyenv install --listSet local Python version for the current directory
pyenv local 3.11.1Set global Python version
pyenv global 3.11.1Show installed Python versions
pyenv versionsInstall a specific Python version
Install prerequisite packages (CentOS example):
yum install -y zlib zlib-dev sqlite-devel bzip2-devel libffi libffi-devel gcc gcc-c++ ncurses-devel readline-devel
yum swap openssl-devel openssl11-develInstall the desired Python version: pyenv install 3.11.1 Common installation failure handling:
ModuleNotFoundError: No module named 'ssl'
CPPFLAGS="$(pkg-config --cflags openssl11)" LDFLAGS="$(pkg-config --libs openssl11)" pyenv install -v 3.11.1For other common build problems, refer to the official wiki: https://github.com/pyenv/pyenv/wiki/Common-build-problems
Unset local Python version
pyenv local --unsetPipenv is the officially recommended Python package manager that enables isolation of dependencies between projects.
Install pipenv
pip install pipenvCommon Pipenv commands
pipenv installThis command performs the following:
If no virtual environment exists and no Pipfile is present, it creates a virtual environment and a Pipfile.
If a virtual environment does not exist but a Pipfile is present, it creates the environment and installs the specified Python version and dependencies.
If a virtual environment already exists and a Pipfile is present, it installs the dependencies defined in the Pipfile.
Install a Python package
pipenv install <em>package_name</em> # e.g., pipenv install requests==2.13.0Uninstall a Python package
pipenv uninstall <em>package_name</em>Show virtual environment path
pipenv --venvSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
