Python保存環(huán)境方式(導(dǎo)出requirements.txt文件)
Python保存環(huán)境(導(dǎo)出requirements.txt文件)
在深度學(xué)習(xí)場(chǎng)景中,我們經(jīng)過會(huì)遇到需要保存環(huán)境的時(shí)候,例如將代碼共享給他人的時(shí)候,需要導(dǎo)出一個(gè)requirements.txt文件,讓其他人知道運(yùn)行該代碼所需要的python包依賴。
本文主要介紹一些常見的導(dǎo)出python環(huán)境的方法和工具。
1. 使用pip包導(dǎo)出
1)pip freezen > requirements.txt
該方法導(dǎo)出的的requirements.txt包含安裝包所在路徑,常用于一些大型項(xiàng)目的環(huán)境保存,一般大型項(xiàng)目會(huì)加載和運(yùn)行多個(gè)環(huán)境,這個(gè)時(shí)候每個(gè)環(huán)境對(duì)應(yīng)的位置也需要告知。
pip freezen > requirements.txt
2)pip list --format=freeze >requirement.txt
該方法導(dǎo)出的requirements.txt不包含安裝包所在路徑,與常見的requirements.txt非常類似。
以上兩種方法,導(dǎo)出的都是該環(huán)境里面所有安裝的python包,但是有些包并不是該項(xiàng)目中所必備的依賴,有時(shí)候?qū)С龅囊恍┌⒉皇琼?xiàng)目運(yùn)行所必要的。
pip list --format=freeze >requirement.txt
3)使用pip和requirements.txt安裝包
使用以下命令安裝依賴包:
pip install -r requirements.txt
2. 使用conda導(dǎo)出
如果使用conda管理環(huán)境,也可以使用conda命令導(dǎo)出和安裝python依賴
1)conda list -e > requirements.txt
使用以下命令導(dǎo)出requirements.txt文件
conda list -e > requirements.txt
若要使用conda安裝requirements.txt文件,使用以下命令安裝依賴:
conda install --yes --file requirements.txt
2)conda env export > freeze.yml
還可以通過conda導(dǎo)出yaml文件的方式來導(dǎo)出環(huán)境,命令如下:
conda env export > freeze.yml
導(dǎo)出的yaml文件使用如下命令安裝:
conda env create -f freeze.yml
3. 使用pipreqs包導(dǎo)出(建議)
以上兩種方法導(dǎo)出的都是整個(gè)安裝環(huán)境,但是有時(shí)候一個(gè)項(xiàng)目并不需要安裝環(huán)境里面所有的依賴,建議使用pipreqs包導(dǎo)出requirements.txt文件。
使用 pipreqs 可以自動(dòng)檢索到當(dāng)前項(xiàng)目下的所有組件及其版本,并生成 requirements.txt 文件,極大方便了項(xiàng)目遷移和部署的包管理。相比直接用 requirements.txt 命令,能直接隔離其它項(xiàng)目的包生成。
使用如下命令安裝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.使用如下命令導(dǎo)出requirements.txt文件:
pipreqs ./
如果是在Windows環(huán)境下,建議使用如下命令導(dǎo)出requirements.txt文件:
pipreqs ./ --encoding=utf-8
如果環(huán)境中存在requirements.txt文件,需要使用以下命令導(dǎo)出requirements.txt文件:
pipreqs ./ --encoding=utf-8 --force
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 中創(chuàng)建 PostgreSQL 數(shù)據(jù)庫(kù)連接池
這篇文章主要介紹了Python 中創(chuàng)建 PostgreSQL 數(shù)據(jù)庫(kù)連接池,Python 連接 PostgreSQL 是主要有兩個(gè)包, py-postgresql 和 psycopg2 , 而本文的實(shí)例將使用后者,感興趣的小伙伴可以參考一下2021-10-10
python讀取json文件并將數(shù)據(jù)插入到mongodb的方法
這篇文章主要介紹了python讀取json文件并將數(shù)據(jù)插入到mongodb的方法,實(shí)例分析了Python操作json及mongodb數(shù)據(jù)庫(kù)的技巧,需要的朋友可以參考下2015-03-03
python字符串轉(zhuǎn)換成浮點(diǎn)數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了python字符串轉(zhuǎn)換成浮點(diǎn)數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2026-03-03
Python新版極驗(yàn)驗(yàn)證碼識(shí)別驗(yàn)證碼教程詳解
這篇文章主要介紹了Python新版極驗(yàn)驗(yàn)證碼識(shí)別驗(yàn)證碼,極驗(yàn)驗(yàn)證是一種在計(jì)算機(jī)領(lǐng)域用于區(qū)分自然人和機(jī)器人的,通過簡(jiǎn)單集成的方式,為開發(fā)者提供安全、便捷的云端驗(yàn)證服務(wù)2023-02-02
python中的iterator和"lazy?iterator"區(qū)別介紹
這篇文章主要介紹了python中的iterator和?“l(fā)azy?iterator“之間有什么區(qū)別,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04

