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. I’ve included Windows and Linux commands. 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!
Before We Start, You Should Know
Python 2 reached end of life on January 1st, 2020.
Back in 2018 when I first used pip, the Python community was still transitioning from Python 2 to 3. At the time, the pip installer was not shipping with all Python versions in use. Python 2 has officially been sunset.
Python comes with pip now, no setup is required.
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:
- 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! 😀
Did that work? If not, 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
Confused yet? It’s ok, I was too when I first sorted through all of this.
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.
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
How to Enable the pip Installer
Assumes Python is already installed.
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. Make sure your directory is set to using command prompt’s cd command. 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.