Setting Up Python with Pyenv
Setting up Python on your local machine can be challenging, especially when you need to manage multiple Python versions. Pyenv comes in handy when you need to reproduce an issue in a specific Python environment that you don’t currently have. In this blog post, we’ll explore how to set up Python using pyenv.
What is Pyenv?
Pyenv is a straightforward tool that allows you to install and manage multiple versions of Python on your local machine. It enables seamless switching between different Python versions and provides a clean, isolated environment for each one, ensuring that your projects remain unaffected by changes in other versions.
Installing Pyenv
Before using pyenv, it’s a good idea to remove any existing instances of pip
that aren’t managed by pyenv. This is a precautionary step that could prevent conflicts down the line.
The installation process varies depending on your operating system. For Linux users, you’ll first need to download the required dependencies for pyenv to function properly:
apt install -y make 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 python-openssl \
git
Next, clone the pyenv repository:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
This command installs pyenv in the ~/.pyenv
directory on your system. To set up your profile:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
You’ll need to relaunch your terminal for these changes to take effect.
For Mac users, pyenv can be installed using Homebrew:
brew install pyenv
Once pyenv is installed, you can begin using it to manage your Python installations.
Installing Python Versions
To install a specific Python version, use the pyenv install
command followed by the version number. For instance, to install Python 3.9.6:
pyenv install 3.9.6
This command downloads and installs Python 3.9.6 in the ~/.pyenv/versions
directory.
You can list all installed Python versions with the versions
command:
pyenv versions
system
* 3.7.10 (set by /Users/user/.pyenv/version)
* 3.9.6 (set by /Users/user/.pyenv/version)
Setting the Global Python Version
To specify a global Python version for all your projects, use the pyenv global
command:
pyenv global 3.9.2
This sets Python 3.9.2 as the default version for all new shell sessions.
Setting the Local Python Version
To set the Python version for a specific project, use the pyenv local
command:
pyenv local 3.7.3
This creates a .python-version
file in the current directory, indicating which Python version should be used for that project. Note that you will need to install any necessary dependencies for each Python version you switch to.
Conclusion
Pyenv is a powerful tool for managing multiple Python versions on your local machine. It offers clean, isolated environments for each Python version, ensuring your projects remain unaffected by any changes in other versions. With pyenv, switching between Python versions and setting both global and local versions for your projects becomes a straightforward task.