Nearby lessons
155 of 159Python - PIP
- 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:
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.
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.
Uninstalling a Package
To remove an installed package, use the pip uninstall command.
Listing Installed Packages
To see all packages installed in the current environment, use the pip list command.
Showing Information About a Package
To get detailed information about an installed package, use the pip show command.
Upgrading a Package
To upgrade an already installed package to its latest version, use the --upgrade option.
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.
The requirements.txt File
The output of pip freeze is usually saved into a file named requirements.txt.
Installing All Packages from requirements.txt
Another developer can install all the same packages at once using the -r option.
Explanation
This is a very common workflow in real projects.
- After setting up a project, run
pip freeze > requirements.txtto save the exact dependencies. - Share the
requirements.txtfile 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 |
- 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