Concepts / Importing Libraries in Python

Importing Libraries in Python

The Python Package Index (PyPI) is a repository containing thousands of open source Python libraries.

  • 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.

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

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.

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

What pip Does

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.

library source forPyPIstores librariespipretrieves and installs
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

Tracing One Library Request

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

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

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

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

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

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

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

pip startslibrary foundlibrary retrievedLibrary requestthrough pipConnect to PyPIfind the libraryDownload libraryfrom PyPIInstall libraryon local machine
What happens after pip requests a Python library from PyPI?
library requestedinstalls libraryavailable for usePyPIlibrary storedpipretrieves and installsLocal machinelibrary installedPython projectlibrary available locally
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.

library retrieved bylibrary installed intomakes library available toPyPIshared library repositorypipcommand-line toolPython environmentlocal machineDeveloper projectreuses library code
How does a library move through the repository, installation tool, and developer environment?

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.

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

  • Treating PyPI and pip as interchangeable names

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

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

  • Stopping at library availability on PyPI

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

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

  • Recreating existing functionality unnecessarily

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

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

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.

Answering the Workflow Question

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

Separate storage from installation: PyPI is the repository that contains the library.

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

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

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

Key Takeaways

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

Key Takeaways

  • 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.