Working with External Code - Installing Packages using pip
17th October 2025 By Gururaj
blog

Python's strength lies in its extensive ecosystem of third-party packages, which allow developers to extend functionality without reinventing the wheel. These packages, created by the global Python community, cover everything from web development to data analysis, machine learning, and automation. To tap into this ecosystem, you use pip, the standard package installer for Python, which connects to the Python Package Index (PyPI), a massive online repository hosting over 400,000 packages. This makes it easy to find, install, and manage libraries tailored to your needs.

What is PyPI?

The Python Package Index (PyPI) is like an app store for Python libraries. It’s a centralized hub where developers publish their packages, complete with documentation, version histories, and installation instructions. Whether you need a library like requests for making HTTP requests, numpy for numerical computations, or django for building web applications, PyPI is the go-to source. You can browse PyPI at pypi.org, search for packages, and view details like usage examples and dependencies.

Getting Started with pip

pip is a command-line tool included with most Python installations (Python 3.4 and later). It communicates with PyPI to download and install packages. To check if pip is installed, open your terminal or command prompt and run:

bash
 
pip --version
 
 

This displays the installed pip version and the associated Python version. If it’s not installed, you can download it from pip.pypa.io.

Installing Your First Package

Let’s install a popular package, requests, which simplifies making HTTP requests. In your terminal, run:

bash
 
pip install requests
 
 

This command downloads the latest version of requests from PyPI and installs it in your Python environment. Once installed, you can use it in your Python code. For example:

python
 
import requests
response = requests.get('https://api.github.com')
print(response.json())
 
 

This code fetches data from GitHub’s API and prints it. Without pip, you’d need to manually write complex networking code—requests saves the day!

Managing Packages

pip does more than just install packages. Here are some key commands:

  • List installed packages: pip list shows all packages in your environment with their versions.
  • Upgrade a package: pip install --upgrade package_name updates a package to its latest version.
  • Uninstall a package: pip uninstall package_name removes a package.
  • Install a specific version: pip install package_name==version (e.g., pip install requests==2.28.1) installs a specific version for compatibility.
  • Show package details: pip show package_name displays metadata like version, dependencies, and author.

Virtual Environments

When working on multiple projects, you might need different packages or versions for each. Python’s virtual environments keep projects isolated. To create one, use:

bash
 
python -m venv myenv
 
 

Activate it:

  • Windows: myenv\Scripts\activate
  • macOS/Linux: source myenv/bin/activate

Once activated, pip install commands install packages only in that environment, avoiding conflicts. Deactivate with deactivate.

Finding Packages

To discover packages, search PyPI’s website or use:

bash
 
pip search search_term
 
 

(Note: pip search may be deprecated in some versions, so PyPI’s website is often better.) Read package documentation to ensure it suits your needs, and check for active maintenance to avoid outdated libraries.

Best Practices

  • Use requirements files: Store project dependencies in a requirements.txt file with pip freeze > requirements.txt. Install them elsewhere with pip install -r requirements.txt.
  • Stay secure: Only install trusted packages, as PyPI is open to community uploads. Check package popularity and reviews on PyPI.
  • Keep pip updated: Run pip install --upgrade pip regularly for the latest features and security fixes.

Example: Installing and Using numpy

Let’s try another package, numpy, for numerical computations. Install it:

bash
 
pip install numpy
 
 

Then, use it in Python:

python
 
import numpy as np
array = np.array([1, 2, 3, 4])
print(array * 2)  # Outputs: [2 4 6 8]
 
 

This multiplies each element by 2, showcasing numpy’s efficient array operations.

 

By mastering pip and PyPI, you unlock Python’s full potential, leveraging a global community’s work to build robust, efficient applications.