本文使用環境為 Apple Silicon,並使用 pyenv 安裝 Python 環境。
以下介紹我的 Python 環境安裝方法(適用於 MacOS 或 Linux):
依照 pyenv 的 Wiki 建議的環境建置,安裝相關系統依賴:
https://github.com/pyenv/pyenv/wiki
使用自動安裝腳本安裝 pyenv:
https://github.com/pyenv/pyenv-installer
安裝完成後最後會顯示類似以下訊息,並依照指示操作。
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by appending
# the following to
your shell's login startup file (for login shells)
and your shell's interactive startup file (for interactive shells) :
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
# Restart your shell for the changes to take effect.
# Load pyenv-virtualenv automatically by adding
# the following to your profile:
eval "$(pyenv virtualenv-init -)"
設定 Shell 環境:
https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv
如同步驟 2. 的安裝完成後的提示訊息操作,以 Zsh 為例,編輯 ~/.zprofile
加入以下內容:
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
重新啟動 Shell:
$
代表後面的內容為指令
$ exec "$SHELL"
安裝指定版本 Python:
$ pyenv install 3.11.1
設定環境的 Python 的版本:
# 全域環境預設的 Python 版本
$ pyenv global 3.11.1
# 設定某個目錄下,使用特定 Python 版本
$ pyenv local 3.11.1
檢查 Python:
$ python -V
Python 3.11.1
$ which python
/Users/user/.pyenv/shims/python
Windows 用戶可以使用 pyenv-win
https://github.com/pyenv-win/pyenv-win ,或是使用 WSL 依照上述方法安裝。
接下來安裝 Poetry
在特定的 pyenv 的下的 Python 版本,安裝 Poetry:
https://python-poetry.org/docs/#installation
$ curl -sSL https://install.python-poetry.org | python -
設定 Shell 環境:
以 Zsh 為例,編輯 ~/.zprofile
加入以下內容:
export PATH="$HOME/.local/bin:$PATH"
重新啟動 Shell:
$ exec "$SHELL"
檢查 Poetry:
$ poetry --version
Poetry (version 1.6.1)
最後建立一個練習 Strawberry 的環境
使用 poetry 建立專案:
建立專案目錄
$ mkdir strawberry-tutorial
切換到專案目錄內
$ cd strawberry-tutorial
初始化專案
$ poetry init
Package name [strawberry-tutorial]:
Version [0.1.0]:
Description []:
Author [username <email>, n to skip]:
License []:
Compatible Python versions [^3.11]:
Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file
[tool.poetry]
name = "strawberry-tutorial"
version = "0.1.0"
description = ""
authors = ["username <email@email.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Do you confirm generation? (yes/no) [yes]
安裝 Strawberry 套件:
$ poetry add 'strawberry-graphql[debug-server]'
依照 Strawberry 官方入門教學建立一個簡單的 GraphQL 範例程式:
在專案目錄下建立一個名為 schema.py
的 Python 檔,並貼上以下內容
import typing
import strawberry
@strawberry.type
class Book:
title: str
author: str
@strawberry.type
class Query:
books: typing.List[Book]
def get_books():
return [
Book(
title="The Great Gatsby",
author="F. Scott Fitzgerald",
),
]
@strawberry.type
class Query:
books: typing.List[Book] = strawberry.field(resolver=get_books)
schema = strawberry.Schema(query=Query)
使用 poetry 啟用 Python 虛擬環境
$ poetry shell
啟動 strawberry debug server:
$ strawberry server schema
Running strawberry on http://0.0.0.0:8000/graphql 🍓
使用網頁瀏覽器開啟 http://0.0.0.0:8000/graphql
strawberry-tutorial
目錄下的內容
.
├── .python-version
├── poetry.lock
├── pyproject.toml
└── schema.py
.python-version
的內容,用於 pyenv 指定特定 Python 版本
3.11.3
pyproject.toml
的內容,poetry 的設定檔
[tool.poetry]
name = "strawberry-tutorial"
version = "0.1.0"
description = ""
authors = ["username <email@email.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
strawberry-graphql = {extras = ["debug-server"], version = "^0.205.0"}
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
poetry.lock
poetry 鎖定安裝的套件版本以及相關依賴的套件版本資訊