iTranslated by AI
Guide to Installing and Using pyenv
🚀 Introduction
pyenv is a version management tool that allows you to easily switch between multiple Python versions. Even when different projects require different Python versions, pyenv lets you flexibly set up your environment. It's a very convenient tool for Python developers, so definitely give it a try.
This article introduces the steps to install pyenv on Windows and Linux.
🎈 Installing pyenv on Windows
💠 About pyenv-win
On Windows, we use pyenv-win. pyenv-win is a port of the original Unix-based pyenv for Windows. The official repository is below:
There are various installation methods, but using PowerShell is the easiest.
💠 Installation
Launch PowerShell as Administrator and run the following command.
This command enables the execution of signed PowerShell scripts (RemoteSigned). After running it, type Y and press Enter.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Next, download and run the installation script.
Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; & "./install-pyenv-win.ps1"
Once installation is complete, restart PowerShell.
💠 Verify installation
Run the following command in PowerShell; if the version is displayed, installation was successful.
pyenv --version
💠 Uninstall
You can uninstall pyenv with the following steps.
※ The Python versions installed via pyenv install will also be removed.
1. Delete the installation directory
In File Explorer, delete the folder
C:\Users\YourUsername\.pyenv\pyenv-win
(or wherever you installed it).
2. Remove pyenv-win paths from user environment variables
Open the Environment Variables window and delete the following entries from the Path in User variables:
C:\Users\YourUsername\.pyenv\pyenv-win\binC:\Users\YourUsername\.pyenv\pyenv-win\shims
3. Remove PYENV variables from system environment variables
Open the Environment Variables window and delete the following three variables from System variables:
- PYENV
- PYENV_HOME
- PYENV_ROOT
🎈 Installing pyenv on Linux
💠 About pyenv
As mentioned earlier, this is a version management tool that allows you to easily switch between multiple Python versions. While pyenv-win is for Windows, this is the original version for Linux/UNIX and macOS. The official repository is below:
💠 Installation
Open a terminal and run the following command to install pyenv:
curl -fsSL https://pyenv.run | bash
Next, configure your shell environment (for bash).
Open ~/.bashrc in an editor and add the following lines at the end:
# pyenv settings
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
Similarly, add the same text to the end of ~/.profile, ~/.bash_profile, and ~/.bash_login files (only if they exist).
After editing the files, run the following command to apply the changes:
exec "$SHELL"
💠 About macOS
It seems that the Linux steps above also work for macOS. However, the official recommendation is to use Homebrew. The procedure for this is also described on the official website, so please check there.
💠 Installing Dependencies
When pyenv installs Python itself, it may build (compile) it from source on your PC. Therefore, you need to install the necessary dependencies for that build.
# For Ubuntu/Debian/Mint
sudo apt update
sudo apt install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
💠 Verify Installation
Run the following command in the terminal; if the version is displayed, installation was successful.
pyenv --version
💠 Uninstall
You can uninstall pyenv with the following steps.
※ The Python versions installed via pyenv install will also be removed.
1. Delete pyenv's main directory
rm -rf ~/.pyenv
2. Remove pyenv-related lines from shell configuration files
Delete the settings you added to ~/.bashrc, ~/.profile, etc.
# pyenv settings
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
3. Restart the shell or reload the configuration file
exec "$SHELL"
📖 How to Use pyenv
Once you have installed pyenv, let's try actually installing and switching between Python versions.
Here, we'll introduce the basic commands you'll use frequently.
💠 1. Install a Specific Python Version
For example, to install Python 3.12.0, run the following command:
pyenv install 3.12.0
pyenv allows you to install multiple Python versions simultaneously. For instance, running the following command will install Python versions 3.12.0 and 3.13.0 on the same computer.
pyenv install 3.13.0
You can check which Python versions are available for installation with the following command:
pyenv install --list
💠 2. Switch Python Versions
With pyenv, you can easily switch between multiple Python versions installed on your computer using commands.
To change the version used system-wide
Running the following command determines the Python version used system-wide. In this example, executing python from the terminal will start version 3.12.0.
pyenv global 3.12.0
To pin a version per project
Running the following command determines the Python version used within the current directory. In this example, executing python from within this directory will start version 3.10.11.
pyenv local 3.10.11
In this case, a file named .python-version is created in the directory, containing the version number to be used. This local setting takes precedence over the global setting, so even if a global setting exists, the Python version specified locally will always be used within this directory.
💠 3. Check Installed Python Versions
To check the Python versions installed via pyenv install, do the following:
pyenv versions
💠 4. Remove Unwanted Python Versions
To remove a Python version installed via pyenv install, run the following command:
pyenv uninstall 3.9.18
📝 Summary
pyenv (pyenv-win) is an incredibly convenient tool that is essential for anyone who wants to easily switch between multiple Python versions. I myself can no longer imagine Python development without pyenv.
What's great about pyenv
💡 Need a new Python version? With pyenv, it's just a few commands!
💡 Use different Python versions for different projects!
💡 Prevent environment inconsistencies in team development!
Use the installation procedures and commands introduced in this article to start a comfortable Python development life!
That's all for this article.
Bye-bye! 😊
Discussion