pip requirements.txt使用

在Python开发中,pip 是一个常用的包管理工具,用于安装和管理Python包。requirements.txt 文件通常用于记录项目依赖的Python包及其版本,以便在不同环境中快速安装相同的依赖包。以下是关于如何使用 piprequirements.txt 的一些基本指南:

创建 requirements.txt 文件

  1. 列出依赖包

    • 在项目目录中创建一个名为 requirements.txt 的文件。
    • 在文件中列出所有需要安装的Python包及其版本号,每行一个包。例如:
       

      Flask==1.1.2
      requests==2.25.1
      numpy>=1.19.2
  2. 从现有环境中导出依赖

    • 如果你已经有一个虚拟环境或项目环境,可以使用以下命令将当前环境中安装的包导出到 requirements.txt 文件中:
       

      bash

      pip freeze > requirements.txt
    • 这会列出所有已安装的包及其版本号。

安装依赖包

  1. 安装所有依赖

    • 在项目目录中,运行以下命令来安装 requirements.txt 文件中列出的所有包:
       

      bash

      pip install -r requirements.txt

 输入命令之后打印, 直到安装完成

PS E:\git_code\cursor-auto-free-main> pip install -r requirements.txt
Requirement already satisfied: altgraph==0.17.4 in d:\users\huawei\appdata\local\programs\python\python312\lib\site-packages (from -r requirements.txt (line 1)) (0.17.4)
Collecting certifi==2024.12.14 (from -r requirements.txt (line 2))
  Downloading certifi-2024.12.14-py3-none-any.whl.metadata (2.3 kB)
Requirement already satisfied: cffi==1.17.1 in d:\users\huawei\appdata\local\programs\python\python312\lib\site-packages (from -r requirements.txt (line 3)) (1.17.1)
Collecting charset-normalizer==3.4.1 (from -r requirements.txt (line 4))
  Downloading charset_normalizer-3.4.1-cp312-cp312-win_amd64.whl.metadata (36 kB)
Collecting click==8.1.8 (from -r requirements.txt (line 5))
  Downloading click-8.1.8-py3-none-any.whl.metadata (2.3 kB)
Requirement already satisfied: colorama==0.4.6 in d:\users\huawei\appdata\local\programs\python\python312\lib\site-packages (from -r requirements.txt (line 6)) (0.4.6)
Collecting cryptography==44.0.0 (from -r requirements.txt (line 7))
  Downloading cryptography-44.0.0-cp39-abi3-win_amd64.whl.metadata (5.7 kB)
Collecting cssselect==1.2.0 (from -r requirements.txt (line 8))
  Downloading cssselect-1.2.0-py2.py3-none-any.whl.metadata (2.2 kB)
Collecting DataRecorder==3.6.2 (from -r requirements.txt (line 9))
  Downloading DataRecorder-3.6.2-py3-none-any.whl.metadata (4.5 kB)
Collecting DownloadKit==2.0.7 (from -r requirements.txt (line 10))
  Downloading DownloadKit-2.0.7-py3-none-any.whl.metadata (1.7 kB)
Collecting DrissionPage==4.1.0.9 (from -r requirements.txt (line 11))
  Downloading DrissionPage-4.1.0.9-py3-none-any.whl.metadata (4.5 kB)
Collecting et_xmlfile==2.0.0 (from -r requirements.txt (line 12))
  Downloading et_xmlfile-2.0.0-py3-none-any.whl.metadata (2.7 kB)
Requirement already satisfied: filelock==3.16.1 in d:\users\huawei\appdata\local\programs\python\python312\lib\site-packages (from -r requirements.txt (line 13)) (3.16.1)
Collecting idna==3.10 (from -r requirements.txt (line 14))
  Downloading idna-3.10-py3-none-any.whl.metadata (10 kB)
Collecting lxml==5.3.0 (from -r requirements.txt (line 15))
  Downloading lxml-5.3.0-cp312-cp312-win_amd64.whl.metadata (3.9 kB)
Collecting macholib==1.16.3 (from -r requirements.txt (line 16))
  Downloading macholib-1.16.3-py2.py3-none-any.whl.metadata (11 kB)
Requirement already satisfied: openpyxl==3.1.5 in d:\users\huawei\appdata\local\programs\python\python312\lib\site-packages (from -r requirements.txt (line 17)) (3.1.5)
Collecting packaging==24.2 (from -r requirements.txt (line 18))
  Downloading packaging-24.2-py3-none-any.whl.metadata (3.2 kB)
Collecting pefile==2023.2.7 (from -r requirements.txt (line 19))
  Downloading pefile-2023.2.7-py3-none-any.whl.metadata (1.4 kB)
