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!
First, Some Python Version Caveats + History
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.
In December 2021, Python 3.6 reached “end of life phase”.
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.
Psst! To view all your installed libraries, enter:
You can list your outdated packages with the –outdated argument:
pip list --outdated
- I recommend taking a look at this list of 20 modules to get started.
- Here is another more comprehensive list of libraries you can now install.
- PyPI, the Python Package Index is the primary repository that houses a variety of libraries that are typically downloadable by pip.
- Swing by the pip documentation Quickstart and User Guide to learn some helpful commands. I also like this pip cheat sheet from opensource.com.
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
ensurepippackage provides support for bootstrapping the
pipinstaller into an existing Python installation or virtual environment. This bootstrapping approach reflects the fact that
pipis 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.”
Sidebar: NEW IN 2020, the pip resolver
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:
- Go to “Advanced System Settings”.
- Select “Environment Variables”.
- Under “System Variables”, edit your “path” variable.
- 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.
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:
cd add_env_path_here/bin & source activate
python -m pip install pandas
Alternatively, on Windows computers:
cd add_env_path_here\scripts & activate
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:
- Working on legacy Python 2 code or Python 3.3 or lower for which pip is not installed.
- Seeking to fix a faulty pip install that is not working properly.
- Curious to know how to manually set up pip.
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 siteinto 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