Home  >  Article  >  Backend Development  >  Starting from scratch: Mastering installing Python libraries using pip

Starting from scratch: Mastering installing Python libraries using pip

王林
王林Original
2024-01-04 15:59:591158browse

Starting from scratch: Mastering installing Python libraries using pip

Start from scratch: Learn to use pip to install Python packages, you need specific code examples

Introduction: Python is a popular programming language with rich Third-party libraries and packages. In order to better develop and use Python, it is an important step to learn to use pip to install Python packages. This article will start from scratch, detail how to use pip to install Python packages, and provide specific code examples.

1. What is pip?
pip is a package management tool for Python, which can easily help us install, upgrade and uninstall Python packages. pip is easy to use, powerful, and supports automatic resolution of dependencies. It is an indispensable tool for Python developers.

2. Install pip
Before starting to use pip, we first need to make sure that we have installed Python correctly. pip is usually installed with Python, but manual installation may be required in some cases.

  1. Windows system:
    On Windows system, we can install pip by downloading and running the get-pip.py file. First, we need to open https://bootstrap.pypa.io/get-pip.py in the browser and save the file locally. Then, we open the command prompt (Win R, enter cmd and press Enter), switch to the directory where get-pip.py is located, and execute the following command:
python get-pip.py
  1. Mac or Linux System:
    On Mac or Linux systems, we can use the terminal to install pip. First, we open the terminal and execute the following command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Then, we execute the following command to install pip:

python get-pip.py

3. Use pip to install the Python package
Once We successfully installed pip and can start using it to install Python packages. The following are some commonly used pip command examples:

  1. Installation package:
    We can use the following command to install a certain Python package:
pip install package_name

For example, we want to To install the numpy package, you can execute the following command:

pip install numpy
  1. Specify version:
    If we want to install a specific version of the package, we can use the following command:
pip install package_name==version

For example, if we want to install version 1.18.1 of numpy, we can execute the following command:

pip install numpy==1.18.1
  1. Upgrade package:
    We can use the following command to upgrade an installed package:
pip install --upgrade package_name

For example, we can execute the following command to upgrade the numpy package:

pip install --upgrade numpy
  1. Uninstall the package:
    If we want to uninstall an installed package, we can use The following command:
pip uninstall package_name

For example, if we want to uninstall the numpy package, we can execute the following command:

pip uninstall numpy

4. Resolve dependencies
pip has the function of automatically resolving dependencies. This means that when we install a package, pip will automatically install other packages that the package depends on. This can greatly simplify our work.

5. Common options of pip
Pip also has some common options that can help us use it better. The following are some examples of commonly used options:

  1. View installed packages:
    We can use the following command to view installed packages in the current environment:
pip list
  1. Find package:
    If we want to find whether a package already exists, we can use the following command:
pip search package_name

For example, if we want to find packages related to matplotlib, we can execute the following Command:

pip search matplotlib
  1. Specify the installation source:
    We can use the following command to specify the installation source:
pip install package_name --index-url=URL

where the URL is the installation source we want to use . For example, we can use Tsinghua University's pip image to install the numpy package and execute the following command:

pip install numpy --index-url=https://pypi.tuna.tsinghua.edu.cn/simple

6. Summary
This article starts from scratch, details how to use pip to install Python packages, and provides specific code examples. pip is an indispensable tool for Python developers. It is convenient, powerful, and supports automatic resolution of dependencies. By learning to use pip, we can better develop and use Python and improve our programming efficiency. I hope this article can help beginners better master the basic skills of using pip.

The above is the detailed content of Starting from scratch: Mastering installing Python libraries using pip. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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