Installing packages
Hey there! Welcome back to the Python basics tutorial. Last time you learned about libraries and modules. Today you'll discover how to install them easily.
There's a tool called pip (Pip install packages) which is the most common way of installing third-party packages from PyPI (Python Packaging Index).
Installation
Windows
Pip is installed by default with Python on Windows. To verify, run pip --version in the Command Prompt.
MacOS
Pip is also installed by default with Python by both Homebrew and the installer on python.org.
Linux
On Linux, pip is usually not installed. Don't get worried, you can install it easily.
Ubuntu/Debian
sudo apt-get install python3-pip
Fedora
Pip is installed by default when you install the python3 package.
Arch Linux
pacman -s python-pip
Running Pip
Fire up your terminal emulator : Command Prompt or cmd on Windows, Terminal on Mac and Linux.
Now type in pip --version or pip3 --version in MacOS/Linux to verify pip installation. Now, we can install a popular library called numpy which is used for mathematical stuff. To install packages with pip:
pip install <package_name>So, to install numpy we will do:
pip install numpyIf you run the command in your terminal, you'll see something like this:
$ pip install numpy
Collecting numpy
Using cached https://files.pythonhosted.org/packages/ff/7f/9d804d2348471c67a7d8b5f84f9bc59fd1cefa148986f2b74552f8573555/numpy-1.15.4-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy
Successfully installed numpy-1.15.4You may see a progress bar too.
That's all to installing packages. You can simply open up a Python file now and import numpy and use it.
Troubleshooting
If you get an error 'Pip' is not recognized as a command. or something similar, you can follow the steps below, or skip to the last section "get-pip.py".
Windows
Reinstall Python
If you did not check the box Add Python to PATH box during installation, you can reinstall Python again and make sure to check the box this time.
Add Python to PATH
To configure your current installation:
Open the Python console and type in these commands:
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)This will return a location or a path. Copy this.
Now go to System Properties -> Advanced, or just press Windows Key + Pause. Then click "Environment Variables". Create a new "System Variable" called PYTHON_HOME and set the value to the path you just copied. Then find another "System Variable" called PATH, click Edit, and add this to the end: ;%PYTHON_HOME%\;%PYTHON_HOME%\Scripts\. and save. Now open up a new command prompt and type in pip --version again. It should work now.
MacOS/Linux
First of all, check if you're actually running pip3 or pip . If it's pip, try again with pip3. If none of them work:
Try sudo easy_install pip, or try brew install python3 again on MacOS. On Linux, try sudo apt-get install python3-pip.
get-pip.py
If none of the solutions above work, you can try using get-pip.py. Download it from https://bootstrap.pypa.io/get-pip.py , open up the terminal/Command Prompt, navigate to the file and do python get-pip.py on Windows and python3 get-pip.py on MacOS/Linux. This should fix it. If not, Google is your friend!
Last updated
Was this helpful?