2023年12月16日 星期六

Python 學習筆記 : 用 requirements.txt 批次安裝套件

每次要在新系統上安裝常用的 Python 套件時要一個一個執行 pip install 指令蠻累的, 其實這種苦活可以用 requirements.txt 批次檔來自動完成, 只要把想安裝的套件名稱與版本編號一列一列寫在 requirements.txt 套件檔裡面, 再用 pip install 安裝此套件檔例如 :

pandas==2.03
requests==2.31.0
scikit-learn==1.3.0
... (略)...

存檔為 requirements.txt 後就可以用 pip install 指令批次安裝這些套件 :

pip install requirements.txt 

如果要在一台電腦複製另一台電腦目前已安裝的套件環境, 不需手刻 requirements.txt (因套件數量可能多到嚇人), 可以用 pip freeze 指令將這些套件名稱與版本頃印到 requirements.txt : 

pip freeze > requirements.txt   

例如 : 

C:\Users\user>pip freeze > requirements.txt   
C:\Users\user>more requirements.txt   
absl-py==0.15.0
adafruit-ampy==1.0.1
appdirs==1.4.4
astunparse==1.6.3
attrs==22.2.0
Automat==22.10.0
beautifulsoup4==4.11.2
bleach==1.5.0
bokeh==2.3.3
builtwith==1.3.3
cached-property==1.5.2
cachetools==4.2.4
certifi==2017.4.17
cffi==1.15.1
chardet==3.0.4
... (略) ...

然後將其複製到另一台電腦用 pip install 安裝即可. 

為了測試批次安裝功能, 我們先建立一個虛擬環境 venv :

d:\python>virtualenv venv    
created virtual environment CPython3.10.11.final.0-64 in 8709ms
  creator CPython3Windows(dest=D:\python\venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\tony1\AppData\Local\pypa\virtualenv)
    added seed packages: pip==23.2.1, setuptools==68.0.0, wheel==0.41.1
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

切換到 venv 目錄並執行 Scripts 下的 activate 指令啟動虛擬環境 : 

d:\python>cd venv  
(venv) d:\python\venv>dir  
 磁碟區 D 中的磁碟是 新增磁碟區
 磁碟區序號:  1258-16B8

 d:\python\venv 的目錄

2023/12/16  下午 10:13    <DIR>          .
2023/12/16  下午 10:13    <DIR>          ..
2023/12/16  下午 10:13                42 .gitignore
2023/12/16  下午 10:13    <DIR>          Lib
2023/12/16  下午 10:13               371 pyvenv.cfg
2023/12/16  下午 10:13    <DIR>          Scripts
               2 個檔案             413 位元組
               4 個目錄  997,214,068,736 位元組可用

d:\python\venv>Scripts\activate   

用 pip list 檢視已安裝套件 : 

(venv) d:\python\venv>pip list   
Package    Version
---------- -------
pip        23.2.1
setuptools 68.0.0
wheel      0.41.1

可見目前只有 3 個已安裝套件. 安裝一個 request 套件 :

(venv) d:\python\venv>pip install requests    
Collecting requests
  Obtaining dependency information for requests from https://files.pythonhosted.org/packages/70/8e/0e2d847013cb52cd35b38c009bb167a1a26b2ce6cd6965bf26b47bc0bf44/requests-2.31.0-py3-none-any.whl.metadata
  Using cached requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
Collecting charset-normalizer<4,>=2 (from requests)
  Obtaining dependency information for charset-normalizer<4,>=2 from https://files.pythonhosted.org/packages/a2/a0/4af29e22cb5942488cf45630cbdd7cefd908768e69bdd90280842e4e8529/charset_normalizer-3.3.2-cp310-cp310-win_amd64.whl.metadata
  Downloading charset_normalizer-3.3.2-cp310-cp310-win_amd64.whl.metadata (34 kB)
Collecting idna<4,>=2.5 (from requests)
  Obtaining dependency information for idna<4,>=2.5 from https://files.pythonhosted.org/packages/c2/e7/a82b05cf63a603df6e68d59ae6a68bf5064484a0718ea5033660af4b54a9/idna-3.6-py3-none-any.whl.metadata
  Downloading idna-3.6-py3-none-any.whl.metadata (9.9 kB)
