Skip to main content

Python Install Pip

Learn how to install libraries and packages with pip. Install scikit-learn and other data science requirements & dependencies for your code today!
Aug 2020  · 4 min read

pip is a standard package manager used to install and maintain packages for Python. The Python standard library comes with a collection of built-in functions and built-in packages.

Data science packages like scikit-learn and statsmodel are NOT part of the Python standard library. They can be installed through pip, the standard package manager for Python, via the command line.

Pip Documentation

Pip has a variety of commands and option flags designed to manage Python packages.

Pip commands

You can print the pip version the same way you print the Python version. It is important that the pip version is compatible with the Python version. Here we see that pip 19.1.1 is compatible with Python 3.5.2.

Pip and Python version

Upgrading Pip

If pip is giving you an upgrade warning, you can upgrade using pip itself:

Upgrading pip

Viewing a Pip List

Before you make any installs, it is a good idea to see what is already installed. You can use pip list in the command line, and it will display the Python packages in your current working environment in alphabetical order.

Pip list

Installing the scikit-learn Package

In the following example, you will learn how you can install the scikit-learn package, which will install the other necessary dependencies.

Install scikit-learn Package

You may notice from the logs that more then the scikit-learn package is being installed. This is because pip will install any other packages that scikit-learn depends on. These other packages are called dependencies.

Installing a Specific Package Version

pip will always install the latest version, so if you wish to install an older version of scikit-learn, all you need to do is specify it in the installation statement use a double equal sign:

specifying installation statement

Upgrading Packages

If the package you are looking to use is already installed but simply out of date. You can update the package in a similar way we upgraded pip above.

Updating Pip package

This upgrade will also upgrade any necessary dependency packages as well, automatically.

Installing and Upgrading the scikit-learn and statsmodel Package

To pip install more than one Python package, the packages can be listed in line with the same pip install command as long as they are separated with spaces. Here we are installing both scikit-learn and the statsmodel package in one line of code.

Upgrading statsmodel package

You can also upgrade multiple packages in one line of code.

Upgrading scikit-learn package

Installing Packages With requirements.txt

If you want to install many packages at once, you can save them one package per line in a text file called requirements.txt. If we preview the file, it looks like this:

Installing packages with requirements.txt

It is conventional for Python package developers to create a requirements.txt file in their Github repositories listing all dependencies for pip to find and install.

The -r option flag in pip allows pip install to install packages from the file specified after the option flag. Keep in mind that naming this file requirements.txt is conventional but not required.

Using our examples, pip install -r requirements.txt will have the same effect as pip install scikit-learn statsmodel. Typing out each package could get messy if you needed to install ten packages. Using the requirements.txt file is much cleaner.

Using requirements.txt for cleaner installations

Interactive Example of Installing Python Dependencies

In the following example, you will work through the setup process for making sure your Python environment has the proper library dependencies installed prior to executing a Python model script.

You will instantiate the requirements.txt document and add the scikit-learn library to the requirements.txt file.

# Add scikit-learn to the requirements.txt file
echo "scikit-learn" > requirements.txt

# Preview file content
cat requirements.txt

When we run the code above, it produces the following result:

$ # Add scikit-learn to the requirements.txt file
$ echo "scikit-learn" > requirements.txt
$ # Preview file content
$ cat requirements.txt

Try it for yourself.

To learn more about using pip in the command line, please see this video from our course Data Processing in Shell.

This content is taken from DataCamp’s Data Processing in Shell course by Susan Sun.

Introduction to Python

4 hours
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course

Intermediate Python

4 hours
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Introduction to Shell

4 hours
The Unix command line helps users combine existing programs in new ways, automate repetitive tasks, and run programs on clusters and clouds.
See all coursesRight Arrow

The 23 Top Python Interview Questions & Answers

Essential Python interview questions with examples for job seekers, final-year students, and data professionals.
Abid Ali Awan's photo

Abid Ali Awan

22 min

Working with Dates and Times in Python Cheat Sheet

Working with dates and times is essential when manipulating data in Python. Learn the basics of working with datetime data in this cheat sheet.
DataCamp Team's photo

DataCamp Team

Plotly Express Cheat Sheet

Plotly is one of the most widely used data visualization packages in Python. Learn more about it in this cheat sheet.
DataCamp Team's photo

DataCamp Team

0 min

Getting started with Python cheat sheet

Python is the most popular programming language in data science. Use this cheat sheet to jumpstart your Python learning journey.
DataCamp Team's photo

DataCamp Team

8 min

Python pandas tutorial: The ultimate guide for beginners

Are you ready to begin your pandas journey? Here’s a step-by-step guide on how to get started. [Updated November 2022]
Vidhi Chugh's photo

Vidhi Chugh

15 min

Python Iterators and Generators Tutorial

Explore the difference between Python Iterators and Generators and learn which are the best to use in various situations.
Kurtis Pykes 's photo

Kurtis Pykes

10 min

See MoreSee More