SoFunction
Updated on 2025-03-10

Detailed explanation of how to use pip in python

introduction

pip is Python's default package management tool and is installed with the Python version. It makes installing and managing Python packages very easy. This article will introduce the basic usage methods, common commands and code examples of pip.

1. Check if pip is installed

First, make sure that pip is already installed on your system. You can check it by:

Sample code:

pip --version

If the version information is displayed, it meanspipInstalled; otherwise, manual installation may be requiredpip

2. Upgrade pip

To make sure you are using the latest versionpip, it is recommended to upgrade regularlypip

Sample code

pip install --upgrade pip

3. Installation package

usepip installCommand to install Python package. It can be installed directly from PyPI (Python Package Index), or from a local file or a specific URL.

Sample code

# Install packages from PyPIpip install requests
 
# Install package from local filepip install ./downloads/requests-2.26.0-py2.
 
# Install packages from specific URLspip install git+/psf/

4. Uninstall the package

usepip uninstallCommand to uninstall Python packages that are no longer needed.

Sample code

pip uninstall requests

5. List installed packages

usepip listThe command lists all packages and their versions that have been installed in the current environment.

Sample code

pip list

If you only want to view expired packages, you can use the following command:

Sample code

pip list --outdated

6. View package information

usepip showCommand to view the detailed information of an installed package, such as version, author, summary, etc.

Sample code

pip show requests

7. Freeze dependencies

usepip freezeThe command outputs all installed packages and their versions in the current environment, which are usually used to generatedocument.

Sample code

pip freeze > 

8. Install dependencies

usepip install -rCommand basedThe file installs the specified version of the dependency package.

Sample code

pip install -r 

9. Create a virtual environment

To isolate project dependencies, it is recommended to use a virtual environment. Can be usedvenvModules create virtual environments and use thempipManagement package.

Sample code

# Create a virtual environmentpython -m venv myenv
 
# Activate the virtual environment (Windows)myenv\Scripts\activate
 
# Activate the virtual environment (macOS/Linux)source myenv/bin/activate
 
# Exit the virtual environmentdeactivate

10. Search with pip

usepip searchCommands can search for specific packages on PyPI. However, it should be noted thatpip searchIt has been removed in newer versions.

11. Install a specific version of the package

Sometimes you need to install a specific version of the package, you can use the equal sign (==) to specify the version number.

Sample code

pip install requests==2.26.0

12. Install multiple packages

Multiple packages can be installed in a single command, separated by spaces.

Sample code

pip install requests beautifulsoup4 numpy

This is the end of this article about the detailed explanation of how to use pip in python. For more related content on how to use pip in python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!