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

Written by

in

文章目录
  • 在深度学习场景中,我们经过会遇到需要保存环境的时候,例如将代码共享给他人的时候,需要导出一个requirements.txt文件,让其他人知道运行该代码所需要的python包依赖。 本文主要介绍一些常见的导出python环境的方法和工具。
  • 以上为个人经验,希望能给大家一个参考,也希望大家多多支持风君子博客。 您可能感兴趣的文章: Python生成requirements.txt的三种方法 Python pip通过requirements.txt 文件安装依赖 python pipreqs工具生成requirements.txt文件使用详解 python生成requirements.txt文件的两种方法 Python使用requirements.txt和pip打包批量安装的实现 详解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包导出(建议)
      • 总结

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

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

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

        pip freezen > requirements.txt

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

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

        pip list --format=freeze >requirement.txt

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

        pip install -r requirements.txt

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

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

        conda list -e > requirements.txt

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

        conda install --yes --file requirements.txt

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

        conda env export > freeze.yml

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

        conda env create -f freeze.yml

        以上两种方法导出的都是整个安装环境,但是有时候一个项目并不需要安装环境里面所有的依赖,建议使用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>   Use 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"
                                  $ 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 modules 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

        以上为个人经验,希望能给大家一个参考,也希望大家多多支持风君子博客。

        您可能感兴趣的文章:

        • Python生成requirements.txt的三种方法
        • Python pip通过requirements.txt 文件安装依赖
        • python pipreqs工具生成requirements.txt文件使用详解
        • python生成requirements.txt文件的两种方法
        • Python使用requirements.txt和pip打包批量安装的实现
        • 详解python中requirements.txt的一切

        站内搜索