Collecting urllib3<3,>=1.21.1 (from requests)
  Obtaining dependency information for urllib3<3,>=1.21.1 from https://files.pythonhosted.org/packages/96/94/c31f58c7a7f470d5665935262ebd7455c7e4c7782eb525658d3dbf4b9403/urllib3-2.1.0-py3-none-any.whl.metadata
  Downloading urllib3-2.1.0-py3-none-any.whl.metadata (6.4 kB)
Collecting certifi>=2017.4.17 (from requests)
  Obtaining dependency information for certifi>=2017.4.17 from https://files.pythonhosted.org/packages/64/62/428ef076be88fa93716b576e4a01f919d25968913e817077a386fcbe4f42/certifi-2023.11.17-py3-none-any.whl.metadata
  Downloading certifi-2023.11.17-py3-none-any.whl.metadata (2.2 kB)
Using cached requests-2.31.0-py3-none-any.whl (62 kB)
Downloading certifi-2023.11.17-py3-none-any.whl (162 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 162.5/162.5 kB 1.6 MB/s eta 0:00:00
Downloading charset_normalizer-3.3.2-cp310-cp310-win_amd64.whl (100 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.3/100.3 kB 6.0 MB/s eta 0:00:00
Downloading idna-3.6-py3-none-any.whl (61 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.6/61.6 kB ? eta 0:00:00
Downloading urllib3-2.1.0-py3-none-any.whl (104 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 104.6/104.6 kB 5.9 MB/s eta 0:00:00
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2023.11.17 charset-normalizer-3.3.2 idna-3.6 requests-2.31.0 urllib3-2.1.0

再用 pip list 檢視已安裝套件 : 

(venv) d:\python\venv>pip list   
Package            Version
------------------ ----------
certifi            2023.11.17
charset-normalizer 3.3.2
idna               3.6
pip                23.2.1
requests           2.31.0
setuptools         68.0.0
urllib3            2.1.0
wheel              0.41.1

可見已安裝了 requests 與其相依套件, 這樣就可以用 pip freeze 將目前環境中的已安裝套件名稱與版本頃印到 requirements.txt :

(venv) d:\python\venv>pip freeze > requirements.txt   

開啟此 requirements.txt 內容如下 : 

certifi==2023.11.17
charset-normalizer==3.3.2
idna==3.6
requests==2.31.0
urllib3==2.1.0

接著我們建立另一個虛擬環境 venv2 來測試是否能利用批次安裝檔來複製執行環境 :

d:\python>virtualenv venv2   
created virtual environment CPython3.10.11.final.0-64 in 286ms
  creator CPython3Windows(dest=D:\python\venv2, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\tony1\AppData\Local\pypa\virtualenv)
    added seed packages: pip==23.2.1, setuptools==68.0.0, wheel==0.41.1
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

d:\python>cd venv2   

d:\python\venv2>dir  
 磁碟區 D 中的磁碟是 新增磁碟區
 磁碟區序號:  1258-16B8

 d:\python\venv2 的目錄

2023/12/16  下午 10:20    <DIR>          .
2023/12/16  下午 10:20    <DIR>          ..
2023/12/16  下午 10:20                42 .gitignore
2023/12/16  下午 10:20    <DIR>          Lib
2023/12/16  下午 10:20               371 pyvenv.cfg
2023/12/16  下午 10:20    <DIR>          Scripts
               2 個檔案             413 位元組
               4 個目錄  997,192,208,384 位元組可用

啟動 venv2 虛擬環境 : 

d:\python\venv2>Scripts\activate    

檢視已安裝套件 : 

(venv2) d:\python\venv2>pip list   
Package    Version
---------- -------
pip        23.2.1
setuptools 68.0.0
wheel      0.41.1

將 venv 底下的 requirements.txt 複製到 venv2 下, 然後將 requests 改為限制版本為至少 2.20.0; 但 urllib3 則不指定版本 (即安裝最新版), 但不指定版本的話要在 install 後面添加 -r 參數 (如果用 pip freeze 產生的檔案原封不動安裝就不需要 -r 參數) :

certifi==2023.11.17
charset-normalizer==3.3.2
idna==3.6
requests>=2.20.0    
urllib3  

最後在 venv2 虛擬環境中用 pip install 依此檔案內容批次安裝套件 : 

(venv2) d:\python\venv2>pip install -r requirements.txt    
Collecting certifi==2023.11.17 (from -r requirements.txt (line 1))
  Obtaining dependency information for certifi==2023.11.17 from https://files.pythonhosted.org/packages/64/62/428ef076be88fa93716b576e4a01f919d25968913e817077a386fcbe4f42/certifi-2023.11.17-py3-none-any.whl.metadata
  Using cached certifi-2023.11.17-py3-none-any.whl.metadata (2.2 kB)
Collecting charset-normalizer==3.3.2 (from -r requirements.txt (line 2))
  Obtaining dependency information for charset-normalizer==3.3.2 from https://files.pythonhosted.org/packages/a2/a0/4af29e22cb5942488cf45630cbdd7cefd908768e69bdd90280842e4e8529/charset_normalizer-3.3.2-cp310-cp310-win_amd64.whl.metadata
  Using cached charset_normalizer-3.3.2-cp310-cp310-win_amd64.whl.metadata (34 kB)
Collecting idna==3.6 (from -r requirements.txt (line 3))
  Obtaining dependency information for idna==3.6 from https://files.pythonhosted.org/packages/c2/e7/a82b05cf63a603df6e68d59ae6a68bf5064484a0718ea5033660af4b54a9/idna-3.6-py3-none-any.whl.metadata
  Using cached idna-3.6-py3-none-any.whl.metadata (9.9 kB)
Collecting requests>=2.20.0 (from -r requirements.txt (line 4))
  Obtaining dependency information for requests>=2.20.0 from https://files.pythonhosted.org/packages/70/8e/0e2d847013cb52cd35b38c009bb167a1a26b2ce6cd6965bf26b47bc0bf44/requests-2.31.0-py3-none-any.whl.metadata
  Using cached requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
Collecting urllib3 (from -r requirements.txt (line 5))
  Obtaining dependency information for urllib3 from https://files.pythonhosted.org/packages/96/94/c31f58c7a7f470d5665935262ebd7455c7e4c7782eb525658d3dbf4b9403/urllib3-2.1.0-py3-none-any.whl.metadata
  Using cached urllib3-2.1.0-py3-none-any.whl.metadata (6.4 kB)
Using cached certifi-2023.11.17-py3-none-any.whl (162 kB)
Using cached charset_normalizer-3.3.2-cp310-cp310-win_amd64.whl (100 kB)
Using cached idna-3.6-py3-none-any.whl (61 kB)
Using cached requests-2.31.0-py3-none-any.whl (62 kB)
Using cached urllib3-2.1.0-py3-none-any.whl (104 kB)
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2023.11.17 charset-normalizer-3.3.2 idna-3.6 requests-2.31.0 urllib3-2.1.0

可見已順利完成安裝, 用 pip list 檢視已安裝套件 : 

(venv2) d:\python\venv2>pip list   
Package            Version
------------------ ----------
certifi            2023.11.17
charset-normalizer 3.3.2
idna               3.6
pip                23.2.1
requests           2.31.0  
setuptools         68.0.0
urllib3            2.1.0   
wheel              0.41.1

可見 requests 限制版本至少要 2.20.0, 實際安裝最新的 2.31.0; 而 urllib3 未指定版本也是安裝最新的 2.1.0 版. 

測試完可用 deactivate 指令關閉虛擬環境, 然後用 rmdir 將其目錄刪除即可 :  

(venv2) d:\python\venv2>deactivate   
d:\python\venv2>cd ..  
d:\python>rmdir venv2 /s   
venv2,您確定要執行嗎 (Y/N)? y

注意, 因 venv2 底下有檔案與子目錄, 用 /s 參數可強制刪除. 

參考 :

沒有留言:

張貼留言