如何使用conda和pip批量安裝Python包
使用conda和pip批量安裝Python包
在debug Yolov5之前,需要按照其txt文件中指定的包的版本來指定安裝工程需要的Python包,截圖如下:
(這里面的torch慎裝,因為這種方式裝的pytorch不吃吃GPU,如果某個包不想安裝,只要在該行前面輸入注釋符就行)
conda方式批量安裝
進(jìn)入(cd)到txt文件所在文件夾路徑下,運(yùn)行以下命令:
$ conda install --file=requirements_conda.txt
pip方式批量安裝
pip install -r requirements_conda.txt
conda和pip總結(jié)
conda相關(guān)
基本命令
- 查看conda相關(guān)信息:conda info
- 顯示所有的虛擬環(huán)境: conda info -e(–envs)
- 激活環(huán)境:conda activate xxxx
- 關(guān)閉環(huán)境:conda deactivate
創(chuàng)建、刪除虛擬環(huán)境
- 創(chuàng)建環(huán)境: conda create -n xxxx python=3.7 #創(chuàng)建python3.7的xxxx虛擬環(huán)境
- 刪除環(huán)境:conda remove -n xxxx --all //刪除xxxx虛擬環(huán)境
復(fù)制、重命名環(huán)境
Conda是沒有重命名環(huán)境的功能的, 要實現(xiàn)這個基本需求, 只能通過愚蠢的克隆-刪除的過程,切記不要直接mv移動環(huán)境的文件夾來重命名, 會導(dǎo)致一系列無法想象的錯誤的發(fā)生!
- 克隆oldname環(huán)境為newname環(huán)境: conda create --name newname --clone oldname
- 徹底刪除舊環(huán)境:conda remove --name oldname --all
注意:必須在base環(huán)境下進(jìn)行以上操作,否則會出現(xiàn)各種莫名的問題。
安裝、更新、卸載安裝包
- 查看已經(jīng)安裝的文件包: conda list
- 指定查看xxx虛擬環(huán)境下安裝的package: conda list -n xxx
- 安裝xxx文件包:conda install xxx
- 更新xxx文件包:conda update xxx
- 卸載xxx文件包:conda uninstall xxx
conda安裝requirements中的包:
conda install --yes --file requirements.txt
但是這里存在一個問題,如果requirements.txt中的包不可用,則會拋出“無包錯誤”。使用下面這個命令可以解決這個問題
while read requirement; do conda install --yes $requirement; done < requirements.txt
如果想要在conda命令無效時使用pip命令來代替,那么使用如下命令:
while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt
conda安裝包清理(conda瘦身)
conda clean -H
:查看conda clean使用參數(shù)conda clean -p
:刪除一些沒用的包,這個命令會檢查哪些包沒有在包緩存中被硬依賴到其他地方,并刪除它們conda clean -t
:可以刪除conda保存下來的tar包。conda clean -a
:刪除索引緩存、鎖定文件、未使用過的包和tar包。
conda自動開啟/關(guān)閉激活
- 關(guān)閉自動激活狀態(tài): conda config --set auto_activate_base false
- 開啟自動激活狀態(tài): conda config --set auto_activate_base true
conda批量導(dǎo)出、安裝:
- 可以導(dǎo)出到.yml文件:conda env export > freeze.yml
- 然后直接創(chuàng)建conda環(huán)境:conda env create -f freeze.yml
解決conda install 下載速度慢,conda數(shù)據(jù)源管理
- 查看配置信息:conda config --show
- 顯示目前conda的數(shù)據(jù)源有哪些: conda config --show channels
添加數(shù)據(jù)源:例如, 添加清華anaconda鏡像:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/ conda config --add channels ?https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ conda config --set show_channel_urls yes
然后運(yùn)行conda clean -i清除索引緩存,保證用的是鏡像站提供的索引
刪除單個數(shù)據(jù)源:
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
恢復(fù)默認(rèn)源:conda config --remove-key channels
pip相關(guān)
安裝、更新、卸載包
- 列出當(dāng)前緩存的包:pip list
- 安裝xxx包: pip install xxx
- 卸載xxx包: pip uninstall xxx
- 展示指定的已安裝的xxx包: pip show xxx
- 檢查xxx包的依賴是否合適:pip check xxx
pip數(shù)據(jù)源管理
- 顯示目前pip的數(shù)據(jù)源有哪些:pip config list
- 臨時使用數(shù)據(jù)源:pip install markdown -i https://pypi.tuna.tsinghua.edu.cn/simple
永久使用該數(shù)據(jù)源:
方法一:
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple pip config set global.trusted-host mirrors.aliyun.com
方法二:配置文件配置
vim ~/.pip/pip.conf
寫入以下內(nèi)容:
[global] index-url = http://mirrors.aliyun.com/pypi/simple trusted-host = mirrors.aliyun.com
記錄一下pip國內(nèi)源
- 阿里云: https://mirrors.aliyun.com/pypi/simple/
- 中國科技大學(xué): https://pypi.mirrors.ustc.edu.cn/simple/
- 豆瓣(douban): https://pypi.douban.com/simple/
- 清華大學(xué): https://pypi.tuna.tsinghua.edu.cn/simple/
- 中國科學(xué)技術(shù)大學(xué): https://pypi.mirrors.ustc.edu.cn/simple/
- 騰訊源: https://mirrors.cloud.tencent.com/pypi/simple
pip批量導(dǎo)出、安裝:
- 生成requirements.txt文件:pip freeze > requirements.txt
- 安裝requirements.txt文件依賴:pip install -r requirements.txt
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
超詳細(xì)注釋之OpenCV操作圖像平移轉(zhuǎn)換
這篇文章主要介紹了OpenCV操作圖像平移轉(zhuǎn)換,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09