开发者

Python保存环境方式(导出requirements.txt文件)

目录
  • python保存环境(导出requirements.txt文件)
    • 1. 使用pip包导出
      • 1)pip freezen > requirements.txt
      • 2)pip list --format=freeze >requirement.txt
      • 3)使用pip和requirements.txt安装包
    • 2. 使用conda导出
      • 1)conda list -e > requirements.txt
      • 2)conda env export > freeze.yml
    • 3. 使用pipreqs包导出(建议)
    • 总结

      Python保存环境(导出requirements.txt文件)

      在深度学习场景中,我们经过会遇到需要保存环境的时候,例如将代码共享给他人的时候,需要导出一个requirements.txt文件,让其他人知道运行该代码所需要的python包依赖。

      本文主要介绍一些常见的导出python环境的方法和工具。

      1. 使用pip包导出

      1)pip freezen > requirements.txt

      该方法导出的的requirements.txt包含安装包所在路径,常用于一些大型项目的环境保存,一般大型项目会加载和运行多个环境,这个时候每个环境对应的位置也需要告知。

      pip freezen > requirements.txt

      2)pip list --format=freeze >requirement.txt

      该方法导出的requirements.txt不包含安装包所在路径,与常见的requirements.txt非常类似。

      以上两种方法,导出的都是该环境里面所有安装的python包,但是有些包并不是该项目中所必备的依赖,有时候导出的一些包并不是项目运行所必要的。

      pip list --format=freeze >requirement.txt

      3)使用pip和requirements.txt安装包

      使用以下命令安装依赖包:

      pip install -r requirements.txt

      2. 使用conda导出

      如果使用conda管理环境,也可以使用conda命令导出和安装python依赖

      1)conda list -e > requirements.txt

      使用以下命令导出requirements.txt文件

      conda list -e > requirements.txt

      若要使用conwww.devze.comda安装requirements.txt文件,使用以下命令安装依赖:

      conda install --yes --file requirements.txt

      2)conda env export > freeze.yml

      还可以通过conda导出yaml文件的方式来导出环境,命令如下:

      conda env export > freeze.yml

      导出的yaml文件使用如下命令安装:

      condwww.devze.coma env create -f freeze.yml

      3. 使用pipreqs包导出(建议)

      以上两种方法导出的都是整个安装环境,但是有时候一个项目并不需要安装环境里面所有的依赖,建议使用pipreqs包导出requirements.txt文件。

      使用 pipreqs 可以自动检索到当前项目下的所有组件及其版本,并生成 requirements.txt 文件,极大方便了项目迁移和部署的包管理。相比直接用 requirements.txt 命令,能直接隔离其它项目的包生成。

      使用如下命令安装pipreqs:

      pip install pipreqs
      Usage:
          pipreqs [options] <path>
      
      Options:
          --use-local           Use ONLY local package info instead of querying PyPI
          --pypi-server <url>   Us编程e custom PyPi server
          --proxy <url>         Use Proxy, parameter will be passed to requests library. You can also just set the
                                environments parameter in your terminal:
                                $ export HTTP_PROXY="http://10.10.1.10:3128"
                   ZnxwQmmj             $ export HTTPS_PROXY="https://10.10.1.10:1080"
          --debug               Print debug information
          --ignore <dirs>...    Ignore extra directories
          --encoding <charset>  Use encoding parameter for file open
          --savepath <file>     Save the list of requirements in the given file
          --print               Output the list of requirements in the standard output
          --force               Overwrite existing requirements.txt
          --diff <file>         Compare modules in requirements.txt to project imports.
          --clean <file>        Clean up requirements.txt by removing moZnxwQmmjdules that are not imported in project.
          --no-pin              Omit version of output packages.

      使用如下命令导出requirements.txt文件:

      pipreqs ./

      如果是在Windows环境下,建议使用如下命令导出requirements.txt文件:

      pipreqs ./ --encoding=utf-8

      如果环境中存在requirements.txt文件,需要使用以下命令导出requirements.txt文件:

      pipreqs ./ --encoding=utf-8 --force

      总结

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

      0

      上一篇:

      下一篇:

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      最新开发

      开发排行榜