Home > Backend Development > Python Tutorial > Detailed explanation of common pip commands in Python

Detailed explanation of common pip commands in Python

WBOY
Release: 2023-04-12 12:13:18
forward
1351 people have browsed it

The editor believes that most people who are familiar with Python must have heard of and used the pip tool, but their understanding of it may not be very thorough. Today I will introduce to you 10 A little tip on using pip, I believe it will be helpful for you to manage and use the standard library in Python in the future.

Installation

Of course, after Python 3.4 and Python 2.7.9, the installation package on the official website already comes with pip, and users can use it directly after installing Python. , if you use a virtual environment created by virtualenv or pyvenv, then pip is also installed by default.

If you need to install the pip package yourself, run the following command line in an environment where Python has been configured.

py -m ensurepip --upgrade
Copy after login

Another way is to download it from the official website ​​Download the get-pip.py script directly, and then run the python get-pip.py script directly

How to use

After installation, enter pip in the command line, Then press Enter, and the instructions shown in the picture below will appear:

Detailed explanation of common pip commands in Python

Upgrade

If you feel that your pip version is a bit low, think about it. To upgrade, enter the following command on the command line

pip install --upgrade pip
Copy after login

or

pip install -U pip
Copy after login

to install a certain version of the package

If you plan to use pip to install third-party packages , the following command line is used

pip install package-name
Copy after login

For example, we want to install a specified version of a third-party package, such as installing version 3.4.1 of matplotlib,

pip install matplotlib==3.4.1
Copy after login

Uninstall or update the package

If you plan to uninstall a certain package, the command line to enter is

pip uninstall package_name
Copy after login

and if you plan to update a certain package, the corresponding command line is

pip install --upgrade package_name# 或者是pip install -U package_name
Copy after login

View The information of a certain package can be viewed through the following command line.

pip show -f requests
Copy after login

output

Name: requests
Version: 2.24.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: c:userspc120pycharmprojectspythonproject1venvlibsite-packages
Requires: certifi, chardet, idna, urllib3
Required-by: etelemetry, gTTS, pandas-datareader, pandas-profiling, pyler, pywhatkit, pyxnat, streamlit, tushare, wikipedia, yfinance
Files:
requests-2.24.0.dist-infoDESCRIPTION.rst
requests-2.24.0.dist-infoINSTALLER
.......
Copy after login

View the package that needs to be upgraded

We need to check it Among these existing packages, which ones need to be upgraded, you can use the following command line to check,

pip list -o
Copy after login

output

PackageVersion Latest Type
---------- ------- ------ -----
docutils 0.15.20.18.1 wheel
PyYAML 5.4.1 6.0wheel
rsa4.7.2 4.8wheel
setuptools 56.0.062.1.0 wheel
Copy after login

Check compatibility issues

in When downloading and installing some standard libraries, you need to consider compatibility issues. The installation of some standard libraries may need to rely on other standard libraries, and there may be problems such as version conflicts. Let us first use the following command line to check whether there are conflicts. problem exists.

pip check package_name
Copy after login

Of course, if we do not specify which standard library it is, we will check whether there are version conflicts and other issues in all currently installed packages.

pip check
Copy after login

output

yfinance 0.1.70 has requirement requests>=2.26, but you have requests 2.24.0.
selenium 4.1.0 has requirement urllib3[secure]~=1.26, but you have urllib3 1.25.11.
Copy after login

Specify domestic sources to install

If we feel that the installation speed is a bit slow, we can specify domestic sources to install a certain package, such as

pip install -i https://pypi.douban.com/simple/ package_name
Copy after login

There are domestic sources

清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/ 
豆瓣:http://pypi.douban.com/simple/
Copy after login

Download the package but not install it

If we want to download a package to the specified path, the command line is as follows

pip download package_name -d "某个路径"
Copy after login

For example,

pip download requests -d "."
Copy after login

downloads the requests module and other dependent modules in the current directory.

Batch installation of software packages

When we see other people's projects, we usually include a requirements.txt file, which contains some third-party libraries that need to be used in Python projects.

Detailed explanation of common pip commands in Python

To generate this kind of txt file, you need to do this

pip freeze > requirements.txt
Copy after login

And if we need to install third-party libraries in batches, enter the following in the command line This command

pip install -r requirements.txt
Copy after login


The above is the detailed content of Detailed explanation of common pip commands in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template