Collecting psutil==6.1.1 (from -r requirements.txt (line 20))
  Downloading psutil-6.1.1-cp37-abi3-win_amd64.whl.metadata (23 kB)
Requirement already satisfied: pycparser==2.22 in d:\users\huawei\appdata\local\programs\python\python312\lib\site-packages (from -r requirements.txt (line 21)) (2.22)
Collecting pycryptodome==3.21.0 (from -r requirements.txt (line 22))
  Downloading pycryptodome-3.21.0-cp36-abi3-win_amd64.whl.metadata (3.4 kB)
Collecting pyinstaller==6.11.1 (from -r requirements.txt (line 23))
  Downloading pyinstaller-6.11.1-py3-none-win_amd64.whl.metadata (8.3 kB)
Collecting pyinstaller-hooks-contrib==2024.11 (from -r requirements.txt (line 24))
  Downloading pyinstaller_hooks_contrib-2024.11-py3-none-any.whl.metadata (16 kB)
Collecting PyVirtualDisplay==3.0 (from -r requirements.txt (line 25))
  Downloading PyVirtualDisplay-3.0-py3-none-any.whl.metadata (943 bytes)
Requirement already satisfied: pywin32-ctypes==0.2.3 in d:\users\huawei\appdata\local\programs\python\python312\lib\site-packages (from -r requirements.txt (line 26)) (0.2.3)
Requirement already satisfied: requests==2.32.3 in d:\users\huawei\appdata\local\programs\python\python312\lib\site-packages (from -r requirements.txt (line 27)) (2.32.3)
Collecting requests-file==2.1.0 (from -r requirements.txt (line 28))
  Downloading requests_file-2.1.0-py2.py3-none-any.whl.metadata (1.7 kB)
Collecting setuptools==75.6.0 (from -r requirements.txt (line 29))
  Downloading setuptools-75.6.0-py3-none-any.whl.metadata (6.7 kB)
Collecting tldextract==5.1.3 (from -r requirements.txt (line 30))
  Downloading tldextract-5.1.3-py3-none-any.whl.metadata (11 kB)
Collecting urllib3==2.3.0 (from -r requirements.txt (line 31))
  Downloading urllib3-2.3.0-py3-none-any.whl.metadata (6.5 kB)
Collecting websocket-client==1.8.0 (from -r requirements.txt (line 32))
  Downloading websocket_client-1.8.0-py3-none-any.whl.metadata (8.0 kB)
