How to Install Libraries and Enable the pip Installer in Python

Python comes with a bunch of standard modules. My favorites are shutil, glob, datetime, time, os (operating system), re (regular expressions) and webbrowser. The standard library is loaded.

Inevitably, you’ll want to install new libraries from Python’s rich ecosystem of external modules. Enter pip, python’s handy package manager and people’s champion.

This post will teach you some Python history, show how to install pandas, and help you troubleshoot problems if it’s not working. You’ll find Windows and Linux commands for venv setup (recommended). With pip, you’ll feel like Neo when installing new modules. Any skill is at your fingertips. It’s like learning kung fu. There’s probably a library for that!

i know kung fu

Before We Start, You Should Know

Python 2 reached end of life on January 1st, 2020. Python 2 has officially been sunset.

Python comes with pip now, no setup is required. But certain versions such as Python 3.2 or the Python 2.7 that came stock on my improbably still functioning 2008 black Macbook, for example, may not have it installed.

According to the pip installation documentation:

“pip is already installed if you are using Python 2 >=2.7.9 or Python 3 >=3.4 downloaded from python.org or if you are working in a Virtual Environment created by virtualenv or pyvenv.

Enter This in Your Terminal

python -m pip install pandas

Pandas is a super useful library for wrangling spreadsheet data, AKA “tabular” data. If successful, you should see activity that looks similar to the below screenshot, where I am installing openpyxl, an additional Python Excel library you’ll likely want. You are good to go! This is the part where you get to feel like Neo! See Installing Python Modules in the Python Documentation for more detailed instructions.

neo_pip

Psst! To view all your installed libraries, enter:

pip list

You can list your outdated packages with the –outdated argument:

pip list --outdated

Supplementary Resources

Congrats on figuring out how to install packages with pip, have fun! 😀

Having trouble? Try using the ensurepip command.

This command will install and upgrade pip to the newest version. New in Python 3.4:

python -m ensurepip --upgrade

“The ensurepip package provides support for bootstrapping the pip installer into an existing Python installation or virtual environment. This bootstrapping approach reflects the fact that pip is an independent project with its own release cycle, and the latest available stable version is bundled with maintenance and feature releases of the CPython reference interpreter.”

ensurepip Python Documentation

Sidebar: NEW IN 2020, the pip resolver

Got library version conflict issues? If so, you may want to try the pip dependency resolver, newly released by the pip team.

Here’s an example of installing a library and using pip’s “dependency resolver” flag:

pip install pandas --use-feature=2020-resolver

If you’re still having problems, here are some things to consider.

Ensure Python can run in any directory on Windows.

To check if you can run Python anywhere: open command prompt, type python and hit enter. Type quit() to exit the interpreter, then use the cd command to change the directory and type python again. If the python interpreter runs in a few different file path locations, you’re good to go. If this doesn’t work, you may need to do the following:

system_vars

  1. Go to “Advanced System Settings”.
  2. Select “Environment Variables”.
  3. Under “System Variables”, edit your “path” variable.
  4. Paste in c:\Python38; and click “ok”, then “apply”. Make sure you edit this to match your Python version if it’s not Python 3.8.

You should follow best practice and create a virtual environment before installing libraries. venv or virtualenv will help you out. To create with venv:

python3 -m venv add_env_name_here

After your environment is created, activate it with the first command below, then install a library on Ubuntu Linux:

  1. cd add_env_path_here/bin & source activate
  2. python -m pip install pandas

Alternatively, on Windows computers:

  1. cd add_env_path_here\scripts & activate
  2. python -m pip install pandas

Know your OS.

If you’re interested in installing pip on Linux, try here. For Mac, try here. Windows, Mac and Linux sometimes use different prefixes (e.g. python, py, python3) to run a python script. Which leads me to my next point:

Getting the prefix right can be tricky.

Since this was written, I moved to Python 3.8. When I first moved from python 2 to 3 on Windows, I somehow accidentally configured the following behavior: entering python some_program.py ran a .py file with Python 2. Whereas, entering py some_program.py ran a .py file with Python 3.

In the install command, the prefix is a reference to your Python executable. You may just need to alter your prefix to call it correctly. Here are some to try in place of “python”. Observe what happens when you run these command variations. Good luck!

python3 -m pip install pandas

python3.8 -m pip install pandas

py -m pip install pandas

pip3 install pandas

How to Manually Enable the pip Installer

The rest of this post may be useful to you if you are:

  1. Working on legacy Python 2 code or Python 3.3 or lower for which pip is not installed.
  2. Seeking to fix a faulty pip install that is not working properly.
  3. Curious to know how to manually set up pip.

Assumes Python is already installed. If you’re running Windows 10, I found it easy to install Python from the Windows store.

Download the get-pip.py file.

Go to the link, right click the page and “Save As” a .py file to download. Then place the file where you want to access it. I placed mine in C:\Python27\Lib\site-packages

You could also download the file with curl:

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

If you are not sure where your site-packages folder is, type python -m site into command prompt for file path ideas.

Run the get-pip.py file.

Using command prompt’s cd command with a Windows “&” operator to run the python file in a Windows command prompt:

cd c:\Python27\Lib\site-packages & python get-pip.py

Or Linux terminal:

cd /Python27/Lib/site-packages && python get-pip.py

You should see some activity in command prompt that shows installation/updating of “setup” and “wheel”. When it finishes, you have installed pip.

Type into command prompt at the same location:

python -m pip install requests 

This should install the Requests module into your Python libraries. Requests is an http module which is highly regarded almost universally by the Python community.

Thanks for reading! Check out these other posts with pip installed library examples:

gooey – GUI library

tweepy – Twitter library

Plotting Datasets Using Jupyter, Pandas and Matplotlib

Integrating MySQL with Flask, pandas and pythonanywhere

8 thoughts on “How to Install Libraries and Enable the pip Installer in Python

Leave a Reply