在前一篇測試中順利在系統的 Python 環境中安裝了 LangChain, 但這種方式可能因為 LangChain 強行將某些相依套件升版導致版本衝突, 使得有用到這些被升版套件的其他套件無法正常執行.
避免版本衝突的方法是在虛擬環境中安裝 LangChain, 傳統上使用 virtualenv 來建立虛擬環境, 但我在 "LangChain 奇幻旅程" 這本書看到管理套件與虛擬環境的好工具 Poetry, 安裝方式參考 :
安裝完 Poetry 且用 poetry config virtualenvs.in-project true 指令開啟專案內的虛擬環境功能後, 就可以在目前工作目錄下用 poetry new 指令來建立一個 Python 專案 :
D:\python\langchain>poetry new project1
Created package project1 in project1
這樣便建立了一個 projetc1 專案 :
專案目錄下有一個 pyproject.toml 為專案設定檔, Poetry 利用此檔來儲存專案的 metadata, 主要用來管理套件與其依賴關係, 可用記事本開啟, 預設內容如下 :
[project]
name = "project1"
version = "0.1.0"
description = ""
authors = [
{name = "Your Name",email = "you@example.com"}
]
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
]
[tool.poetry]
packages = [{include = "project1", from = "src"}]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
其中的 requires-python 變數用來指定虛擬環境的 Python 版本, 此處 >=3.12 表示最低版本為 3.12, 也可以拿掉 ">=" 指定精確版本. dependencies 變數用來放置安裝的套件名稱, 預設是空串列表示尚未安裝任何套件, 當使用 poetry add 指令安裝套件時就會自動將套件名稱放進 dependencies 串列中了.
接下來先用上面預設的專案設定檔來安裝虛擬環境與 LanChain.
首先進入專案目錄 project1 :
D:\python\langchain>cd project1
然後執行 poetry install 指令, 這會依照專案設定檔 pyproject.toml 中 requires-python 變數所規範的 Python 版本去安裝 Python 虛擬環境 :
D:\python\langchain\project1>poetry install
Creating virtualenv project1 in D:\python\langchain\project1\.venv
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
Installing the current project: project1 (0.1.0)
完成後在專案目錄下就會多出一個 .venv 子目錄 :
用 poetry run python --version 指令檢視此虛擬環境之 Python 版本 :
D:\python\langchain\project1>poetry run python --version
Python 3.12.2
接下來用 poetry add 指令安裝 LangChain :
D:\python\langchain\project1>poetry add langchain
Using version ^0.3.25 for langchain
Updating dependencies
Resolving dependencies... (4.8s)
The current project's supported Python range (>=3.12.4) is not compatible with some of the required packages Python requirement:
- langchain-text-splitters requires Python <4.0,>=3.9, so it will not be installable for Python >=4.0
Because no versions of langchain match >0.3.25,<0.4.0
and langchain (0.3.25) depends on langchain-text-splitters (>=0.3.8,<1.0.0), langchain (>=0.3.25,<0.4.0) requires langchain-text-splitters (>=0.3.8,<1.0.0).
Because langchain-text-splitters (0.3.8) requires Python <4.0,>=3.9
and no versions of langchain-text-splitters match >0.3.8,<1.0.0, langchain-text-splitters is forbidden.
Thus, langchain is forbidden.
So, because project1 depends on langchain (^0.3.25), version solving failed.
* Check your dependencies Python requirement: The Python requirement can be specified via the `python` or `markers` properties
For langchain-text-splitters, a possible solution would be to set the `python` property to ">=3.12.4,<4.0"
https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies,
https://python-poetry.org/docs/dependency-specification/#using-environment-markers
似乎是抱怨要求的 Python 版本 >=3.12.4 與套件所需不符而無法安裝, 但上面 pyproject.toml 的預設設定是 requires-python = ">=3.12" 而非 requires-python = ">=3.12.4" 啊? 真奇怪. 我將上面訊息提交給 ChatGPT 解釋, 它的回答是 Poetry 可能在解析某些依賴時會依據目前執行 Python 版本與專案 requires-python 的交集去做更嚴格限制, 有時候會 "高估" 需求版本.
解決辦法是先將 pyproject.toml 中的 Python 版本改為 requires-python = ">=3.12.2,<4.0", 添加 <4.0 這個版本上限來形成一個範圍, 這樣 Poetry 就不會因為採取嚴格之版本判斷而不安裝 Python :
[project]
name = "project1"
version = "0.1.0"
description = ""
authors = [
{name = "Your Name",email = "you@example.com"}
]
readme = "README.md"
requires-python = ">=3.12.2,<4.0"
dependencies = [
]
[tool.poetry]
packages = [{include = "project1", from = "src"}]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
然後利用檔案總管刪除虛擬環境目錄 .venv 與記錄完整的相依套件樹與版本的 poetry.lock 檔, 再次安裝 Python 虛擬環境就成功了 :
D:\python\langchain\project1>poetry install
Creating virtualenv project1 in D:\python\langchain\project1\.venv
Updating dependencies
Resolving dependencies... (1.1s)
接下來就可以安裝 LangChain 了 :
D:\python\langchain\project1>poetry add langchain
Using version ^0.3.25 for langchain
Updating dependencies
Resolving dependencies... (4.8s)
Package operations: 29 installs, 0 updates, 0 removals
- Installing certifi (2025.4.26)
- Installing charset-normalizer (3.4.2)
- Installing h11 (0.16.0)
- Installing idna (3.10)
- Installing sniffio (1.3.1)
- Installing typing-extensions (4.13.2)
- Installing urllib3 (2.4.0)
- Installing annotated-types (0.7.0)
- Installing anyio (4.9.0)
- Installing httpcore (1.0.9)
- Installing pydantic-core (2.33.2)
- Installing requests (2.32.3)
- Installing typing-inspection (0.4.0)
- Installing httpx (0.28.1)
- Installing jsonpointer (3.0.0)
- Installing orjson (3.10.18)
- Installing packaging (24.2)
- Installing pydantic (2.11.4)
- Installing requests-toolbelt (1.0.0)
- Installing zstandard (0.23.0)
- Installing jsonpatch (1.33)
- Installing langsmith (0.3.42)
- Installing pyyaml (6.0.2)
- Installing tenacity (9.1.2)
- Installing greenlet (3.2.1)
- Installing langchain-core (0.3.58)
- Installing langchain-text-splitters (0.3.8)
- Installing sqlalchemy (2.0.40)
- Installing langchain (0.3.25)
Writing lock file
Installing the current project: project1 (0.1.0)
這時開啟 pyproject.toml 就可以看到 dependencies 不再是空串列, 多了 langchain :
[project]
name = "project1"
version = "0.1.0"
description = ""
authors = [
{name = "Your Name",email = "you@example.com"}
]
readme = "README.md"
requires-python = ">=3.12.2,<4.0"
dependencies = [
"langchain (>=0.3.25,<0.4.0)"
]
[tool.poetry]
packages = [{include = "project1", from = "src"}]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
然後就可以用 poetry shell 指令進入 shell 介面來管理虛擬環境, 但是卻出現下列訊息 :
D:\python\langchain\project1>poetry shell
Looks like you're trying to use a Poetry command that is not available.
Since Poetry (2.0.0), the shell command is not installed by default. You can use,
- the new env activate command (recommended); or
- the shell plugin to install the shell command
Documentation: https://python-poetry.org/docs/managing-environments/#activating-the-environment
Note that the env activate command is not a direct replacement for shell command.
原來 Poetry 2.0.0 之後預設不再內建 poetry shell 指令, 須手動自行安裝 shell plugin, 指令如下 :
poetry self add poetry-plugin-shell
D:\python\langchain\project1>poetry self add poetry-plugin-shell
Using version ^1.0.1 for poetry-plugin-shell
Updating dependencies
Resolving dependencies... (9.4s)
Package operations: 3 installs, 1 update, 0 removals
- Installing ptyprocess (0.7.0)
- Updating virtualenv (20.30.0 -> 20.31.1)
- Installing pexpect (4.9.0)
- Installing poetry-plugin-shell (1.0.1)
Writing lock file
重新輸入 poetry shell 指令即可進入虛擬環境的 Shell 介面了 :
D:\python\langchain\project1>poetry shell
我們可以在此虛擬環境用 poetry add 安裝 Python 套件, 執行 Python 程式等 :
(project1-py3.12) D:\python\langchain\project1>python --version
Python 3.12.2
例如安裝最好用的專業級 Python Web 開發框架 streamlit :
(project1-py3.12) D:\python\langchain\project1>poetry add streamlit
Using version ^1.45.0 for streamlit
Updating dependencies
Resolving dependencies... (4.7s)
Package operations: 30 installs, 0 updates, 0 removals
- Installing attrs (25.3.0)
- Installing rpds-py (0.24.0)
- Installing referencing (0.36.2)
- Installing jsonschema-specifications (2025.4.1)
- Installing markupsafe (3.0.2)
- Installing six (1.17.0)
- Installing smmap (5.0.2)
- Installing colorama (0.4.6)
- Installing gitdb (4.0.12)
- Installing jinja2 (3.1.6)
- Installing jsonschema (4.23.0)
- Installing narwhals (1.38.0)
- Installing numpy (2.2.5)
- Installing python-dateutil (2.9.0.post0)
- Installing pytz (2025.2)
- Installing tzdata (2025.2)
- Installing altair (5.5.0)
- Installing blinker (1.9.0)
- Installing cachetools (5.5.2)
- Installing click (8.1.8)
- Installing gitpython (3.1.44)
- Installing pandas (2.2.3)
- Installing pillow (11.2.1)
- Installing protobuf (6.30.2)
- Installing pyarrow (20.0.0)
- Installing pydeck (0.9.1)
- Installing toml (0.10.2)
- Installing tornado (6.4.2)
- Installing watchdog (6.0.0)
- Installing streamlit (1.45.0)
Writing lock file
接下來安裝另一個常用的 Python Web 開發框架 gradio :
(project1-py3.12) D:\python\langchain\project1>poetry add gradio
Using version ^5.29.0 for gradio
Updating dependencies
Resolving dependencies... (4.7s)
Package operations: 25 installs, 0 updates, 0 removals
- Installing mdurl (0.1.2)
- Installing filelock (3.18.0)
- Installing fsspec (2025.3.2)
- Installing markdown-it-py (3.0.0)
- Installing pygments (2.19.1)
- Installing tqdm (4.67.1)
- Installing huggingface-hub (0.30.2)
- Installing rich (14.0.0)
- Installing shellingham (1.5.4)
- Installing starlette (0.46.2)
- Installing websockets (15.0.1)
- Installing aiofiles (24.1.0)
- Installing fastapi (0.115.12)
- Installing ffmpy (0.5.0)
- Installing gradio-client (1.10.0)
- Installing groovy (0.1.2)
- Installing pydub (0.25.1)
- Installing python-multipart (0.0.20)
- Installing ruff (0.11.8)
- Installing safehttpx (0.1.6)
- Installing semantic-version (2.10.0)
- Installing tomlkit (0.13.2)
- Installing typer (0.15.3)
- Installing uvicorn (0.34.2)
- Installing gradio (5.29.0)
Writing lock file
這時再次開啟專案設定檔 pyproject.toml 就可以看到 dependencies 串列內紀錄了已安裝的四個 Python 套件 : langchain, requests, streamlit, 與 gradio :
dependencies = [
"langchain (>=0.3.25,<0.4.0)",
"requests (>=2.32.3,<3.0.0)",
"streamlit (>=1.45.0,<2.0.0)",
"gradio (>=5.29.0,<6.0.0)"
]
輸入 exit 即可離開虛擬環境 :
(project1-py3.12) D:\python\langchain\project1>exit
D:\python\langchain\project1>
嗯, 實際動手做一遍發現 Poetry 真的蠻好用的, 安裝與管理套件的方式乾淨俐落, 也不用再面對版本互相干擾的問題了.


沒有留言:
張貼留言