Use requirement.txt to install dependencies in python environment

In Python projects, we usually use the requirement.txt file to record the third-party libraries that the project depends on, so that it is easier to install these dependencies when deploying the project on other machines. When using requirement.txt to install dependencies, you can follow the steps below:

  1. install pip

To install dependencies using requirement.txt, you first need to have pip installed on your machine. pip is a third-party library management tool officially recommended by Python, which is used to install, upgrade, and uninstall third-party libraries in the Python environment.

Enter the following command in the terminal to install pip:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
  1. Enter the project directory

Before installing dependencies, you need to enter your project directory:

cd your_project_directory/
  1. Create and edit the requirement.txt file

Create a requirement.txt file in the project directory, and list all dependent libraries and their version numbers in it, for example:

flask==2.0.0
pandas==1.3.3
numpy==1.21.2

The commands to create and edit files are as follows:

touch requirement.txt
vim requirement.txt
  1. install dependencies

In the project directory, run the following command to install dependencies:

pip install -r requirement.txt

Use the -r parameter to specify the requirement.txt file, and pip will automatically install all dependent libraries and their dependencies.

It should be noted that if your Python project uses a virtual environment, you need to activate the virtual environment before installing. For example:

source venv/bin/activate   # 激活虚拟环境
pip install -r requirement.txt   # 安装依赖
deactivate   # 退出虚拟环境

After installing dependencies, you can use these third-party libraries in your project.

Guess you like

Origin blog.csdn.net/hbqjzx/article/details/131138022