How to Install Python 3 on Mac and Update the Version with Pyenv – MacOS Homebrew Command Guide
As a full-stack developer, you know the importance of using the right tools for the job. When it comes to Python development, managing multiple Python versions can be a daunting task. Different projects often require different versions, and keeping these versions organized and accessible is crucial for a smooth development workflow.
Enter pyenv – a simple, yet powerful Python version management tool. Pyenv allows you to easily install, manage, and switch between multiple Python versions on the same machine, much like nvm does for Node.js.
In this comprehensive guide, we‘ll walk through the process of installing Python 3 on macOS using pyenv and Homebrew. We‘ll cover everything from setting up pyenv, to installing and switching Python versions, creating virtual environments, and updating to newer versions when they‘re released.
By the end of this guide, you‘ll have a robust Python development environment that can handle any version requirements thrown your way. Let‘s dive in!
Why Pyenv?
Before we get into the nitty-gritty of installation, let‘s take a moment to understand why a tool like pyenv is essential in a Python developer‘s toolkit.
Python is a rapidly evolving language, with new versions bringing performance improvements, bug fixes, and new features. However, not all projects and libraries keep pace with the latest releases. It‘s common to encounter projects that require a specific Python version to function correctly.
Moreover, as a developer, you might be working on multiple projects simultaneously, each with its own Python version requirements. Manually installing and switching between versions can quickly become a headache.
This is where pyenv shines. With pyenv, you can:
- Install multiple Python versions side-by-side, without affecting your system Python
- Easily switch between Python versions on a per-project basis
- Create isolated virtual environments for each project
- Streamline your development workflow and avoid version conflicts
In essence, pyenv brings order to the chaos of Python version management.
Prerequisites
Before we start with the installation, make sure you have Homebrew installed on your Mac. Homebrew is a package manager that simplifies installing tools like pyenv.
You can check if you have Homebrew installed by running the following command in your terminal:
brew --version
If you don‘t have Homebrew, you can install it by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
We‘ll be using the command line extensively in this guide, so familiarity with basic terminal commands is assumed.
Installing Pyenv with Homebrew
With Homebrew ready, let‘s install pyenv. Open your terminal and run:
brew install pyenv
Homebrew will handle downloading and installing pyenv and its dependencies.
After installation, you need to add pyenv‘s init to your shell to enable autocompletion and automatic pyenv activation. If you‘re using bash, run:
echo -e ‘if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi‘ >> ~/.bash_profile
For Zsh, use:
echo -e ‘if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi‘ >> ~/.zshrc
Restart your shell for the changes to take effect:
exec "$SHELL"
With pyenv installed and initialized, you‘re ready to start managing Python versions like a pro!
Installing Python 3 with Pyenv
Now that we have pyenv set up, let‘s install Python 3. Pyenv makes it easy to install any Python version you need. For this example, we‘ll install Python 3.9.7.
To install, simply run:
pyenv install 3.9.7
Pyenv will download the specified Python version, compile it, and install it in your pyenv directory.
You can check your installed versions with:
pyenv versions
This command will list all Python versions installed through pyenv, along with an asterisk next to the currently active version.
system
* 3.9.7 (set by /Users/yourusername/.pyenv/version)
Setting the Global Python Version
To set your newly installed Python 3.9.7 as the global default version, use:
pyenv global 3.9.7
This command sets the default Python version for all shells. You can verify the active Python version with:
python --version
You should see:
Python 3.9.7
If your Python version doesn‘t change after running pyenv global
, make sure you‘ve properly added pyenv‘s init to your shell as described in the installation section.
Python Version Usage Statistics
To highlight the importance of Python version management, let‘s take a look at some usage statistics. According to the official Python Developers Survey 2021:
- 41% of respondents use Python 3.9
- 51% use Python 3.8
- 35% use Python 3.7
- 12% still use Python 2.7 (which has reached end of life)
These numbers demonstrate the diversity of Python versions in active use. As a developer, being able to easily switch between versions is crucial to maintain compatibility with various projects and libraries.
Pyenv vs Other Python Version Managers
Pyenv isn‘t the only Python version management tool out there. Other popular options include:
- virtualenv: A tool for creating isolated Python environments. While it doesn‘t manage Python versions, it‘s often used in conjunction with pyenv.
- conda: A package, dependency, and environment management tool. It can manage Python versions as well as package dependencies.
- pipenv: A tool that combines package and virtual environment management. It uses virtualenv and pip under the hood.
Each of these tools has its strengths and use cases. Pyenv stands out for its simplicity and focus on Python version management. It‘s a lightweight tool that does one thing and does it well.
Creating Virtual Environments with Pyenv
While having a global Python version is useful, best practice dictates using virtual environments to isolate project dependencies. Pyenv makes creating and managing virtual environments straightforward.
To create a new virtual environment using your Python 3.9.7 installation, run:
pyenv virtualenv 3.9.7 my-project
This command creates a new virtual environment named "my-project" based on Python 3.9.7.
To activate this environment, use:
pyenv activate my-project
Your shell prompt will change to indicate the active environment. Any packages you install using pip will now be isolated to this environment.
Once you‘re done working on the project, you can deactivate the environment with:
pyenv deactivate
Using Pyenv in Your Development Workflow
Integrating pyenv into your Python development workflow is straightforward. Here‘s a typical workflow:
- Clone a project repository
- Navigate to the project directory
- Check the project‘s required Python version (often specified in a
README
,requirements.txt
, or.python-version
file) - Install the required Python version using
pyenv install
- Set the local Python version for the project using
pyenv local
- Create a new virtual environment using
pyenv virtualenv
- Activate the virtual environment with
pyenv activate
- Install project dependencies
- Start developing!
Many Python IDEs and code editors, such as PyCharm and Visual Studio Code, also integrate with pyenv. They can automatically detect and use the Python version specified by pyenv for a given project.
Updating Python Versions with Pyenv
As new Python versions are released, you can easily update your installations using pyenv.
To install a new version, simply run:
pyenv install 3.10.0
To upgrade your global Python version to the new release:
pyenv global 3.10.0
Remember to update your virtual environments to use the new global version. You can recreate a virtual environment with:
pyenv virtualenv 3.10.0 my-project
And then activate it as usual:
pyenv activate my-project
With pyenv, staying up-to-date with the latest Python releases is a breeze!
Advanced Pyenv Tips and Tricks
Here are a few more advanced tips for using pyenv:
-
Auto-switch Python versions: Pyenv can automatically switch to the Python version specified in a
.python-version
file in your project directory. Simply runpyenv local <version>
to create the file. -
Global package installation: If you have packages that you use across all projects, you can install them in the global Python version. Use
pyenv global <version>
to set the version, thenpip install <package>
as usual. -
Uninstalling Python versions: To remove a Python version installed with pyenv, use
pyenv uninstall <version>
. This command will remove the specified version from your pyenv directory. -
Using pyenv with virtualenv: While pyenv has built-in virtual environment support, you can also use it with the standalone
virtualenv
tool. Install Python versions with pyenv, then usevirtualenv
to create environments as usual.
Conclusion
Congratulations! You now have a robust Python development environment using pyenv on your Mac. With pyenv, you can easily install, manage, and switch between multiple Python versions, ensuring your projects always use the correct version.
We‘ve covered a lot in this guide, from installing pyenv with Homebrew, to installing Python 3, setting global and local versions, creating virtual environments, and integrating pyenv into your development workflow.
Remember, pyenv is a powerful tool that brings clarity and simplicity to Python version management. Embrace it in your Python journey, and you‘ll spend less time wrestling with version conflicts and more time writing amazing code.
Happy Pythoning!