pip install poetry
# 更新系統套件
sudo apt update && sudo apt upgrade -y
# 安裝 Poetry
sudo apt install python3-poetry -y
poetry --version
poetry config virtualenvs.in-project true
poetry init
執行後將進入互動式設定:
This command will guide you through creating your pyproject.toml config.
Package name [my-project]:
Version [0.1.0]:
Description []:
Author [Your Name <your_email@example.com>, n to skip]:
License []:
Compatible Python versions [^3.10]:
Would you like to define your main dependencies interactively? (yes/no) [yes]
輸入專案名稱、版本、相容的 Python 版本後,Poetry 會自動建立 pyproject.toml
檔案。
poetry env info
輸出範例:
Virtualenv
Python: 3.10.12
Implementation: CPython
Path: /home/ubuntu/my-project/.venv
Executable: /home/ubuntu/my-project/.venv/bin/python
若未顯示虛擬環境路徑,可手動建立:
poetry env use python3
成功建立後,會顯示虛擬環境的路徑。
source .venv/bin/activate
poetry install
如果只想安裝相依套件(不包含當前專案),可執行:
poetry install --no-root
1️⃣ 按下 Ctrl + Shift + P
開啟命令面板,選擇 Python: Select Interpreter。
2️⃣ 選擇工作區層級的 Python 解析器。
3️⃣ 選擇 Poetry 虛擬環境的 Python 執行檔:
/path/to/project/.venv/bin/python
python3 manage.py runserver
python3 manage.py runserver 0.0.0.0:8000
poetry env remove python
rm pyproject.toml poetry.lock
poetry init