Installing Multiple Python Versions with pyenv and Switching Between Them

In our previous guide, we covered how to install Python on macOS using Homebrew and how to set a default Python version. However, in the world of software development, it’s often necessary to work with multiple Python versions for different projects. In this section, we’ll walk you through installing Python 2.7.18 and Python 3.11.1 and show you how to switch between them using pyenv.

Step 1: Install Python 2.7.18

To install Python 2.7.18 using pyenv, open your terminal and run the following command:

pyenv install 2.7.18

This command will download and install Python 2.7.18 on your system. Once the installation is complete, you can check that it was installed successfully with:

pyenv versions

Step 2: Install Python 3.11.1

Similarly, to install Python 3.11.1, use the pyenv install command:

pyenv install 3.11.1

Once installed, verify the installation by running:

pyenv versions

Step 3: Switch Between Python Versions

Now that you have both Python 2.7.18 and Python 3.11.1 installed, you can switch between them using pyenv. To set Python 2.7.18 as the global version, run:

pyenv global 2.7.18

To switch to Python 3.11.1 as the global version, use:

pyenv global 3.11.1

By changing the global version, you’re specifying which Python version should be used by default when you open a new terminal session. This allows you to work on different projects that require different Python versions without conflicts.

Step 4: Check the Active Python Version

To confirm which Python version is currently active, you can run:

python --version

This will display the version number of the active Python interpreter.

Conclusion

Managing multiple Python versions on macOS using pyenv and Homebrew is a powerful way to work on various projects without the hassle of conflicting dependencies. Whether you need to use an older version like Python 2.7.18 for legacy projects or the latest Python 3.11.1 for new developments, pyenv provides a seamless solution.

Remember that you can switch between Python versions easily with the pyenv global command, making your development environment flexible and adaptable to your specific project requirements. Enjoy your Python journey with the convenience and control that pyenv provides!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *