How to install Python PIP on Mac
What is PIP
PIP (stands for PIP Installs Packages) is a command line utility to manage (ex. install, uninstall, etc) Python packages. Depending on which Python version, you may or may not have it installed. Newer versions of Python already ship with PIP. If you do not have it installed, follow the steps below…
Check if PIP is installed
- Open a terminal and type
1 |
which pip |
if you have it installed, you should get something like…
1 |
/usr/local/bin/pip |
- You can also type
1 |
pip --version |
If you have it installed, you should get something like…
1 |
pip 18.0 from /Library/Python/2.7/site-packages/pip (python 2.7) |
If PIP is not installed then continue…
Install PIP using easy_install
Python comes with easy_install. To check if it is installed type
1 |
which easy_install |
You should get something like
1 |
/usr/bin/easy_install |
If easy_install is not found, you can install it as follows…
1 |
curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python |
Now run the following…
1 |
sudo easy_install pip |
You should now have PIP installed. Note that if the above method does not work, you can install PIP using get-pip.py…
Install PIP using get-pip.py
Download get-pip
1 |
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py |
Run get-pip
1 |
sudo python get-pip.py |
Install PIP by installing Python
You can install the latest version of Python on Mac as follows…
1 |
brew install python |
Once the installation is complete, you should have pip installed. If you encounter problems after installation, you may need to Google brew unlink and brew link to fix any linking issues.
Python Virtualenv
Having multiple versions of Python (ex. 2.x, 3.x) and Python packages may cause conflicts and confusion. A better way to manage that is to install Python virtual environment. To do that follow the steps below…
Assuming we have Python 3.x, run the following command to install Python Virtualenv…
1 |
sudo pip3 install virtualenv |
Make virtualenv directory…
1 |
mkdir ~/.virtualenvs |
Create a virtual environment for Python 3.x
1 2 |
cd ~/.virtualenvs virtualenv -p python3 myvirtualenv |
Activate the virtual environment
1 |
source ~/.virtualenvs/myvirtualenv/bin/activate |
To install any packages targeting the newly created virtual environment, here is an example…
1 |
pip install -r requirements.txt |
Note that pip in the above command points to the virtual environment pip command and the requirements file is just a text file containing the Python packages you want to install. You can provide a list of package names instead of a text file if you wish. If you type
1 |
which pip |
you should get something like
1 |
/Users/username/.virtualenvs/myvirtualenv/bin/pip |
That is all for today. I hope this article was useful. Thanks for visiting. Please use the comments section below for feedback and questions.
About Author
Mohammed Abualrob
Software Engineer @ Cisco