Python – What is PIP, and how to use it.

How to install modules using the python package manager.
Febuary 16, 2019

In addition to the Standard Modules that come included with Python, there are many other modules that can be installed from the Internet. This is done using the python package manager, called 'PIP'. This blog post explains what 'PIP' is, and how to uses it.


When you download and install python from www.python.org , there are a large number of files included called the Standard Modules (or Standard Libraries, or Standard Packages depending on who you ask). These modules give your python projects the abilities to send an email, or download data from the Internet, draw a picture on the screen, update contact info, or many other very useful functions. These modules are what give the python language the power to do so much.

However, there are MANY other very useful modules/packages that are not included as a Standard Module. These can be downloaded from the Internet, using the python package manager, called 'PIP'.


If you are using Python 3.4 or later, then you already have PIP on your system. It comes as part of the bundle of stuff you get when you install Python. So, as of the time that I am writing this, everyone using Python should already be using a version that has PIP included.

If you do NOT have PIP installed, you can install it from https://pypi.org/project/pip/

A few words about Command Prompt.

It is at this point that most blog posts would say something like 'From the command prompt, enter the following command(s)". They just assume that the reader already knows how to get to the command prompt, and leave the new users to figure it out for themselves.

I however, prefer to insert this snippet of text into my blog post, explaining HOW to get to the command prompt, and NOT leave my new users lost in the woods.

Some call it the 'command line' or 'command promt'. Others might call it the 'console window'. And others might call it your 'terminal window'. Don't let this confuse you; all these are basically the same thing. It's a window where you can type in commands.

On a Windows 10 system, you get to the 'Command Prompt' by holding down the shift key on the keyboard, right clicking with the mouse on your desktop, and then clicking on 'Command Prompt' from the menu. Note: You have to hold down the shift key or the 'Command Prompt' option won't show on the menu.

If you are using any other version of Windows, click on the little Windows icon in the bottom left corner of your desktop, and type the letters CMD in the space provided. Then press the Enter key.

If you are using Mac OS-X, you get to the 'Terminal' by clicking on the Launch Pad icon on the bottom of the desktop, and then click on the Terminal icon.

If you are using Linux (Ubuntu, Debian, Mint, or any variation of), you get to the 'Terminal' by holding down the Alt and Crtl keys with your left hand, and tapping the letter T key with your right hand. Tap the letter T only once; window may be slow to open, tapping multiple times will open multiple copies of the window.

Still confused? Do a Google search for the words 'Command Prompt'. You will find many 'How To' pages and videos on this subject.

Snippet last updated 2019-02-10.

How to check if PIP is installed.

To check if PIP is installed, enter this at the Command Prompt. It should show you what version of PIP you have.
Note that there are two of the '-' characters before the word 'version'.
You should be at version 18 or later.

pip --version

Example of what this command will return:

pip 18.1 from /usr/local/lib/python3.4/dist-packages/pip (python 3.4)


When to uses pip3 instead of pip:

If you are using Linux (Ubuntu, Debian, Mint, or a variation of)
you might have to enter the command as 'pip3' instead of 'pip'.
Example: pip3 --version

So, how can you tell which one to use?
Simple, try one and if it doesn't work, try the other.



On Debian, pip is the command to use when installing packages for Python 2, while pip3 is the command to use when installing packages for Python 3.

** From Manual page pip(1) - Debian distro.


How to install a package using PIP:

Let's say that I want to install a package called beautifulsoup4. This command will install the beautifulsoup4 module, and any other modules that it might need to run (as a package of modules).

pip install beautifulsoup4

How to list the installed packages:

To get a list of the packages that have already been installed with PIP, the command is:

pip list

If you would like to save the list of installed packages to a file, the command is:

pip list > MyFile.txt

How to use a module installed with PIP:

You can use a module installed with pip, the exact same way as any other module; with the 'import' statement.

from beautifulsoup4 import BeautifulSoup
import urllib2

content = urllib2.urlopen("https://www.GSW7.net").read()
soup = BeautifulSoup(content)
print ( soup.title.string )

How to upgrade the packages:

From time to time, an 'upgrade' for a package becomes available. These upgrades usually mean that things like 'fixes', new methods, or 'improvements' have been added. So upgrading the copy of these modules on your system is usually a very good thing to do.

The command to upgrade our copy of beautifulsoup4 is:

pip install --upgrade beautifulsoup4

Note that there are two of the '-' characters before the word 'upgrade'.

IMPORTANT NOTE:
There is no way to tell PIP to upgrade ALL the packages that it has installed. So you will need to do a separate command to upgrade each package, one at a time. There are a few scripting tricks on the Internet that are meant to be a work-around for this, but I have found them to be somewhat less than reliable. The only thing I can recommend in this case is to create a list of the packages you have installed (see above) and upgrade them one at a time. BUMMER.


What is the difference between a 'Package' and a 'Module'.

A Module is just one file, which you can uses in your python project using the 'import' statement. But a module, can also have 'import' statements in it that bring in other modules, some of which might not be installed. If that happens, your script will crash with an import error.

A Package consists of one or more files; usually a module like beautifulsoup4, and pointers to any other modules it needs to run. These other modules will also be installed if you don't already have them.

This eliminates the problem of installing a specialty module, and then trying to figure out what other dozen or so modules also need to be installed for it to work. Everything needed is included in one 'package'. Nice.


About the 'Permission denied' error on Linux systems:

When you install some packages, you might get the error “Could not install packages due to an Environment Error: Permission denied”. This usually means that in order to install or upgrade this particular package, you need admin rights. OK, not a problem. Just uses the command prefix 'sudo' and enter your password when prompted.

sudo pip install beautifulsoup4

Where to find the documentation:

Looking for more info? There are actually two very good places for find it.
On the PyPA website, there is 'pip - The Python Package Installer'. https://pip.pypa.io/en/stable/
And on the Python.org website, there is 'Installing Python Modules'. https://docs.python.org/3/installing/index.html

Last updated: 2019-02-16

Written by Joe Roten

Computer tech, Graphic Artist, Photographer, Writer, Educator, Programmer, Jack of many trades, Social gadfly, and Scholar without portfolio. http://www.gsw7.net/joe/

Written by Joe Roten

http://www.gsw7.net/joe/

As always

The information on my website is FREE.
But donations to help pay for Coffee and Beer are always welcomed.
Thanks.