Python保存環(huán)境方式(導(dǎo)出requirements.txt文件)
Python保存環(huán)境(導(dǎo)出requirements.txt文件)
在深度學(xué)習(xí)場景中,我們經(jīng)過會遇到需要保存環(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)目會加載和運(yùn)行多個(gè)環(huán)境,這個(gè)時(shí)候每個(gè)環(huán)境對應(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)文章
pandas 數(shù)據(jù)結(jié)構(gòu)之Series的使用方法
這篇文章主要介紹了pandas 數(shù)據(jù)結(jié)構(gòu)之Series的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06yolov5中anchors設(shè)置實(shí)例詳解
在YOLOV5算法之中,針對不同的數(shù)據(jù)集,一般會預(yù)先設(shè)置固定的Anchor,下面這篇文章主要給大家介紹了關(guān)于yolov5中anchors設(shè)置的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06python爬蟲使用requests發(fā)送post請求示例詳解
這篇文章主要介紹了python爬蟲使用requests發(fā)送post請求示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Python SELENIUM上傳文件或圖片實(shí)現(xiàn)過程
這篇文章主要介紹了Python SELENIUM上傳文件或圖片實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Python語言實(shí)現(xiàn)百度語音識別API的使用實(shí)例
這篇文章主要介紹了Python語言實(shí)現(xiàn)百度語音識別API的使用實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12