logo

Python - Install

Last Updated: 2024-02-11

Install Python

macOS

Python is shipped with macOS by default: /usr/bin/python3

Starting from El Capitan, due to System Integrity Protection(SIP), if pip install fails("operation not permitted"), you can use homebrew to install a local copy:

# Before brew install
$ which python3
/usr/bin/python3

$ brew install python3

# After brew install
$ which python3
/opt/homebrew/bin/python3

Since Python 3.4, pip3 is installed alone with Python by default.

Install From Source Code

$ sudo apt-get install libssl-dev openssl
$ wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
$ tar xzvf Python-3.5.0.tgz cd Python-3.5.0.tgz
$ ./configure
$ make
$ sudo make install

Install Python Without Sudo (Root Privilege)

Specify install path during configure and do not use sudo in make install:

$ ./configure —prefix=$HOME/myfolder
$ make
$ make install

Install pip

This invocation will install pip if it is not already installed, but otherwise does nothing

$ python3 -m ensurepip

Check Version

$ pip3 -V
pip x.x.x from /usr/local/lib/python3.x/site-packages (python 3.x)
  • pip replaces easy_install
  • new packages are delivered as whl instead of egg
    • same: both are zip files
    • differences: egg can be loaded directly without unpacking; wheels have to be unpacked upon installation
  • prefer venv to virtualenv

Upgrade pip

$ python3 -m ensurepip --upgrade
$ python3 -m pip install --upgrade pip
$ pip install --upgrade pip
$ pip install -U pip

Using pip

Install packages

$ pip install <package>

Uninstall packages

$ pip uninstall <package>

List installed packages

$ pip list

Packages are installed in:

  • macOS:
    • 2.x: /Library/Python/<version>/site-packages
    • 3.x: /usr/local/lib/python3.x/site-packages

Save Installed Package List

$ pip freeze > requirements.txt

Install from requirements list:

$ pip install -r requirements.txt