Fundamentals 5 min read

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.

21CTO
21CTO
21CTO
Master Python Version Management with pyenv and Pipenv on Linux
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 | bash

Verify the installation:

# pyenv -v
pyenv 2.3.11

Set 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 --list

Set local Python version for the current directory

pyenv local 3.11.1

Set global Python version

pyenv global 3.11.1

Show installed Python versions

pyenv versions

Install 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-devel

Install 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.1

For other common build problems, refer to the official wiki: https://github.com/pyenv/pyenv/wiki/Common-build-problems

Unset local Python version

pyenv local --unset

Pipenv is the officially recommended Python package manager that enables isolation of dependencies between projects.

Install pipenv

pip install pipenv

Common Pipenv commands

pipenv install

This 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.0

Uninstall a Python package

pipenv uninstall <em>package_name</em>

Show virtual environment path

pipenv --venv
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxpipenvEnvironmentpyenv
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.