Basic commands and usage of pip

Insert image description here

1. Introduction to pip

pip is a package manager for Python, similar to package managers in other programming languages, such as Ruby's gem or Node.js' npm. It helps you install, update and uninstall Python packages (libraries or tools) easily. You can think of pip as an app store where you can get the Python packages you need and install them into your Python environment.

2. pip basic commands

Installation package:

pip install package_name

This command will download and install the package you specify from PyPI (Python Package Index). For example, if you wanted to install an HTTP library called requests, you could run pip install requests.

Upgrade package:

pip install --upgrade package_name

This command will check if there are new versions of installed packages and upgrade them to the latest version. This is useful for keeping your package compatible with the latest versions.

Uninstall package:

pip uninstall package_name

This command will uninstall the package you specify. It will remove the package and its associated files from your Python environment.

List installed packages:

pip list

This command will display a list of all packages installed in your Python environment. You can view the names and version numbers of installed packages.
Insert image description here

View version information for a specific package:

pip show package_name

This command displays detailed information about a specific package, including its version number, installation path, dependencies, etc.
Insert image description here

Search package:

已经弃用的方式:

pip search package_name

Insert image description here

This command will search PyPI for the package you specify and return results related to it. This can help you find the correct name of the package you want to install or find similar packages.但是因为pip search服务的请求量激增,而开发新的搜索方式需要投入大量的人力物力,所以官方永久禁用XMLRPC搜索功能,具体见上图报错

However, we can download pip-searchit instead!
The specific download command is: pip install pip-search
Insert image description here
Then you can use it pip_search package_nameto query the package.
Insert image description here

Install a virtual environment:
Virtual environments allow you to create isolated environments within different Python projects, each of which can have its own dependencies and package versions. This is useful for managing dependencies of different projects. You can install and use a virtual environment using the following commands:

Install virtual environment tools:

pip install virtualenv

Create a virtual environment:

virtualenv venv_name

Insert image description here
From the information provided, we have successfully created a virtual environment using Python. This virtual environment is based on CPython 3.10.11 and is 64-bit. The virtual environment is created in the C:\Users\JiKangyi\test_venv directory.
The following packages and their versions are also installed in the virtual environment:

pip23.3.1
setuptools
69.0.2
wheel==0.42.0
In addition, this virtual environment supports multiple activation methods, including Bash, Batch, Fish, Nushell, PowerShell and Python.

At this time, the virtual environment is created successfully~

Activate virtual environment (on Unix/Linux/macOS):

source venv_name/bin/activate

Activate the virtual environment (on Windows):

.\ENV_DIR\Scripts\activate

Insert image description here

In a virtual environment, you can use the pip command to install and manage packages specific to that environment. When you are finished working in the virtual environment, you can exit the virtual environment using the following command:

On Unix/Linux/macOS: deactivate On Windows: venv_name\Scripts\deactivate.bat.

Delete the virtual environment:

# 删除 virtualenv
rm -r myenv  # Linux/Mac
# 或者
rmdir /s /q myenv  # Windows

Insert image description here

3. Advantages of using pip to manage Python packages:

Using pip to manage Python packages has many advantages, such as: automatic dependency resolution, convenient version control, quick installation and uninstallation of packages, etc. In addition, pip also provides extensive community support and rich documentation, making using and managing Python packages easier and more efficient.

The main advantages of using pip to manage Python packages are as follows:

  • Official recommendation: pip is the officially recommended tool for installing and managing Python packages. It is very convenient to use it to download and manage Python.
  • Rich functions: pip provides the functions of searching, downloading, installing, and uninstalling Python packages, making the management of Python packages simple and easy.
  • Solve dependency problems: When pip installs a package, it will automatically parse and install the dependencies of the package, avoiding the trouble of manually installing dependencies.
  • Version control: pip can easily manage different versions of Python packages, and specific versions of packages can be installed, updated, or uninstalled as needed.
  • Cross-platform support: pip can be used in different operating systems and Python environments, and has good cross-platform support capabilities.
  • Active community: pip has a large user group and active community, which means that when you encounter problems during use, you can quickly find solutions or get help from the community.
  • Integration with other tools: pip can be seamlessly integrated with other Python development tools and frameworks (such as IDEs, build systems, etc.) to improve development efficiency.
  • Security: pip will perform security checks when installing packages to ensure that the source of the installed packages is reliable and reduce security risks.
    In general, using pip to manage Python packages can greatly improve development efficiency and reduce maintenance costs. It is one of the preferred tools for Python developers.

Chapter review

Guess you like

Origin blog.csdn.net/m0_63007797/article/details/135119836