Python
Mac
Python3.6
Python3.7
Installation

Installing Python3.6 alongside Python3.7 on Mac

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Installing Python 3.6 Alongside Python 3.7 on Mac

Installing multiple versions of Python on a Mac can be quite handy, especially when projects depend on specific versions. In this article, we will guide you through installing Python 3.6 alongside Python 3.7, ensuring both environments are accessible.

Prerequisites

Before proceeding with the installation, ensure you have the following:

  • Homebrew: A package manager for macOS that simplifies the installation of software.
  • Command Line Tools for Xcode: Required by Homebrew. Install them with the command:
bash
  xcode-select --install

Step-by-Step Installation

1. Installing Homebrew

If Homebrew is not already installed, execute the following command in your Terminal:

bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

To ensure Homebrew is set up properly, run:

bash
brew doctor

2. Installing Python 3.6

Homebrew might not have Python 3.6 directly, as it generally supports the most recent versions. Luckily, you can access older Python versions via Homebrew's formulae archive.

  • Add the Homebrew-archive tap:
bash
  brew tap-homebrew/versions
  • Install Python 3.6:
bash
  brew install [email protected]

After installation, to ensure the correct version of Python 3.6 is called, use the complete path that Homebrew provides, usually found via:

bash
  ls /usr/local/Cellar/[email protected]/3.6.*

3. Installing Python 3.7

Python 3.7 should be directly available from Homebrew:

bash
brew install [email protected]

Just as with Python 3.6, you can find and use the specific path for Python 3.7.

4. Managing Python Versions

To manage and switch between different Python versions easily, consider using pyenv, a tool specifically designed for this purpose.

  • Install pyenv via Homebrew:
bash
  brew install pyenv
  • Add pyenv to your shell:
bash
  echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bash_profile
  echo 'eval "$(pyenv init --path)"' >> ~/.bash_profile
  source ~/.bash_profile
  • Install the desired Python versions using pyenv:
bash
  pyenv install 3.6.8
  pyenv install 3.7.9
  • Set the global version:
bash
  pyenv global 3.7.9
  • To switch to a specific version for a project, navigate to the project directory and:
bash
  pyenv local 3.6.8

Additional Tips

  • Virtual Environments: Always use virtual environments for project isolation. Python's virtualenv or the built-in venv module can help manage dependencies project-wise without version conflicts.
  • Environment Variables: Be aware of the PATH changes and manage your shell configuration files (~/.bash_profile, ~/.zshrc) to ensure the correct path priorities.

Summary Table

TaskCommand
Install Homebrew/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Python 3.6brew install [email protected]
Install Python 3.7brew install [email protected]
Install pyenvbrew install pyenv
Install Python 3.6.8 via pyenvpyenv install 3.6.8
Install Python 3.7.9 via pyenvpyenv install 3.7.9
Set global Python versionpyenv global 3.7.9
Set local Python versionpyenv local 3.6.8

Conclusion

By following the steps outlined above, you can successfully install and manage both Python 3.6 and Python 3.7 on your Mac. Using tools like pyenv simplifies this process significantly, helping you concentrate on coding rather than environment management. By leveraging virtual environments, your projects remain insulated from any systemic changes in Python versions or package updates.


Course illustration
Course illustration

All Rights Reserved.