ããã¯ããªã«ãããããŠæžãããã®ïŒ
ããããPythonã®ãããžã§ã¯ã管çããŒã«ãã²ãšã€èŠããŠãããã®ã§ã¯ãšæã£ãŠããã®ã§ãããä»ããæ±ããªãuvãããããã ã£ãã®ã§
ã€ã³ã¹ããŒã«ããŠã¿ãããšã«ããŸããã
Poetryãããã¯æ©äŒããã£ãããŸãè©ŠããšããŸãã
uv
uvã®Webãµã€ãã¯ãã¡ãã
GitHubãªããžããªãŒã¯ãã¡ãã§ããã
GitHub - astral-sh/uv: An extremely fast Python package and project manager, written in Rust.
uvã¯Rustã§æžãããé«éãªPythonã®ããã±ãŒãžãšãããžã§ã¯ãã®ç®¡çããŒã«ãšãããŠããŸãã
An extremely fast Python package and project manager, written in Rust.
ç¹åŸŽã¯ãã¡ãã
- pipãpip-toolsãpipxãPoetryãpyenvãtwineãvirtualenvãªã©ã眮ãæããåäžã®ããŒã«
- pipãã10ã100åé«é
- Pythonã®ã€ã³ã¹ããŒã«ããã³ããŒãžã§ã³ç®¡çãå¯èœ
- Pythonã¢ããªã±ãŒã·ã§ã³ã®å®è¡ãšã€ã³ã¹ããŒã«ãå¯èœ
- ã€ã³ã©ã€ã³ã®äŸåé¢ä¿ã¡ã¿ããŒã¿ã䜿çšããŠã¹ã¯ãªãããå®è¡å¯èœ
- ãŠãããŒãµã«ããã¯ãã¡ã€ã«ã䜿ã£ãŠå æ¬çãªãããžã§ã¯ã管çãæäŸ
- pipäºæã€ã³ã¿ãŒãã§ãŒã¹ã®æäŸ
- Cargoã¹ã¿ã€ã«ã®ã¯ãŒã¯ã¹ããŒã¹ã®ãµããŒã
- äŸåé¢ä¿ã®éè€æé€ã®ããã®ã°ããŒãã«ãã£ãã·ã¥ãåãããã£ã¹ã¯é åãå¹ççã«äœ¿çš
- curlãŸãã¯pipçµç±ã§ã€ã³ã¹ããŒã«ã§ããã€ã³ã¹ããŒã«ã«RustãPythonã¯äžèŠ
- macOSãLinuxãWindowsããµããŒã
ãããžã§ã¯ã管çããŒã«ã ãšæã£ãŠããã®ã§ãããPythonèªäœã®ã€ã³ã¹ããŒã«ã
ã¹ã¯ãªããã®å®è¡ã
PythonããŒã«ã®ã€ã³ã¹ããŒã«ã
ãããžã§ã¯ãã®ç®¡çãšãããããšã§ãããã§ããã
pipãšã®äºæã€ã³ã¿ãŒãã§ãŒã¹ãåããŸãã
ã³ãã³ãã®ãªãã¡ã¬ã³ã¹ã¯ãã¡ãã
èšå®ãã¡ã€ã«ã®ãªãã¡ã¬ã³ã¹ïŒproject.toml
ïŒã¯ãã¡ãã
ããã¥ã¡ã³ããèŠãã®ã¯ãããããã«ããŠãè©ŠããŠã¿ãããšæããŸãã
ç°å¢
ä»åã®ç°å¢ã¯ãã¡ãã
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 24.04.1 LTS Release: 24.04 Codename: noble $ uname -srvmpio Linux 6.8.0-51-generic #52-Ubuntu SMP PREEMPT_DYNAMIC Thu Dec 5 13:09:44 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
Ubuntu Linux 24.04 LTSã§ãã
uvãã€ã³ã¹ããŒã«ãã
ãŸãã¯uvãã€ã³ã¹ããŒã«ããŸãã
ã€ã³ã¹ããŒã«æ¹æ³ã¯ããã€ããããŸãããcurlã§ã€ã³ã¹ããŒã«ããããšã«ããŸãã
$ curl -LsSf https://astral.sh/uv/install.sh | sh
Installing uv / Installation methods / Standalone installer
$HOME/.local/bin
ã«ã€ã³ã¹ããŒã«ãããããã§ãã
downloading uv 0.5.12 x86_64-unknown-linux-gnu no checksums to verify installing to $HOME/.local/bin uv uvx everything's installed! $ which uv $HOME/.local/bin/uv
ããŒãžã§ã³ã確èªã
$ uv --version uv 0.5.12
ãã«ãã衚瀺ã
$ uv help An extremely fast Python package manager. Usage: uv [OPTIONS] <COMMAND> Commands: run Run a command or script init Create a new project add Add dependencies to the project remove Remove dependencies from the project sync Update the project's environment lock Update the project's lockfile export Export the project's lockfile to an alternate format tree Display the project's dependency tree tool Run and install commands provided by Python packages python Manage Python versions and installations pip Manage Python packages with a pip-compatible interface venv Create a virtual environment build Build Python packages into source distributions and wheels publish Upload distributions to an index cache Manage uv's cache self Manage the uv executable version Display uv's version generate-shell-completion Generate shell completion help Display documentation for a command Cache options: -n, --no-cache Avoid reading from or writing to the cache, instead using a temporary directory for the duration of the operation [env: UV_NO_CACHE=] --cache-dir <CACHE_DIR> Path to the cache directory [env: UV_CACHE_DIR=] Python options: --python-preference <PYTHON_PREFERENCE> Whether to prefer uv-managed or system Python installations [env: UV_PYTHON_PREFERENCE=] [possible values: only-managed, managed, system, only-system] --no-python-downloads Disable automatic downloads of Python. [env: "UV_PYTHON_DOWNLOADS=never"] Global options: -q, --quiet Do not print any output -v, --verbose... Use verbose output --color <COLOR_CHOICE> Control colors in output [default: auto] [possible values: auto, always, never] --native-tls Whether to load TLS certificates from the platform's native certificate store [env: UV_NATIVE_TLS=] --offline Disable network access [env: UV_OFFLINE=] --allow-insecure-host <ALLOW_INSECURE_HOST> Allow insecure connections to a host [env: UV_INSECURE_HOST=] --no-progress Hide all progress outputs [env: UV_NO_PROGRESS=] --directory <DIRECTORY> Change to the given directory prior to running the command --project <PROJECT> Run the command within the given project directory --config-file <CONFIG_FILE> The path to a `uv.toml` file to use for configuration [env: UV_CONFIG_FILE=] --no-config Avoid discovering configuration files (`pyproject.toml`, `uv.toml`) [env: UV_NO_CONFIG=] -h, --help Display the concise help for this command -V, --version Display the uv version Use `uv help <command>` for more information on a specific command.
ã·ã§ã«ã®ãªãŒãã³ã³ããªãŒããèšå®ãã
ã·ã§ã«ã§ã®ãªãŒãã³ã³ããªãŒããã§ããããã§ãã
Installing uv / Shell autocompletion
èªåã®å Žåã¯bashãªã®ã§ããã¡ãã§ããã
$ echo 'eval "$(uv generate-shell-completion bash)"' >> ~/.bashrc $ echo 'eval "$(uvx --generate-shell-completion bash)"' >> ~/.bashrc
èšå®ããããã·ã§ã«ãèµ·åãçŽããŸãã
uvãã¢ããããŒããã
uvèªèº«ãã¢ããããŒãããã«ã¯ãuv self update
ãå®è¡ããã°ããã¿ããã§ãã
$ uv self update
䜿çšããPythonãã€ã³ã¹ããŒã«ãã
Pythonã®ã€ã³ã¹ããŒã«ã«ã€ããŠã¯ãã¡ãã
ã²ãšãŸãuv python list
ã§ãçŸåšã€ã³ã¹ããŒã«ãããŠããPythonãèŠãŠã¿ãŸããèªåã®ç°å¢ã§ã¯ãããªããŸããã
$ uv python list cpython-3.13.1+freethreaded-linux-x86_64-gnu <download available> cpython-3.13.1-linux-x86_64-gnu <download available> cpython-3.12.8-linux-x86_64-gnu <download available> cpython-3.12.3-linux-x86_64-gnu /usr/bin/python3.12 cpython-3.12.3-linux-x86_64-gnu /usr/bin/python3 -> python3.12 cpython-3.12.3-linux-x86_64-gnu /bin/python3.12 cpython-3.12.3-linux-x86_64-gnu /bin/python3 -> python3.12 cpython-3.11.11-linux-x86_64-gnu <download available> cpython-3.10.16-linux-x86_64-gnu <download available> cpython-3.9.21-linux-x86_64-gnu <download available> cpython-3.8.20-linux-x86_64-gnu <download available> cpython-3.7.9-linux-x86_64-gnu <download available> pypy-3.10.14-linux-x86_64-gnu <download available> pypy-3.9.19-linux-x86_64-gnu <download available> pypy-3.8.16-linux-x86_64-gnu <download available> pypy-3.7.13-linux-x86_64-gnu <download available>
Ubuntu Linux 24.04 LTSã®ããã±ãŒãžã§ã€ã³ã¹ããŒã«ããŠããPython 3.12ãèªèãããŠããŸãã
確èªã
$ uv run python -c 'import sys; print(sys.version)' 3.12.3 (main, Nov 6 2024, 18:32:19) [GCC 13.2.0]
ãã¹ãŠã®ããŒãžã§ã³ã衚瀺ããå Žåã¯ãã¡ãã
$ uv python list --all-versions
ä»ã®ãã©ãããã©ãŒã ã®ãã®ãå«ãå Žåã¯ãã¡ãã
$ uv python list --all-platforms
ã€ã³ã¹ããŒã«ããŠãããã®ã®ã¿ã衚瀺ããå Žåã¯ãã¡ãã
$ uv python list --only-installed
Python 3.13ãã€ã³ã¹ããŒã«ã
$ uv python install 3.13
è¿œå ãããŸããã
$ uv python list cpython-3.13.1+freethreaded-linux-x86_64-gnu <download available> cpython-3.13.1-linux-x86_64-gnu .local/share/uv/python/cpython-3.13.1-linux-x86_64-gnu/bin/python3.13 cpython-3.12.8-linux-x86_64-gnu <download available> cpython-3.12.3-linux-x86_64-gnu /usr/bin/python3.12 cpython-3.12.3-linux-x86_64-gnu /usr/bin/python3 -> python3.12 cpython-3.12.3-linux-x86_64-gnu /bin/python3.12 cpython-3.12.3-linux-x86_64-gnu /bin/python3 -> python3.12 cpython-3.11.11-linux-x86_64-gnu <download available> cpython-3.10.16-linux-x86_64-gnu <download available> cpython-3.9.21-linux-x86_64-gnu <download available> cpython-3.8.20-linux-x86_64-gnu <download available> cpython-3.7.9-linux-x86_64-gnu <download available> pypy-3.10.14-linux-x86_64-gnu <download available> pypy-3.9.19-linux-x86_64-gnu <download available> pypy-3.8.16-linux-x86_64-gnu <download available> pypy-3.7.13-linux-x86_64-gnu <download available>
ãã€ããŒããŒãžã§ã³ã®ã¿ã®æå®ã ãšãã®æã®ææ°ã®ãããããŒãžã§ã³ãã€ã³ã¹ããŒã«ãããããã§ããããã£ãšçŽ°ããæå®ããŠã
ã€ã³ã¹ããŒã«ã§ããã¿ããã§ãã
$ uv python install 3.11.9 $ uv python list cpython-3.13.1+freethreaded-linux-x86_64-gnu <download available> cpython-3.13.1-linux-x86_64-gnu .local/share/uv/python/cpython-3.13.1-linux-x86_64-gnu/bin/python3.13 cpython-3.12.8-linux-x86_64-gnu <download available> cpython-3.12.3-linux-x86_64-gnu /usr/bin/python3.12 cpython-3.12.3-linux-x86_64-gnu /usr/bin/python3 -> python3.12 cpython-3.12.3-linux-x86_64-gnu /bin/python3.12 cpython-3.12.3-linux-x86_64-gnu /bin/python3 -> python3.12 cpython-3.11.11-linux-x86_64-gnu <download available> cpython-3.11.9-linux-x86_64-gnu .local/share/uv/python/cpython-3.11.9-linux-x86_64-gnu/bin/python3.11 cpython-3.10.16-linux-x86_64-gnu <download available> cpython-3.9.21-linux-x86_64-gnu <download available> cpython-3.8.20-linux-x86_64-gnu <download available> cpython-3.7.9-linux-x86_64-gnu <download available> pypy-3.10.14-linux-x86_64-gnu <download available> pypy-3.9.19-linux-x86_64-gnu <download available> pypy-3.8.16-linux-x86_64-gnu <download available> pypy-3.7.13-linux-x86_64-gnu <download available>
ã¡ãªã¿ã«æ瀺çã«ã€ã³ã¹ããŒã«ããªããŠããã³ãã³ãã«--python
ãªãã·ã§ã³ãæå®ããŠå®è¡ããæã«ãã®ããŒãžã§ã³ã®Pythonã
ã€ã³ã¹ããŒã«ãããŠããªããã°èªåã§ããŠã³ããŒãããŠããã¿ããã§ãã
Python versions / Requesting a version
䜿çšããPythonãåºå®ãã
䜿çšããPythonã®ããŒãžã§ã³ãåºå®ããã«ã¯ãuv python pin
ã䜿ãããã§ãã
$ uv python pin 3.13.1 Pinned `.python-version` to `3.13.1`
Python versions / Requesting a version / Python version files
.python-version
ãšãããã¡ã€ã«ãã§ããŸãã
.python-version
3.13.1
確èªã
$ uv run python -c 'import sys; print(sys.version)' 3.13.1 (main, Dec 19 2024, 14:32:25) [Clang 18.1.8 ]
確ãã«åãæ¿ãããŸããã
ãããžã§ã¯ããæ±ã
ã§ã¯ããããžã§ã¯ããæ±ã£ãŠã¿ãŸãããã
ãããžã§ã¯ããäœæãã
uv init
ã§ãããžã§ã¯ããäœæããŸãã
$ uv init hello-world
Working on projects / Creating a new project
ãããžã§ã¯ãå ãžç§»åã
$ cd hello-world
äœæããããã¡ã€ã«ã
$ tree -a . âââ .git â  âââ HEAD â  âââ branches â  âââ config â  âââ description â  âââ hooks â  â  âââ applypatch-msg.sample â  â  âââ commit-msg.sample â  â  âââ fsmonitor-watchman.sample â  â  âââ post-update.sample â  â  âââ pre-applypatch.sample â  â  âââ pre-commit.sample â  â  âââ pre-merge-commit.sample â  â  âââ pre-push.sample â  â  âââ pre-rebase.sample â  â  âââ pre-receive.sample â  â  âââ prepare-commit-msg.sample â  â  âââ push-to-checkout.sample â  â  âââ sendemail-validate.sample â  â  âââ update.sample â  âââ info â  â  âââ exclude â  âââ objects â  â  âââ info â  â  âââ pack â  âââ refs â  âââ heads â  âââ tags âââ .gitignore âââ .python-version âââ README.md âââ hello.py âââ pyproject.toml 11 directories, 23 files
GitãªããžããªãŒãšããŠäœæããããã§ããããã®ãããã¯--vcs
ãªãã·ã§ã³ã§å¶åŸ¡ã§ãããã§ãã
uvã«é¢ä¿ãããšæããããã¡ã€ã«ãèŠãŠãããŸãããã
.python-version
3.12
pyproject.toml
[project] name = "hello-world" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.12" dependencies = []
ãµã³ãã«ã¹ã¯ãªãããçæãããŠããŸãã
hello.py def main(): print("Hello from hello-world!") if __name__ == "__main__": main()
å®è¡ããŠã¿ãŸãããã
$ uv run hello.py Using CPython 3.12.3 interpreter at: /usr/bin/python3.12 Creating virtual environment at: .venv Hello from hello-world!
ååã¯ä»®æ³ç°å¢ãäœæããããã§ãã
ãŸãuv.lock
ãšãããã¡ã€ã«ãçæãããããã§ãã
uv.lock
version = 1 requires-python = ">=3.12" [[package]] name = "hello-world" version = "0.1.0" source = { virtual = "." }
2åç®ããã¯è¡šç€ºãããªããªããŸããã
$ uv run hello.py Hello from hello-world!
äŸåé¢ä¿ã管çãã
次ã¯äŸåé¢ä¿ã管çããŠã¿ãŸãã
Working on projects / Managing dependencies
ãµã³ãã«ãšããããšã§ãPyYAMLãã€ã³ã¹ããŒã«ããŠã¿ãŸãã
$ uv add pyyaml
pyproject.toml
ãæŽæ°ãããŸããã
pyproject.toml
[project] name = "hello-world" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.12" dependencies = [ "pyyaml>=6.0.2", ]
uv.lock
ãæŽæ°ãããŠããŸãã
uv.lock
version = 1 requires-python = ">=3.12" [[package]] name = "hello-world" version = "0.1.0" source = { virtual = "." } dependencies = [ { name = "pyyaml" }, ] [package.metadata] requires-dist = [{ name = "pyyaml", specifier = ">=6.0.2" }] [[package]] name = "pyyaml" version = "6.0.2" source = { registry = "https://pypi.org/simple" } sdist = { url = "https://files.pythonhosted.org/packages/54/ed/79a089b6be93607fa5cdaedf301d7dfb23af5f25c398d5ead2525b063e17/pyyaml-6.0.2.tar.gz", hash = "sha256:d584d9ec91ad65861cc08d42e834324ef890a082e591037abe114850ff7bbc3e", size = 130631 } wheels = [ { url = "https://files.pythonhosted.org/packages/86/0c/c581167fc46d6d6d7ddcfb8c843a4de25bdd27e4466938109ca68492292c/PyYAML-6.0.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:c70c95198c015b85feafc136515252a261a84561b7b1d51e3384e0655ddf25ab", size = 183873 }, { url = "https://files.pythonhosted.org/packages/a8/0c/38374f5bb272c051e2a69281d71cba6fdb983413e6758b84482905e29a5d/PyYAML-6.0.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:ce826d6ef20b1bc864f0a68340c8b3287705cae2f8b4b1d932177dcc76721725", size = 173302 }, { url = "https://files.pythonhosted.org/packages/c3/93/9916574aa8c00aa06bbac729972eb1071d002b8e158bd0e83a3b9a20a1f7/PyYAML-6.0.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1f71ea527786de97d1a0cc0eacd1defc0985dcf6b3f17bb77dcfc8c34bec4dc5", size = 739154 }, { url = "https://files.pythonhosted.org/packages/95/0f/b8938f1cbd09739c6da569d172531567dbcc9789e0029aa070856f123984/PyYAML-6.0.2-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:9b22676e8097e9e22e36d6b7bda33190d0d400f345f23d4065d48f4ca7ae0425", size = 766223 }, { url = "https://files.pythonhosted.org/packages/b9/2b/614b4752f2e127db5cc206abc23a8c19678e92b23c3db30fc86ab731d3bd/PyYAML-6.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:80bab7bfc629882493af4aa31a4cfa43a4c57c83813253626916b8c7ada83476", size = 767542 }, { url = "https://files.pythonhosted.org/packages/d4/00/dd137d5bcc7efea1836d6264f049359861cf548469d18da90cd8216cf05f/PyYAML-6.0.2-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:0833f8694549e586547b576dcfaba4a6b55b9e96098b36cdc7ebefe667dfed48", size = 731164 }, { url = "https://files.pythonhosted.org/packages/c9/1f/4f998c900485e5c0ef43838363ba4a9723ac0ad73a9dc42068b12aaba4e4/PyYAML-6.0.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8b9c7197f7cb2738065c481a0461e50ad02f18c78cd75775628afb4d7137fb3b", size = 756611 }, { url = "https://files.pythonhosted.org/packages/df/d1/f5a275fdb252768b7a11ec63585bc38d0e87c9e05668a139fea92b80634c/PyYAML-6.0.2-cp312-cp312-win32.whl", hash = "sha256:ef6107725bd54b262d6dedcc2af448a266975032bc85ef0172c5f059da6325b4", size = 140591 }, { url = "https://files.pythonhosted.org/packages/0c/e8/4f648c598b17c3d06e8753d7d13d57542b30d56e6c2dedf9c331ae56312e/PyYAML-6.0.2-cp312-cp312-win_amd64.whl", hash = "sha256:7e7401d0de89a9a855c839bc697c079a4af81cf878373abd7dc625847d25cbd8", size = 156338 }, { url = "https://files.pythonhosted.org/packages/ef/e3/3af305b830494fa85d95f6d95ef7fa73f2ee1cc8ef5b495c7c3269fb835f/PyYAML-6.0.2-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:efdca5630322a10774e8e98e1af481aad470dd62c3170801852d752aa7a783ba", size = 181309 }, { url = "https://files.pythonhosted.org/packages/45/9f/3b1c20a0b7a3200524eb0076cc027a970d320bd3a6592873c85c92a08731/PyYAML-6.0.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:50187695423ffe49e2deacb8cd10510bc361faac997de9efef88badc3bb9e2d1", size = 171679 }, { url = "https://files.pythonhosted.org/packages/7c/9a/337322f27005c33bcb656c655fa78325b730324c78620e8328ae28b64d0c/PyYAML-6.0.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0ffe8360bab4910ef1b9e87fb812d8bc0a308b0d0eef8c8f44e0254ab3b07133", size = 733428 }, { url = "https://files.pythonhosted.org/packages/a3/69/864fbe19e6c18ea3cc196cbe5d392175b4cf3d5d0ac1403ec3f2d237ebb5/PyYAML-6.0.2-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:17e311b6c678207928d649faa7cb0d7b4c26a0ba73d41e99c4fff6b6c3276484", size = 763361 }, { url = "https://files.pythonhosted.org/packages/04/24/b7721e4845c2f162d26f50521b825fb061bc0a5afcf9a386840f23ea19fa/PyYAML-6.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:70b189594dbe54f75ab3a1acec5f1e3faa7e8cf2f1e08d9b561cb41b845f69d5", size = 759523 }, { url = "https://files.pythonhosted.org/packages/2b/b2/e3234f59ba06559c6ff63c4e10baea10e5e7df868092bf9ab40e5b9c56b6/PyYAML-6.0.2-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:41e4e3953a79407c794916fa277a82531dd93aad34e29c2a514c2c0c5fe971cc", size = 726660 }, { url = "https://files.pythonhosted.org/packages/fe/0f/25911a9f080464c59fab9027482f822b86bf0608957a5fcc6eaac85aa515/PyYAML-6.0.2-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:68ccc6023a3400877818152ad9a1033e3db8625d899c72eacb5a668902e4d652", size = 751597 }, { url = "https://files.pythonhosted.org/packages/14/0d/e2c3b43bbce3cf6bd97c840b46088a3031085179e596d4929729d8d68270/PyYAML-6.0.2-cp313-cp313-win32.whl", hash = "sha256:bc2fa7c6b47d6bc618dd7fb02ef6fdedb1090ec036abab80d4681424b84c1183", size = 140527 }, { url = "https://files.pythonhosted.org/packages/fa/de/02b54f42487e3d3c6efb3f89428677074ca7bf43aae402517bc7cca949f3/PyYAML-6.0.2-cp313-cp313-win_amd64.whl", hash = "sha256:8388ee1976c416731879ac16da0aff3f63b286ffdd57cdeb95f3f2e085687563", size = 156446 }, ]
æåã«äœæãããŠããhello.py
ãããæžãæããŠã¿ãŸãã
hello.py
import yaml def load_yaml() -> dict: yaml_document = """ a: 1 b: c: 3 d: 4 """ return yaml.load(yaml_document, Loader=yaml.CLoader) if __name__ == "__main__": print(load_yaml())
å®è¡ã
$ uv run hello.py {'a': 1, 'b': {'c': 3, 'd': 4}}
OKã§ããã
次ã¯ãã¹ãçšã®äŸåé¢ä¿ãšããŠãpytestãè¿œå ããŸãããã®å Žå--dev
ãªãã·ã§ã³ãã€ããŠå®è¡ããŸãã
$ uv add --dev pytest
pyproject.toml
ã¯ãããªããŸããã
pyproject.toml
[project] name = "hello-world" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.12" dependencies = [ "pyyaml>=6.0.2", ] [dependency-groups] dev = [ "pytest>=8.3.4", ]
ãã¹ãã³ãŒããäœæã
test_hello.py
from hello import load_yaml def test_hello() -> None: assert load_yaml() == {"a": 1, "b": {"c": 3, "d": 4}}
uv run
ã§pytestãå®è¡ã
$ uv run pytest ============================================================================ test session starts ============================================================================ platform linux -- Python 3.12.3, pytest-8.3.4, pluggy-1.5.0 rootdir: /path/to/hello-world configfile: pyproject.toml collected 1 item test_hello.py . [100%] ============================================================================= 1 passed in 0.03s =============================================================================
OKã§ããã
äŸåé¢ä¿ãã¢ããã°ã¬ãŒãããå Žåã¯uv lock --upgrade-package
ã§ãã
$ uv lock --upgrade-package pyyaml $ uv lock --upgrade-package pytest
äŸåé¢ä¿ãåé€ããå Žåã¯ãuv remove
ã§ãã
$ uv remove pyyaml $ uv remove --dev pytest
ãããžã§ã¯ãå®çŸ©ããäŸåé¢ä¿ãã€ã³ã¹ããŒã«ãã
ãªããžããªãŒãcloneããåŸãªã©ããããžã§ã¯ãã®å®çŸ©ã®ã¿ã®å ŽåããäŸåé¢ä¿ãã€ã³ã¹ããŒã«ããã«ã¯uv sync
ã䜿ãã°ããããã§ãã
$ uv sync
ãã ãuvã®ã³ãã³ããå®è¡ãããšèªåçã«è¡ããããããªã®ã§æ°ã«ããªããŠãããããããŸããã
requirements.txtãäœæãã
requirements.txt
ã欲ãããªãæããããšæããŸããããã¯uv pip compile
ã§äœæã§ããŸãã
$ uv pip compile pyproject.toml -o requirements.txt
çµæã
requirements.txt
# This file was autogenerated by uv via the following command: # uv pip compile pyproject.toml -o requirements.txt pyyaml==6.0.2 # via hello-world (pyproject.toml)
éçºçšã®äŸåé¢ä¿ã¯é€å€ãããŠãããšãããè¯ãã§ããã
[project] name = "hello-world" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.12" dependencies = [ "pyyaml>=6.0.2", ] [dependency-groups] dev = [ "pytest>=8.3.4", ]
ããŒã«ã䜿ã
uvã§ã¯Pythonããã±ãŒãžã§æäŸãããŠããããŒã«ã䜿ãããšãã§ããŸãã
ããã§ã¯mypyã䜿ã£ãŠã¿ãŸãããã®æ䜿ãã³ãã³ãã¯uvx
ïŒuv tool run
ã®ãšã€ãªã¢ã¹ïŒã§ãã
$ uvx mypy
mypyãããŠã³ããŒããããå®è¡ã§ããããã«ãªããŸãã
$ uvx mypy --version mypy 1.14.0 (compiled: yes)
å®è¡ã
$ uvx mypy --disallow-untyped-defs . hello.py:1: error: Library stubs not installed for "yaml" [import-untyped] hello.py:1: note: Hint: "python3 -m pip install types-PyYAML" hello.py:1: note: (or run "mypy --install-types" to install all missing stub packages) hello.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports Found 1 error in 1 file (checked 2 source files)
PyYAMLã®åã¢ãããŒã·ã§ã³ããªããšæãããŸããããã®å Žåã--with
ã§äŸåé¢ä¿ãè¿œå ã§ããããã§ãã
$ uvx --with types-PyYAML mypy --disallow-untyped-defs .
Tools / Tool versions / Including additional dependencies
ããã±ãŒãžãã€ã³ã¹ããŒã«ãã€ã€ãå®è¡ã§ããŸããã
$ uvx --with types-PyYAML mypy --disallow-untyped-defs . Installed 4 packages in 29ms Success: no issues found in 2 source files
ããŒã«ã®èšå®ããããã£ãããpyproject.toml
ã«ãããªæãã§æžãã¿ããã§ãã
[tool.mypy] disallow_untyped_defs = true
ãããžã§ã¯ãå ã®äŸåé¢ä¿ã«è¿œå ãã
ãšããã§uvx --with types-PyYAML mypy
ã®ããã«å®è¡ããŸããããããã¡ãã£ãšåŸ®åŠã§ããã
ãããžã§ã¯ãã®éçºçšããã±ãŒãžãšããŠè¿œå ããæ¹ãããã®ãããããŸããã
$ uv add --dev mypy types-PyYAML
ãã®å Žåãuv run
ã§å®è¡ããŸãã
$ uv run mypy .
ãã®åœ¢æ ã§ã以äžã®èšå®ã¯æå¹ã«ãªãããã§ãã
[tool.mypy] disallow_untyped_defs = true
ãã®ããã«ããŒã«ã®å®è¡ã«ãããžã§ã¯ãã®äŸåé¢ä¿ãå¿ èŠã«ãªãå Žåã¯ãuvxã§ã¯ãªãuv runã䜿ãããšããã¡ãã«ãæžãããŠããŸãã
If you are running a tool in a project and the tool requires that your project is installed, e.g., when using pytest or mypy, you'll want to use uv run instead of uvx. Otherwise, the tool will be run in a virtual environment that is isolated from your project.
ããŒã«ã®ããŒãžã§ã³ãåºå®ã§ããã®ã§ããã¡ãã®æ¹ãå人çã«ã¯å¥œã¿ãããããŸããã
ãããªãšããã§ããããã
ãããã«
Pythonã®ããã±ãŒãžããããžã§ã¯ã管çããŒã«ã§ããuvãè©ŠããŠã¿ãŸããã
ããããPythonã®ãããžã§ã¯ã管çããŒã«ãèŠããæ¹ããããã ãããªããšæã£ãŠããã®ã§ããæ©äŒã«ãªããŸããã
ä»åŸã¯uvã䜿ã£ãŠããããšæããŸãã