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 meanspip
Installed; 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 install
Command 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 uninstall
Command to uninstall Python packages that are no longer needed.
Sample code:
pip uninstall requests
5. List installed packages
usepip list
The 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 show
Command to view the detailed information of an installed package, such as version, author, summary, etc.
Sample code:
pip show requests
7. Freeze dependencies
usepip freeze
The 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 -r
Command 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 usedvenv
Modules create virtual environments and use thempip
Management 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 search
Commands can search for specific packages on PyPI. However, it should be noted thatpip search
It 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!