• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

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

    发布者: 天下网吧 | 发布时间: 2025-6-17 08:13| 查看数: 300| 评论数: 0|帖子模式

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

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

    1. 使用pip包导出


    1)pip freezen > requirements.txt

    该方法导出的的requirements.txt包含安装包所在路径,常用于一些大型项目的环境保存,一般大型项目会加载和运行多个环境,这个时候每个环境对应的位置也需要告知。
    1. pip freezen > requirements.txt
    复制代码
    2)pip list --format=freeze >requirement.txt

    该方法导出的requirements.txt不包含安装包所在路径,与常见的requirements.txt非常类似。
    以上两种方法,导出的都是该环境里面所有安装的python包,但是有些包并不是该项目中所必备的依赖,有时候导出的一些包并不是项目运行所必要的。
    1. pip list --format=freeze >requirement.txt
    复制代码
    3)使用pip和requirements.txt安装包

    使用以下命令安装依赖包:
    1. pip install -r requirements.txt
    复制代码
    2. 使用conda导出

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

    1)conda list -e > requirements.txt

    使用以下命令导出requirements.txt文件
    1. conda list -e > requirements.txt
    复制代码
    若要使用conda安装requirements.txt文件,使用以下命令安装依赖:
    1. conda install --yes --file requirements.txt
    复制代码
    2)conda env export > freeze.yml

    还可以通过conda导出yaml文件的方式来导出环境,命令如下:
    1. conda env export > freeze.yml
    复制代码
    导出的yaml文件使用如下命令安装:
    1. conda env create -f freeze.yml
    复制代码
    3. 使用pipreqs包导出(建议)

    以上两种方法导出的都是整个安装环境,但是有时候一个项目并不需要安装环境里面所有的依赖,建议使用pipreqs包导出requirements.txt文件。
    使用 pipreqs 可以自动检索到当前项目下的所有组件及其版本,并生成 requirements.txt 文件,极大方便了项目迁移和部署的包管理。相比直接用 requirements.txt 命令,能直接隔离其它项目的包生成。
    使用如下命令安装pipreqs:
    1. pip install pipreqs
    复制代码
    1. Usage:
    2.     pipreqs [options] <path>

    3. Options:
    4.     --use-local           Use ONLY local package info instead of querying PyPI
    5.     --pypi-server <url>   Use custom PyPi server
    6.     --proxy <url>         Use Proxy, parameter will be passed to requests library. You can also just set the
    7.                           environments parameter in your terminal:
    8.                           $ export HTTP_PROXY="http://10.10.1.10:3128"
    9.                           $ export HTTPS_PROXY="https://10.10.1.10:1080"
    10.     --debug               Print debug information
    11.     --ignore <dirs>...    Ignore extra directories
    12.     --encoding <charset>  Use encoding parameter for file open
    13.     --savepath <file>     Save the list of requirements in the given file
    14.     --print               Output the list of requirements in the standard output
    15.     --force               Overwrite existing requirements.txt
    16.     --diff <file>         Compare modules in requirements.txt to project imports.
    17.     --clean <file>        Clean up requirements.txt by removing modules that are not imported in project.
    18.     --no-pin              Omit version of output packages.
    复制代码
    使用如下命令导出requirements.txt文件:
    1. pipreqs ./
    复制代码
    如果是在Windows环境下,建议使用如下命令导出requirements.txt文件:
    1. pipreqs ./ --encoding=utf-8
    复制代码
    如果环境中存在requirements.txt文件,需要使用以下命令导出requirements.txt文件:
    1. pipreqs ./ --encoding=utf-8 --force
    复制代码
    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    来源:https://www.jb51.net/python/339744amr.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表