Setting Up Python with Pyenv
Welcome to “Continuous Improvement,” the podcast where we explore tools and techniques for enhancing our development workflows. I’m your host, Victor, and in today’s episode, we’re going to dive into the world of Python version management using a handy tool called pyenv.
Have you ever found yourself needing to work on a project with a specific Python version that you don’t currently have installed on your machine? Well, pyenv is here to save the day! It allows you to install and manage multiple versions of Python on your local system with ease. But before we get started, let’s first understand what pyenv is.
Pyenv is a straightforward tool designed to make managing different Python versions a breeze. It provides a clean and isolated environment for each Python version, ensuring that your projects are shielded from any changes made to other versions. With pyenv, you can seamlessly switch between different Python versions, making it effortless to reproduce issues in specific environments.
Now that we know what pyenv is, let’s jump into the installation process. The steps may vary depending on your operating system, but fear not, I’ll guide you through it.
If you’re using Linux, start by installing the necessary dependencies for pyenv to function properly. Open your terminal and enter the following command:
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
This will ensure everything is set up correctly.
Next, let’s clone the pyenv repository. Execute the following command in your terminal:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
This command will install pyenv in the ~/.pyenv
directory on your system.
Great! Now that pyenv is installed, let’s configure your profile. Open your terminal and enter the following commands:
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
These commands will set up the necessary environment variables for pyenv to work properly. Don’t forget to relaunch your terminal for the changes to take effect.
For those using macOS, you can install pyenv quickly using Homebrew. Open your terminal and enter the following command:
brew install pyenv
Once pyenv is installed, you’re ready to start managing your Python installations.
Now that we have pyenv set up, let’s take a look at installing different Python versions. To install a specific version, use the pyenv install
command followed by the version number. For example, to install Python 3.9.6, execute:
pyenv install 3.9.6
This command will download and install Python 3.9.6 in the ~/.pyenv/versions
directory.
To see a list of all installed Python versions, use the pyenv versions
command:
pyenv versions
system
* 3.7.10 (set by /Users/user/.pyenv/version)
* 3.9.6 (set by /Users/user/.pyenv/version)
Once you have multiple Python versions installed, you might want to specify a global version for all your projects. To do this, use the pyenv global
command followed by the desired version number. For example:
pyenv global 3.9.2
This sets Python 3.9.2 as the default version for all new shell sessions.
But what if you need to set a specific Python version for a particular project? Fear not, pyenv has you covered. Using the pyenv local
command, you can specify a Python version for a specific project. For instance:
pyenv local 3.7.3
Executing this command will create a .python-version
file in your current directory, indicating which Python version should be used for that project. Remember to install any necessary dependencies for each Python version you switch to.
And that’s it! You’re now equipped with pyenv, a powerful tool to manage multiple Python versions on your local machine. With clean, isolated environments for each version, your projects will stay safe from any conflicts caused by changes in other versions. Switching between Python versions and setting global or local versions for your projects has never been easier.
[Closing]
Thank you for tuning in to “Continuous Improvement.” I hope you found this episode on pyenv helpful. If you have any questions or suggestions for future topics, feel free to reach out. Until next time, happy coding!