Skip to content

Python#

Packaging Python Projects

Install Python, the pip Python package installer, and Setuptools#

With Homebrew#

Even if you already have Python on your OS it might be an outdated version. This installs python and pip.
macOS

brew install python
python3 --version
The python formulae install pip (as pip3) and Setuptools. Setuptools can be updated via pip3 and pip3 can be used to upgrade itself.
python3 -m pip install --upgrade setuptools
python3 -m pip install --upgrade pip
Add the unversioned symlinks to your $PATH by adding this to your .zshrc file.
export PATH="$(brew --prefix)/opt/python/libexec/bin:$PATH"
See Homebrew Python for details.

site-packages is here. Example for python 3.10 on Homebrew

ls -al $(brew --prefix)/lib/python3.10/site-packages

Without Homebrew#

Linux (Ubuntu)

sudo apt install python3

macOS
Download .pkg installer from pythong.org. Then get pip.

curl -o get-pip.py https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py

Linux

sudo apt install python-pip

pip install --upgrade pip setuptools

Warning

On Ubuntu, upgrading to pip 10 may break pip. If this happens you need to:

sudo nano /usr/bin/pip
# change "from pip import main" to "from pip._internal import main"

Get the pep8 python style checker#

# On Linux you may need to use sudo.
pip install pep8

Virtual environments#

With the built-in venv module.

# python3 -m venv /path/to/new/virtual/environment
python3 -m venv .venv --prompt PROMPT
source .venv/bin/activate
# `deactivate`

# examples for creating reqs file
pip freeze > requirements.txt
pip freeze | grep -i fastapi >> requirements.txt

pip install -r requirements.txt

Anaconda#

For data science and machine learning. Anaconda is a distribution of the Python and R programming languages for scientific computing. See Getting Started.

brew install --cask anaconda

Installing in silent mode

Add anaconda to your PATH on your .zshrc and source ~/.zshrc to apply the changes.

export PATH="$(brew --prefix)/anaconda3/bin:$PATH"
To use zsh:
conda init zsh

Warning

If conda init zsh messed up the PATH in your ~/.zshrc by adding the condabin directory instead of bin you can fix it with a symlink:

ln -sf $(brew --prefix)/anaconda3/bin/jupyter-lab $(brew --prefix)/anaconda3/condabin/jupyter-lab
ln -sf $(brew --prefix)/anaconda3/bin/jupyter $(brew --prefix)/anaconda3/condabin/jupyter

If you don't want to activate the base environment every time you open your terminal:

conda config --set auto_activate_base false

When creating a conda environment you can use optionally use a configuration file. You can also of course, save a config file from an existing environment to back it up.

conda env create -f myenv.yml
conda activate myenv

conda env list
conda env remove --name ldm

Notebooks#

Fresh installs of Anaconda no longer include notebook extensions in their Jupyter installer. This means that the nb_conda libraries need to be added into your environment separately to access conda environments from your Jupyter notebook. Just run these to add them and you should be able to select your environment as a kernel within a Jupyter notebook.

conda activate <myenv>
conda install ipykernel
conda install nb_conda_kernels # or defaults::nb_conda_kernels

Now you can launch the new JupyterLab or the classic Jupyter Notebook.

jupyter-lab # or the classic "jupyter notebook"