Downloading certifi-2024.12.14-py3-none-any.whl (164 kB)
Downloading charset_normalizer-3.4.1-cp312-cp312-win_amd64.whl (102 kB)
Downloading click-8.1.8-py3-none-any.whl (98 kB)
Downloading cryptography-44.0.0-cp39-abi3-win_amd64.whl (3.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.2/3.2 MB 6.7 MB/s eta 0:00:00
Downloading cssselect-1.2.0-py2.py3-none-any.whl (18 kB)
Downloading DataRecorder-3.6.2-py3-none-any.whl (37 kB)
Downloading DownloadKit-2.0.7-py3-none-any.whl (21 kB)
Downloading DrissionPage-4.1.0.9-py3-none-any.whl (246 kB)
Downloading et_xmlfile-2.0.0-py3-none-any.whl (18 kB)
Downloading idna-3.10-py3-none-any.whl (70 kB)
Downloading lxml-5.3.0-cp312-cp312-win_amd64.whl (3.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.8/3.8 MB 6.7 MB/s eta 0:00:00
Downloading macholib-1.16.3-py2.py3-none-any.whl (38 kB)
Downloading packaging-24.2-py3-none-any.whl (65 kB)
Downloading pefile-2023.2.7-py3-none-any.whl (71 kB)
Downloading psutil-6.1.1-cp37-abi3-win_amd64.whl (254 kB)
Downloading pycryptodome-3.21.0-cp36-abi3-win_amd64.whl (1.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 9.9 MB/s eta 0:00:00
Downloading pyinstaller-6.11.1-py3-none-win_amd64.whl (1.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 6.9 MB/s eta 0:00:00
Downloading pyinstaller_hooks_contrib-2024.11-py3-none-any.whl (339 kB)
Downloading PyVirtualDisplay-3.0-py3-none-any.whl (15 kB)
Downloading requests_file-2.1.0-py2.py3-none-any.whl (4.2 kB)
Downloading setuptools-75.6.0-py3-none-any.whl (1.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 8.8 MB/s eta 0:00:00
Downloading tldextract-5.1.3-py3-none-any.whl (104 kB)
Downloading urllib3-2.3.0-py3-none-any.whl (128 kB)
Downloading websocket_client-1.8.0-py3-none-any.whl (58 kB)
Installing collected packages: PyVirtualDisplay, websocket-client, urllib3, setuptools, pycryptodome, psutil, pefile, packaging, macholib, lxml, idna, et_xmlfile, cssselect, click, charset-normalizer, certifi, pyinstaller-hooks-contrib, cryptography, requests-file, pyinstaller, DataRecorder, tldextract, DownloadKit, DrissionPage
  Attempting uninstall: urllib3
    Found existing installation: urllib3 2.2.2
    Uninstalling urllib3-2.2.2:
      Successfully uninstalled urllib3-2.2.2
  Attempting uninstall: setuptools
    Found existing installation: setuptools 74.1.2
    Uninstalling setuptools-74.1.2:
      Successfully uninstalled setuptools-74.1.2
  Attempting uninstall: pycryptodome
    Found existing installation: pycryptodome 3.20.0
    Uninstalling pycryptodome-3.20.0:
      Successfully uninstalled pycryptodome-3.20.0
  Attempting uninstall: pefile
    Found existing installation: pefile 2024.8.26
    Uninstalling pefile-2024.8.26:
      Successfully uninstalled pefile-2024.8.26
  Attempting uninstall: packaging
    Found existing installation: packaging 24.1
    Uninstalling packaging-24.1:
      Successfully uninstalled packaging-24.1
  Attempting uninstall: idna
    Found existing installation: idna 3.8
    Uninstalling idna-3.8:
      Successfully uninstalled idna-3.8
  Attempting uninstall: et_xmlfile
    Found existing installation: et-xmlfile 1.1.0
    Uninstalling et-xmlfile-1.1.0:
      Successfully uninstalled et-xmlfile-1.1.0
  Attempting uninstall: click
    Found existing installation: click 8.1.7
    Uninstalling click-8.1.7:
      Successfully uninstalled click-8.1.7
  Attempting uninstall: charset-normalizer
    Found existing installation: charset-normalizer 3.3.2
    Uninstalling charset-normalizer-3.3.2:
      Successfully uninstalled charset-normalizer-3.3.2
  Attempting uninstall: certifi
    Found existing installation: certifi 2024.8.30
    Uninstalling certifi-2024.8.30:
      Successfully uninstalled certifi-2024.8.30
  Attempting uninstall: pyinstaller-hooks-contrib
    Found existing installation: pyinstaller-hooks-contrib 2024.8
    Uninstalling pyinstaller-hooks-contrib-2024.8:
      Successfully uninstalled pyinstaller-hooks-contrib-2024.8
  Attempting uninstall: cryptography
    Found existing installation: cryptography 42.0.8
    Uninstalling cryptography-42.0.8:
      Successfully uninstalled cryptography-42.0.8
  Attempting uninstall: pyinstaller
    Found existing installation: pyinstaller 6.10.0
    Uninstalling pyinstaller-6.10.0:
      Successfully uninstalled pyinstaller-6.10.0
Successfully installed DataRecorder-3.6.2 DownloadKit-2.0.7 DrissionPage-4.1.0.9 PyVirtualDisplay-3.0 certifi-2024.12.14 charset-normalizer-3.4.1 click-8.1.8 cryptography-44.0.0 cssselect-1.2.0 et_xmlfile-2.0.0 idna-3.10 lxml-5.3.0 macholib-1.16.3 packaging-24.2 pefile-2023.2.7 psutil-6.1.1 pycryptodome-3.21.0 pyinstaller-6.11.1 pyinstaller-hooks-contrib-2024.11 requests-file-2.1.0 setuptools-75.6.0 tldextract-5.1.3 urllib3-2.3.0 websocket-client-1.8.0

[notice] A new release of pip is available: 24.2 -> 24.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
  1. 升级依赖包

    • 如果你想要升级 requirements.txt 中列出的包到最新版本,可以使用:
       

      bash

      pip install --upgrade -r requirements.txt

常用的 pip 命令

  • 安装包
     

    bash

    pip install package_name
  • 卸载包
     

    bash

    pip uninstall package_name
  • 查看已安装的包
     

    bash

    pip list
  • 检查依赖冲突
     

    bash

    pip check

注意事项

  • 虚拟环境:建议在虚拟环境中使用 pip 和 requirements.txt,以避免污染全局Python环境。可以使用 venv 或 virtualenv 创建虚拟环境。
  • 版本管理:在 requirements.txt 中指定版本号可以确保不同环境下的依赖一致性,但过于严格的版本号可能会导致依赖冲突或难以更新包.
  • 依赖管理工具:对于更复杂的依赖管理需求,可以考虑使用 pipenv 或 poetry 等工具,它们提供了更高级的功能,如依赖解析、锁文件管理等.

通过合理使用 piprequirements.txt,可以有效地管理Python项目的依赖,确保项目的可移植性和可复现性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值