Land your first dev job — 100% money-back guarantee.
Continue

Pip and Package Management in Python

Learn how to install, manage, and remove external Python packages using pip. Understand virtual environments, requirements files, and best practices for dependency management.

What is pip?

pip is Python's official package manager. It allows you to install external libraries (also called packages) that are not included in the Python standard library. These packages help you build applications faster by reusing code written by others.

Check if pip is Installed

Before installing packages, you should verify that pip is installed on your system.

Installing Python Packages

You can install third-party libraries from the Python Package Index (PyPI). One popular example is the 'requests' library, which helps you make HTTP requests.

Install a Package

Install the requests library using pip.

Using the Installed Package

After installation, you can import and use the package in your Python code.

Upgrading and Uninstalling Packages

Over time, packages receive updates. You may need to upgrade them or remove them completely.

Upgrade a Package

Upgrade an installed package to its latest version.

Uninstall a Package

Remove a package from your system.

Virtual Environments

In real projects, different applications may require different versions of the same library. Virtual environments allow you to create isolated Python environments for each project.

Create a Virtual Environment

Use the built-in venv module to create a virtual environment.

Activate the Virtual Environment

Activate the environment before installing packages.

Requirements File

A requirements.txt file lists all the packages your project depends on. This makes it easy to recreate the environment on another machine.

Generate requirements.txt

Export all installed packages into a file.

Install from requirements.txt

Install all dependencies listed in a requirements file.