pip === ###### tags: `Python` ###### tags: `Python`, `pip`, `pip3`, `pip show`, `相依性`, `dependencies`, `importlib`, `pkg_resources`, `sys.modules` <br> [TOC] <br> ## 指令 ### 安裝 pip 模組 - ### 方法一 ``` $ apt-get install python3-pip ``` - ### 方法二 ``` $ python -m ensurepip --upgrade ``` - [ModuleNotFoundError: No module named 'pip' in Python](https://bobbyhadz.com/blog/python-no-module-named-pip) <br> ### 安裝套件 - [How to pip install a package with min and max version range?](https://stackoverflow.com/questions/8795617/) > keywords: pip install maximum version ``` $ pip install "package>=0.2,<0.3" ``` ### 套件清單 ```bash pip list ``` ### 套件資訊 ```bash $ pip show requests Name: requests Version: 2.22.0 Summary: Python HTTP for Humans. Home-page: http://python-requests.org Author: Kenneth Reitz Author-email: me@kennethreitz.org License: Apache 2.0 Location: /usr/lib/python3/dist-packages Requires: Required-by: ``` - #### 安裝位置 - 一般環境 ``` pip show requests | grep Location ``` ``` Location: /usr/lib/python3/dist-packages ``` - Anaconda (使用 ipython 啟動) ``` Location: ... /anaconda3/lib/python3.8/site-packages ``` - conda 環境 ``` Location: ... /anaconda3/envs/hail/lib/python3.7/site-packages ``` - #### 實際應用 ``` HAIL_HOME=$(pip3 show hail | grep Location | awk -F' ' '{print $2 "/hail"}') ``` ``` [1] Location: ... /anaconda3/envs/hail/lib/python3.7/site-packages [2] ... /anaconda3/envs/hail/lib/python3.7/site-packages [3] Location: ... /anaconda3/envs/hail/lib/python3.7/site-packages/hail ``` - 參考資料 [Install Hail on a Spark Cluster](https://hail.is/docs/0.2/install/other-cluster.html) <br> ### 取得當前環境的所有套件版本 ``` pip freeze > requirements.txt ``` <br> ## 套件相依性 - ### [Is there a way to list pip dependencies/requirements?](https://stackoverflow.com/questions/11147667) ``` $ pip install johnnydep $ johnnydep your_package ``` - ### [Identifying the dependency relationship for python packages installed with pip](https://stackoverflow.com/questions/9232568/) > 關鍵字 pip show dependencies ``` $ pip install pipdeptree $ pipdeptree ``` ### 測試 tokenizers ``` $ pip show tokenizers Name: tokenizers Version: 0.15.1 Summary: Home-page: Author: Anthony MOI <m.anthony.moi@gmail.com> Author-email: Nicolas Patry <patry.nicolas@protonmail.com>, Anthony Moi <anthony@huggingface.co> License: Location: /home/diatango_lin/tj_tsai/software/anaconda3/envs/llava-0410/lib/python3.10/site-packages Requires: huggingface_hub Required-by: llava, transformers ``` - #### Requires: huggingface_hub ``` $ pipdeptree -p tokenizers Warning!!! Possibly conflicting dependencies found: * jupyterlab==4.1.6 - httpx [required: >=0.25.0, installed: 0.24.0] ------------------------------------------------------------------------ tokenizers==0.15.1 └── huggingface-hub [required: >=0.16.4,<1.0, installed: 0.22.2] ├── filelock [required: Any, installed: 3.13.4] ├── fsspec [required: >=2023.5.0, installed: 2024.3.1] ├── packaging [required: >=20.9, installed: 24.0] ├── PyYAML [required: >=5.1, installed: 6.0.1] ├── requests [required: Any, installed: 2.31.0] │ ├── certifi [required: >=2017.4.17, installed: 2024.2.2] │ ├── charset-normalizer [required: >=2,<4, installed: 3.3.2] │ ├── idna [required: >=2.5,<4, installed: 3.6] │ └── urllib3 [required: >=1.21.1,<3, installed: 2.2.1] ├── tqdm [required: >=4.42.1, installed: 4.66.2] └── typing_extensions [required: >=3.7.4.3, installed: 4.11.0] ``` - #### Required-by: llava, transformers ``` $ pipdeptree -r -p tokenizers Warning!!! Possibly conflicting dependencies found: * jupyterlab==4.1.6 - httpx [required: >=0.25.0, installed: 0.24.0] ------------------------------------------------------------------------ tokenizers==0.15.1 ├── llava==1.2.2.post1 [requires: tokenizers==0.15.1] └── transformers==4.37.2 [requires: tokenizers>=0.14,<0.19] ├── llava==1.2.2.post1 [requires: transformers==4.37.2] └── peft==0.10.0 [requires: transformers] └── llava==1.2.2.post1 [requires: peft] ``` <br> ## 套件安裝問題 - [成功解決INFO: pip is looking at multiple versions of keras-preprocessing to determine which version is ...](https://www.gushiciku.cn/pl/abHS/zh-tw) > INFO: pip is looking at multiple versions of keras-preprocessing to determine which version is compatible with other requirements. This could take a while.\ INFO: This is taking longer than usual. You might need to provide the dependency resolver with stricter constraints to reduce runtime. If you want to abort this run, you can press Ctrl + C to do so. To improve how pip performs, tell us what happened here: https\://pip.pypa.io/surveys/backtracking\ INFO: pip is looking at multiple versions of h5py to determine which version is compatible with other requirements. This could take a while.\ INFO: pip is looking at multiple versions of grpcio to determine which version is compatible with other requirements. This could take a while. > 資訊:pip正在研究keras預處理的多個版本,以確定哪個版本與其他需求相容。這可能需要一段時間。\ 資訊:這比通常需要更長的時間。您可能需要為依賴項解析程式提供更嚴格的約束以減少執行時間。如果要中止此執行,可以按Ctrl+C來中止。要改進pip的執行方式,請在此處告訴我們發生了什麼:https\://pip.pypa.io/surveys/backtracking\ 資訊:pip正在研究h5py的多個版本,以確定哪個版本與其他要求相容。這可能需要一段時間。\ 資訊:pip正在研究grpcio的多個版本,以確定哪個版本與其他需求相容。這可能需要一段時間。 <br> ## 套件更新問題 - ### [Why is pip not upgrading cleanly on ubuntu 20.04 lts?](https://askubuntu.com/questions/1394163/) - ### [🦙 Python Bindings for llama.cpp](https://llama-cpp-python.readthedocs.io/en/latest/) `pip install llama-cpp-python --force-reinstall --upgrade --no-cache-dir ` - `--force-reinstall` - `--upgrade` - `--no-cache-dir` - ### Q: python 的 pip 的 --no-cache-dir 參數,到底帶來哪些影響? [ChatGPT] `pip` 的` --no-cache-dir` 參數用於避免使用快取目錄,它會影響以下方面: - **不使用快取**:當您使用 `pip install` 安裝套件時,`pip` 通常會將已經下載的套件和元數據(metadata)存儲在快取目錄中,以便在以後的安裝中重複使用。使用 `--no-cache-dir` 參數後,pip 不會使用這些快取,而是重新下載和安裝所需的套件,這會增加安裝時間。 - **避免快取問題**:有時,使用快取的套件可能會導致問題,例如過時的元數據或套件版本。使用 `--no-cache-dir` 可以確保始終安裝最新版本的套件,而不依賴於快取中的內容。 - **無需快取空間**:如果您處於磁盤空間有限的情況下,或者不想在磁盤上保留快取,`--no-cache-dir` 可以幫助您節省磁盤空間。 總結來說,`--no-cache-dir` 參數主要用於強制 pip 忽略快取並從頭開始下載和安裝套件。這在某些情況下很有用,但可能會導致較長的安裝時間,特別是當需要安裝大量套件時。 <br> ## 討論 ### 如何動態檢查一個 python 套件是否有安裝,比如 numpy? - ### ChatGPT-4o 在 Python 中,你可以透過幾種方式動態地檢查一個套件(如 `numpy`)是否已被安裝。以下是一些常見的方法: ### 方法 1:使用 importlib 你可以使用 Python 的標準庫 `importlib` 來嘗試導入一個模組,並捕捉導入錯誤來判斷模組是否安裝。 ```python= import importlib def check_module_installed(module_name): try: importlib.import_module(module_name) return True except ImportError: return False # 檢查 numpy 是否安裝 is_installed = check_module_installed('numpy') print(f'Is numpy installed? {is_installed}') ``` 這個方法的好處是直接且不需要額外的依賴。 - **備註**: - error 實體類別為 `ModuleNotFoundError` (繼承至 `ImportError`) - `e.__class__.mro()` `[<class 'ModuleNotFoundError'>, <class 'ImportError'>, <class 'Exception'>, <class 'BaseException'>, <class 'object'>]` - `except ImportError` 或 `except ModuleNotFoundError` 都可以 ### 方法 2:使用 pkg_resources `pkg_resources` 是 setuptools 的一部分,它可以用來檢查套件的安裝情況。這個方法需要你已經安裝了 setuptools。 ```python= import pkg_resources def check_module_installed(module_name): try: pkg_resources.get_distribution(module_name) return True except pkg_resources.DistributionNotFound: return False # 檢查 numpy 是否安裝 is_installed = check_module_installed('numpy') print(f'Is numpy installed? {is_installed}') ``` 這個方法適用於需要確認套件版本或其他元數據時。 ### 方法 3:使用 sys.modules 如果模組在當前 Python 進程中已經被導入,你可以使用 `sys.modules` 來檢查它是否存在。 ```python= import sys def is_module_loaded(module_name): return module_name in sys.modules # 首先嘗試導入模組 try: import numpy except ImportError: numpy_installed = False else: numpy_installed = is_module_loaded('numpy') print(f'Is numpy loaded? {numpy_installed}') ``` 這種方法檢查模組是否已在當前進程中加載,而不一定檢查是否安裝。 ### 選擇合適的方法 - 如果你需要檢查任何套件是否安裝而不需加載它,使用 `pkg_resources` 或 `importlib` 方法較為合適。 - 如果套件可能在程式執行過程中已被加載,並且你只想確認它是否可用,則使用 `sys.modules`。 這些方法各有利弊,選擇哪一種取決於你的具體需求和環境。