Concepts / Importing Libraries in Python

Importing Libraries in Python

Keep the roles separate: PyPI is the repository where libraries live, while pip is the command-line tool that retrieves and installs them.

  • Programming

From Shared Code to Local Use

Python developers do not need to create every useful feature from the beginning. They can use open source libraries that other developers have shared. The basic system behind this reuse has two parts: PyPI stores Python libraries, and pip retrieves and installs them on a local machine.

Key point

Keep the roles separate: PyPI is the repository where libraries live, while pip is the command-line tool that retrieves and installs them.

What do you think happens?

A library is available on PyPI, but pip has not installed it on your machine yet. Which part of the workflow is still incomplete?

  • The library has not yet been retrieved and installed locally

  • PyPI must be used as the installation tool

  • The library must be recreated from scratch

Reveal answer
Answer:

The library has not yet been retrieved and installed locally

PyPI provides the storage location, while pip retrieves a requested library from PyPI and installs it on the local machine.

What PyPI Contains

Definition

The Python Package Index, usually called PyPI, is a repository containing thousands of open source Python libraries.

Think of PyPI as a large shared collection rather than as a tool that performs installation. Its contents are open source Python libraries that developers can access for reuse. The important idea is that PyPI is the place where these libraries are stored and made available.

PyPIrepositoryPython libraryopen sourcePython libraryopen sourceMany librariesthousandscontainscontainscontains
What contains what: PyPI is a repository containing many open source Python libraries.

What pip Does

Definition

pip is a command-line tool used to install libraries from PyPI onto your local machine.

When you request a library through pip, pip connects to PyPI, finds the requested library, downloads it, and installs it on your local machine. This gives pip an active role in the workflow: it retrieves and installs what PyPI stores.

PyPIstores librariespipretrieves and installslibrary source for
What is the difference between the repository and the installation tool?
PartRole
PyPIRepository containing thousands of open source Python libraries
pipCommand-line tool that retrieves and installs libraries from PyPI
Local machineWhere pip installs the requested library

The three roles in the basic library installation system

The Installation Path

Worked example

Tracing One Library Request

A developer wants to use an existing open source Python library instead of recreating that functionality.

  1. 1
    Locate the source

    The library is available in the collection of open source Python libraries stored on PyPI.

  2. 2
    Request the library

    The developer uses pip, the command-line installation tool, to request the library.

  3. 3
    Retrieve the library

    pip connects to PyPI, finds the requested library, and downloads it.

  4. 4
    Install locally

    pip installs the downloaded library on the developer's local machine.

  5. 5
    Use the installed library

    The library is now available locally for use in the developer's project workflow.

Result

The complete path is PyPI stores the library, pip retrieves and installs it, and the local machine receives the installed library.

Library requestthrough pipConnect to PyPIfind the libraryDownload libraryfrom PyPIInstall libraryon local machinepip startslibrary foundlibrary retrieved
What happens after pip requests a Python library from PyPI?
PyPIlibrary storedpipretrieves and installsLocal machinelibrary installedPython projectlibrary available locallylibrary requestedinstalls libraryavailable for use
How does a library move from being available on PyPI to being available locally for a Python project?

The Connected System

PyPI and pip work together without being the same component. PyPI supplies a shared repository of libraries. pip accesses that repository and places a requested library on the local machine. Separating storage from installation makes it easier for developers to share code and for users to access it.

PyPIshared library repositorypipcommand-line toolPython environmentlocal machineDeveloper projectreuses library codelibrary retrieved bylibrary installed intomakes library available to
How does a library move through the repository, installation tool, and developer environment?
In the real world

Suppose a developer needs functionality already provided by an open source Python library. Rather than reinventing the wheel, the developer can rely on the PyPI and pip workflow: the library is stored on PyPI, pip retrieves and installs it, and the installed library becomes available on the local machine for the project.

Best practice

When explaining or troubleshooting library setup, name the component involved. Ask whether the library is present on PyPI, whether pip retrieved it, or whether pip installed it locally. These are different points in the workflow.

Mistakes Beginners Make

  • Common mistake

    Treating PyPI and pip as interchangeable names

    What it looks like

    Saying that PyPI installs a library or that pip is the repository

    Why it fails

    PyPI stores the libraries, while pip is the command-line tool that retrieves and installs them.

    Do this instead

    Describe PyPI as the repository and pip as the installation tool.

  • Common mistake

    Stopping at library availability on PyPI

    What it looks like

    Assuming that finding a library in PyPI means it is already installed locally

    Why it fails

    A library can be stored in the repository before pip retrieves and installs it on the local machine.

    Do this instead

    Trace the remaining steps: pip finds the library, downloads it, and installs it locally.

  • Common mistake

    Recreating existing functionality unnecessarily

    What it looks like

    Writing new code for functionality that an existing open source library can provide

    Why it fails

    The PyPI and pip ecosystem is intended to support sharing and reusing existing code.

    Do this instead

    First consider whether an appropriate open source library is available through PyPI.

Edge case

A library being present in PyPI describes its availability in the repository. It does not, by itself, describe the library's installation state on a particular local machine. The installation state changes when pip retrieves and installs the library locally.

Check Your Understanding

EASY

A teammate says, “The library is on PyPI, so pip is unnecessary.” Explain the error in this statement and describe the correct relationship between PyPI and pip.

Hints
  • Identify which component stores libraries.
  • Identify which component retrieves and installs them.
  • Mention the local machine in your explanation.
Worked example

Answering the Workflow Question

Explain why a library being listed in PyPI is not the same as having that library installed locally.

  1. 1
    Separate storage from installation

    PyPI is the repository that contains the library.

  2. 2
    Name the installation tool

    pip is the command-line tool used to install libraries from PyPI.

  3. 3
    Trace the transfer

    pip connects to PyPI, finds and downloads the requested library, and installs it on the local machine.

Result

The library can be available in PyPI before pip has retrieved and installed it locally.

Key Takeaways

  1. 1
    PyPI is a repository containing thousands of open source Python libraries.
  2. 2
    pip is a command-line tool used to install libraries from PyPI onto a local machine.
  3. 3
    PyPI stores libraries; pip retrieves and installs them.
  4. 4
    The workflow is: a library is stored on PyPI, pip finds and downloads it, and pip installs it locally.
  5. 5
    This system helps developers reuse existing code instead of reinventing the wheel.

At a glance

  • PyPI is the repository for thousands of open source Python libraries.
  • pip is the command-line tool that retrieves and installs libraries from PyPI.
  • PyPI and pip are complementary: one stores libraries and the other installs them.
  • A library's presence on PyPI does not mean it has already been installed on a local machine.
  • Together, PyPI and pip support sharing and reusing Python code.

Related concepts

More concepts starting with I