Nearby lessons

155 of 159

Python - PIP

📌 What You Will Learn
  • Understand what PIP is and why it is needed
  • Install a package using pip install
  • Uninstall a package using pip uninstall
  • List installed packages with pip list
  • Share dependencies using pip freeze and requirements.txt

What is PIP?

PIP stands for Package Installer for Python.

It is a package management tool that allows us to install, uninstall, and manage third-party packages written in Python.

These packages are downloaded from the Python Package Index (PyPI), the official online repository of Python packages.

Why Do We Need PIP?

Python comes with a standard library, but many useful features are provided by third-party packages.

For example, to work with an Oracle database, we need the cx_Oracle package. To work with a MySQL database, we need mysql-connector.

Without PIP, installing these packages would require downloading files manually. PIP automates this process.

Checking the PIP Version

To check whether PIP is installed and which version is available, run the following command in the command prompt:

🐍Code Cell
1pip --version
Output
pip 24.0 from C:\Python\Lib\site-packages\pip (python 3.12)

Installing a Package

To install a package, use the pip install command followed by the package name.

The command should be run from a normal command prompt, not from the Python console.

🐍Code Cell
1pip install cx_Oracle
Output
Collecting cx_Oracle
  Downloading cx_Oracle-8.3.0-cp312-cp36m-win32.whl (200kB)
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-8.3.0

Explanation

PIP connects to PyPI, downloads the requested package, and installs it automatically.

The output shows the package being collected and downloaded, followed by a success message at the end.

Installing a Specific Version

To install a particular version of a package, use == followed by the version number.

🐍Code Cell
1pip install requests==2.31.0
Output
No output captured.

Uninstalling a Package

To remove an installed package, use the pip uninstall command.

🐍Code Cell
1pip uninstall requests
Output
Found existing installation: requests 2.31.0
Uninstalling requests-2.31.0:
  Successfully uninstalled requests-2.31.0

Listing Installed Packages

To see all packages installed in the current environment, use the pip list command.

🐍Code Cell
1pip list
Output
Package    Version
---------- -------
pip        24.0
setuptools 65.5.0
requests   2.31.0
cx_Oracle  8.3.0

Showing Information About a Package

To get detailed information about an installed package, use the pip show command.

🐍Code Cell
1pip show requests
Output
Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Location: C:\Python\Lib\site-packages
Requires: certifi, charset-normalizer, idna, urllib3

Upgrading a Package

To upgrade an already installed package to its latest version, use the --upgrade option.

🐍Code Cell
1pip install --upgrade requests
Output
No output captured.

Freezing Dependencies with pip freeze

To share the list of dependencies with other developers, we can generate a file that lists every installed package and its version.

🐍Code Cell
1pip freeze
Output
certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.6
requests==2.31.0
urllib3==2.2.1

The requirements.txt File

The output of pip freeze is usually saved into a file named requirements.txt.

🐍Code Cell
1pip freeze > requirements.txt
Output
No output captured.

Installing All Packages from requirements.txt

Another developer can install all the same packages at once using the -r option.

🐍Code Cell
1pip install -r requirements.txt
Output
No output captured.

Explanation

This is a very common workflow in real projects.

  • After setting up a project, run pip freeze > requirements.txt to save the exact dependencies.
  • Share the requirements.txt file with the team.
  • Anyone can recreate the same environment by running pip install -r requirements.txt.

PIP Commands at a Glance

Command Purpose
pip install package_name Install a package
pip uninstall package_name Remove a package
pip list Show all installed packages
pip show package_name Show information about a package
pip freeze List packages with exact versions
pip install -r requirements.txt Install all packages from a file
📝 Key Takeaways
  • PIP stands for Package Installer for Python
  • pip install package_name downloads and installs a package from PyPI
  • pip uninstall package_name removes an installed package
  • pip list shows all packages installed in the current environment
  • pip freeze outputs all packages and versions, which can be saved to requirements.txt

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10