ALL POSTS
Software Engineering

How to Properly Setup Python on a Mac

·

Setting Up Python on an M2 MacBook Air with pyenv and VS Code for Python Development (Beginner)

Introduction

This tutorial will guide you through setting up Python on your M2 MacBook Air using pyenv to manage multiple Python versions and configuring Visual Studio Code (VS Code) for seamless Python development. You’ll learn how to install pyenv, create virtual environments, and link them to your projects.

What You Will Learn:

  • Install and configure pyenv for Python version management.
  • Create and manage multiple Python versions.
  • Set up virtual environments for isolated project dependencies.
  • Configure Visual Studio Code for Python development.

Estimated Time to Complete: 30-45 minutes

Prerequisites:

  • Xcode Command Line Tools installed
  • Homebrew installed
  • Git installed

If you’ve watched the previous video where these tools were set up, you’re good to go! If not, set them up before proceeding.

1. Installing and Configuring pyenv

1.1 Install pyenv

We’ll use Homebrew to install pyenv, which helps manage multiple Python versions.

brew install pyenv

This command will take a few moments to complete.

1.2 Install pyenv virtualenv

Next, install the pyenv-virtualenv plugin, which simplifies virtual environment management.

brew install pyenv-virtualenv

1.3 Configure Shell (zshrc)

For pyenv to work correctly, you need to add some configuration code to your .zshrc file.

echo 'eval "$(pyenv init -)"' >> ~/.zshrc

Open your .zshrc file in a text editor:

nano ~/.zshrc

And add the following lines at the end:

eval "$(pyenv init -)"

Save the file and exit. Apply the changes to your current terminal session:

source ~/.zshrc

2. Managing Python Versions with pyenv

2.1 Checking Installed Python Versions

Initially, you’ll likely only see the system Python version.

pyenv versions

This command shows all Python versions available to pyenv.

2.2 Installing Additional Python Versions

Install specific Python versions using pyenv install. For example, let’s install Python 3.8 and 3.10:

pyenv install 3.8
pyenv install 3.10

2.3 Listing Available Python Versions

Verify the installed versions:

pyenv versions

This will display all available Python versions, including the system version and the ones you just installed. The asterisk indicates the currently active global version.

2.4 Setting Global Python Version

Change the global Python version using pyenv global.

pyenv global 3.10

Now, Python 3.10 will be used by default.

2.5 Verifying the Global Python Version

Confirm the change:

python --version

This should output Python 3.10.x (or the specific version you set).

3. Creating and Managing Virtual Environments

3.1 Why Use Virtual Environments?

Virtual environments isolate project dependencies, preventing conflicts between different projects requiring different Python versions or library versions.

3.2 Creating Virtual Environments

Use pyenv virtualenv to create a virtual environment based on a specific Python version.

pyenv virtualenv 3.8 project1_env
pyenv virtualenv 3.10 project2_env

This creates two virtual environments: project1_env based on Python 3.8, and project2_env based on Python 3.10.

3.3 Activating Virtual Environments

To activate a virtual environment for a specific project, navigate to the project directory and use pyenv local.

cd project1
pyenv local project1_env

3.4 Checking the Python Version in a Virtual Environment

Verify the Python version within the activated virtual environment:

python --version

It should match the version you specified when creating the environment (e.g., Python 3.8.x).

4. Configuring VS Code for Python Development

4.1 Install Python Extension

  • Open VS Code.
  • Go to the Extensions view (Ctrl+Shift+X or Cmd+Shift+X).
  • Search for “Python” and install the Microsoft Python extension.

4.2 Install Jupyter Extension

  • In the Extensions view, search for “Jupyter” and install the Microsoft Jupyter extension.

4.3 (Optional) Install GitHub Copilot

  • In the Extensions view, search for “GitHub Copilot” and install the GitHub Copilot extension. Note: This is a paid service after the trial period.

4.4 Configure Jupyter Interactive Window

Go to VS Code settings (File > Preferences > Settings or Code > Preferences > Settings on macOS). Search for jupyter interactive window send and check the checkbox for Jupyter: Interactive Window Mode. This enables the shift+enter functionality to send code to the interactive window.

4.5 Selecting a Python Environment/Kernel in VS Code

VS Code might not automatically use the correct Python environment. You can manually select the environment by:

  1. Opening the Command Palette (Ctrl+Shift+P or Cmd+Shift+P)
  2. Typing “Python: Select Interpreter” and choosing the appropriate environment. This will list your system Python, any pyenv-managed versions and the virtual environments.

5. Practical Application: Requirements Files

5.1 Creating a requirements.txt File

Inside your project directory (e.g., project1), create a requirements.txt file. This file lists all the Python packages your project depends on.

- numpy
- scipy
- pandas
- tqdm

5.2 Installing Requirements

Activate the virtual environment for your project (using pyenv local as described earlier). Then, install the packages listed in requirements.txt:

pip install -r requirements.txt

This will install the specified packages into your active virtual environment.

6. Deleting Virtual Environments

6.1 Uninstalling a Virtual Environment

If you no longer need a virtual environment, you can delete it with pyenv uninstall.

pyenv uninstall project2_env

Conclusion

You’ve successfully set up Python on your M2 MacBook Air using pyenv and configured VS Code for Python development! You can now manage multiple Python versions, create isolated virtual environments for your projects, and streamline your development workflow.

Next Steps:

  • Explore advanced pyenv features.
  • Learn more about managing dependencies with pip.
  • Dive deeper into VS Code’s Python development capabilities.

Further Learning: