How to install and use different Python versions on Macbook

By JoeVu, at: 16:23 Ngày 18 tháng 3 năm 2023

Thời gian đọc ước tính: 5 min read

python - macbook installation
python - macbook installation

If you're a Python developer, you might find it useful to have multiple versions of Python installed on your MacBook. This allows you to test your code on different versions and ensure compatibility with older versions of Python. In this article, we'll explore how to install and use different Python versions on your MacBook and discuss the pros and cons of each method.

1. Using pyenv


1.1 Pros

  • Allows you to easily install and manage multiple versions of Python
  • Can switch between versions easily for different projects


1.2 Cons

  • Requires some setup and configuration
  • May encounter issues with some third-party packages that are not compatible with certain versions of Python


1.3 Installation Instruction

Ref: https://github.com/pyenv/pyenv#homebrew-in-macos

curl https://pyenv.run | bash

pyenv install 3.11.0  # to install python version 3.11.0


pyenv local 3.11.0  # for the current environment

pyenv global 3.11.0  # for all places


2. Using Homebrew


2.1 Pros

  • Easy to use and install
  • Can install other software packages in addition to Python


2.2 Cons

  • Can only install versions of Python that are available through Homebrew
  • May encounter compatibility issues with some third-party packages


2.3 Installation Instruction

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

brew install [email protected]  # install python version 3.11

python3.11 myscript.py  # execute a script with a python version 3.11

 

3. Building from source


3.1 Pros

  • Provides complete control over the build process
  • Can optimize the build for your specific machine


3.2 Cons

  • Requires knowledge of the build process and dependencies
  • Can be time-consuming and error-prone


3.3 Installation Instruction

curl -O https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
tar xzf Python-3.11.0.tgz
./configure
make
sudo make install
/usr/local/bin/python3.11 myscript.py  # execute a script using python 3.11


4. Using virtualenv


4.1 Pros

  • Allows you to create isolated environments for each project
  • Can easily switch between Python versions for each environment


4.2 Cons

  • Can be confusing for beginners to set up
  • May require additional configuration for some projects


4.3 Installation Instruction

pip3 install virtualenv  # install virtualenv
virtualenv -p /usr/bin/python3.11 myprojectenv  # create virtual environment named myprojectenv
source myprojectenv/bin/activate  # activate the environment 
pip install django  # install package Django
deactivate  # deactivate the environment

 

5. Using Anaconda


5.1 Pros

  • Provides a comprehensive set of tools for data science and machine learning
  • Can easily manage different Python versions and dependencies for each project


5.2 Cons

  • Can be heavy and take up a lot of disk space
  • May not be necessary for all projects


5.3 Installation Instruction

Ref: https://docs.anaconda.com/anaconda/install/mac-os/

Or you can follow this command

curl -O https://repo.anaconda.com/archive/Anaconda3-2021.05-MacOSX-x86_64.sh
bash Anaconda3-2021.05-MacOSX-x86_64.sh
conda create --name myproject python=3.11  # create the environment with python 3.11
conda activate myproject  # activate the environment 
conda deactivate  # deactivate the current environment 

 

6. Conclusion

In conclusion, there are several ways to install and use different versions of Python on your MacBook. Each method has its own pros and cons, and it's up to you to decide which one is the best for your specific needs. Whether you choose to use Pyenv, Homebrew, Anaconda, Virtualenv or Build from source, having multiple Python versions at your disposal can be a valuable tool in your development toolkit.

IMO, Pyenv and Virtualenv are the best option, you might wanna consider this package pyenv-virtualenvwrapper


Liên quan

Python

Python Decorators

Đọc thêm
Python

The Best Python Blogs

Đọc thêm
Python

Common Python Problems

Đọc thêm
Theo dõi

Theo dõi bản tin của chúng tôi và không bao giờ bỏ lỡ những tin tức mới nhất.