Installing Python Modules Using pip

Installing Python Modules Using pip

Editor: AI for Humanities Editorial Team

There are many ways to install external Python libraries; this tutorial will introduce one of the most common methods: using pip.

Course Objectives

This course will show you how to download and install Python modules. There are many methods to install external modules, but in this course, we will use a program calledpip that can be easily installed on mac/linux and windows. Starting from Python 2.7.9, pip is installed by default.This tutorial will be helpful for anyone using older versions of Python (which are still quite common).

Introduction to Modules

One of the advantages of using Python is that it has many excellent code libraries that can be easily accessed, saving you a lot of coding work or making certain tasks (like creating CSV files or scraping web pages) simpler.

When you search for solutions to problems on Google, you often find example code that uses libraries you have never heard of. Don’t be intimidated by these!

Once these libraries are installed on your computer, you just need to import them at the beginning of your code; you can import any number of libraries, for example:

import csv
import requests
import kmlwriter
import pprint

For new Python users, downloading and installing external modules for the first time can be a bit daunting. There are many methods to install them (which adds to the confusion); this course introduces the simplest and most common method.

Our goal is to install a software tool on the computer that can automatically download and install Python modules.

We will use a program calledpip to do this.

Note: Starting from Python 3.4, pip is included in the standard installation. You may not have this version for various reasons (or higher), and if so, these instructions will be helpful for you.

Installation on Mac and Linux

According to pip’s documentation, we can download a Python script to install pip. On Mac or Linux, you can use the command line to download the pip installation script using the curl command:

curl -O https://bootstrap.pypa.io/get-pip.py

After downloading theget-pip.py file, you need to execute it with the Python interpreter.

If you run it directly:

python get-pip.py

The script will likely fail because it does not have permission to update certain system directories, which are protected by default to prevent arbitrary modifications to important files and potential virus infections.

In this case—and in all cases where you need to allow a trusted script to write to system folders—you can prepend the command with sudo (short for “Super User DO”):

sudo python get-pip.py

Installation on Windows

On Windows, the easiest way to install pip is also to use a Python program called get-pip.py that you can download from the specified link (https://bootstrap.pypa.io/get-pip.py).

When you open this link, you may be intimidated by the full screen of code. Don’t worry. Just save the page with your browser, keeping the default filename asget-pip.py. It is best to save this file in the Python installation directory for easy access.

After saving the file, you need to run it, and there are two ways to do this:

If you prefer a graphical interface, you can right-click onget-pip.py file, select “Open with,” and then choose any Python interpreter.

If you prefer the command line, first navigate to the directory where both Python and get-pip.py are located. For example, suppose this directory is python27:

C:\>cd python27

After entering the directory, run the command:

python get-pip.py

If you need more information or encounter strange error messages, you can check the relevant pages on StackOverflow, which are updated regularly.

Installing Python Modules

Now that you have pip, installing Python modules is easy because it does all the work for you. When you find a module you want to use, the documentation or installation instructions usually include the required pip command, for example:

pip install requests
pip install beautifulsoup4
pip install simplekml

Remember, for the same reasons explained above (on Mac or Linux systems, but not Windows), you may need to run pip with sudo, for example:

sudo pip install requests

Sometimes, especially on Windows, you may find that using the -m flag is more helpful (to let Python find the pip module), for example:

python -m pip install XXX

Happy installing!

Disclaimer

This article is a translation of the following work:

Fred Gibbs

Installing Python Modules with pip

Original link:https://programminghistorian.org/en/lessons/installing-python-modules-pip

This article is adapted under CC BY 4.0 International License

END

Installing Python Modules Using pip

Humanities

Humanities

Exploring the Intersection of AI and the Humanities

You can’t use ‘macro parameter character #’ in math mode

Leave a Comment