Python中pip freeze和pip list的区别

Python中pip freeze和pip list的区别

The difference between pip freeze and pip list in Python

pip freeze之间的区别pip list在于:

  • pip freeze以可用于生成requirements.txt文件的需求格式输出用户安装的包。
  • pip list输出所有已安装的包,包括可编辑的。

pip freeze用于生成requirements.txt文件

您将最常使用该pip freeze命令来生成
requirements.txt文件。

pip freeze > requirements.txt pip3 freeze > requirements.txt
该命令将 的输出重定向pip freeze到一个名为. requirements.txt

requirements.txt文件可用于安装包和重新创建环境。

pip install -r requirements.txt pip3 install -r requirements.txt

requirements.txt只能从
pip freeze命令的输出生成文件,而不能从pip list.

点冻结输出

pip包的名称和特定版本由两个等号分隔,这是用于安装特定版本包的语法。

pip install requests==2.28.0 pip3 install requests==2.28.0

pip list命令以不同的格式输出包

pip list命令还显示已安装的包及其版本,但格式不同。

点列表输出

pip freeze命令不输出 pip 默认依赖的包,例如wheelsetuptools,而pip list命令输出。

You can use the --all option if you need to include the
pip,
setuptools, distrubute and
wheel packages in the
output of pip freeze.

shell
pip freeze --all

The pip list outputs all installed packages, including editables, whereas
pip freeze shows the packages the user installed and their dependencies.

If you don’t use a virtual environment, it is recommended to create one as they
make management of packages much easier.

shell
# 👇️ optionally store currently installed packages in a file pip freeze > requirements.txt pip3 freeze > requirements.txt # 👇️ use correct version of Python when creating VENV python -m venv venv # 👇️ activate on Unix or MacOS source venv/bin/activate # 👇️ activate on Windows (cmd.exe) venv\Scripts\activate.bat # 👇️ activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # 👇️ Upgrade pip pip install --upgrade pip # 👇️ install package in virtual environment pip install requests # 👇️ optionally install packages from a `requirements.txt` file pip install -r requirements.txt pip3 install -r requirements.txt

If the python -m venv venv command doesn’t work, try one of the following
commands:

  • python3 -m venv venv
  • py -m venv venv

Make sure to use the correct activation command depending on your operating
system.

Your virtual environment will use the version of Python that was used to create it.

Note that the name requirements.txt is just a convention. You can use any to
store the output of the pip freeze command and you can have as many
requirements files as necessary.

# Additional Resources

You can learn more about the related topics by checking out the following
tutorials: