发布于2023-01-21 10:37 阅读(163) 评论(0) 点赞(8) 收藏(5)
在深度学习场景中,我们经过会遇到需要保存环境的时候,例如将代码共享给他人的时候,需要导出一个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
[1] python 中导出requirements.txt 的几种方法 https://blog.csdn.net/weixin_40964777/article/details/126086367
[2] pipreqs https://www.jianshu.com/p/5c30f7c5aa34
原文链接:https://blog.csdn.net/qq_41667743/article/details/128273061
作者:comeonbady
链接:https://www.pythonheidong.com/blog/article/1885271/a171026366a6011e46b9/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!