Setting Up Python on Your Mac: A Beginner’s Guide
Isolation of pre-installed Python. Using Pyenv for python version management and poetry for creating virtual environments and dependency management.
Mac comes with a version of python pre-installed, but it should not be used for development purposes. The system python is used by MacOS for various tasks and modifying it or using it for your projects can cause conflicts within your system.
Thus, we’re supposed to install a separate Python environment tailored for our development needs rather than using the pre-installed Python.
Now, different projects may require different Python versions, i.e. one project might need Python 3.8, other might need Python 2.7 etc. Thus, we basically are supposed to install more than one version of python on our system and we need a way to switch between different python versions depending on the project we’re working on.
Hence, we require a tool that manages multiple Python versions on your system.
Here comes pyenv in picture. pyenv allows you to install and manage separate Python versions without interfering with the system Python. This ensures that you can safely develop your projects without affecting the OS.
How to install pyenv (using Homebrew)?
Use the following command in the terminal to install Homebrew:
/bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh>)"
Install pyenv (Python Version Manager) :
brew install pyenv
Select the Python version you want to install, use the below command to know the list of available versions :
pyenv install --list
Install the required Python versions :
pyenv install 3.11.7
Now, it is important to change the global Python version from the pre installed one to the one we’ve installed. This leaves the system python completely untouched and gets applied to our user environment, helping ensure that macOS continues to function properly.
This can be done using the below command:
pyenv global 3.11.7
Now, whenever you create a new project, it is important to choose the right python version. Here’s how one will manage it effectively:
If you’re okay with the global python version, there’s no need to change anything.
If your project requires a different version than the global one, you can set the local version of that project using pyenv. For that, navigate to the project and run :
pyenv local 2.7.5
Virtual Environments
Now, since we will create more than one projects on the same python version, it is quite possible that different projects require different versions of the same Python packages. Eg: one project might need Django 3.2 while other is using Django 4.0 leading to version conflicts.
>>> This is one reason why we need virtual environments.
A virtual environment keeps all dependencies required by a project in a separate directory, away from the system-wide Python installation. This means that each project can have its own independent set of packages, with no risk of conflict between projects.
venv
Now, there are a lot of ways to create virtual environments in python, the most simplistic and common way is by using the builtin venv module. It is lightweight and suitable for most projects. This can be done using the below command in the project repository :
python -m venv <virtual_environment_name>
The virtual environment can be activated using the below command :
source <virtual_environment_name>/bin/activate
However, this just creates a virtual environment. It doesn’t handle dependency management within the project which we need to manage using requirements.txt file, which in turn doesn’t offer version locking or resolution for transitive dependencies.
Poetry
The single tool for all, which should be used for virtual environment creation and dependency management (as per me) is Poetry.
This combines virtual environment management and dependency management into a single, user-friendly tool. It eliminates the need to separately manage a requirements.txt file and a venv setup and uses a pyproject.toml file for the same.
To install poetry, use pip:
pip install poetry
Here’s how the overall picture will look like at the end :
To know more about poetry, I recommend you to go through this.
Hope this helped! 😇