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/
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)
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.
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 beautifulsoup4To get a list of the packages that have already been installed with PIP, the command is:
pip listIf you would like to save the list of installed packages to a file, the command is:
pip list > MyFile.txtYou 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 )
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 beautifulsoup4Note 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.
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.
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
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