{"id":248,"date":"2026-05-25T14:51:18","date_gmt":"2026-05-25T06:51:18","guid":{"rendered":"https:\/\/www.handytoo.com\/?p=248"},"modified":"2026-05-25T18:38:36","modified_gmt":"2026-05-25T10:38:36","slug":"%e5%91%8a%e5%88%ab-pip-venv-%e7%a2%8e%e7%89%87%e5%8c%96%ef%bc%81uv-%e9%a9%b1%e5%8a%a8%e7%9a%84-python-%e7%8e%b0%e4%bb%a3%e9%a1%b9%e7%9b%ae%e5%b7%a5%e4%bd%9c%e6%b5%81%e5%85%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.handytoo.com\/?p=248","title":{"rendered":"\u544a\u522b pip\/venv \u788e\u7247\u5316\uff01UV \u9a71\u52a8\u7684 Python \u73b0\u4ee3\u9879\u76ee\u5de5\u4f5c\u6d41\u5168\u6559\u7a0b\uff08\u57fa\u4e8e pyproject.toml\uff09"},"content":{"rendered":"\n<p>\u957f\u671f\u4ee5\u6765\uff0cPython \u5f00\u53d1\u8005\u9971\u53d7 <code>pip + venv + requirements.txt<\/code> \u788e\u7247\u5316\u5de5\u4f5c\u6d41\u7684\u56f0\u6270\uff1a\u73af\u5883\u642d\u5efa\u6162\u3001\u4f9d\u8d56\u7248\u672c\u6df7\u4e71\u3001\u5f00\u53d1\/\u751f\u4ea7\u4f9d\u8d56\u65e0\u6cd5\u533a\u5206\u3001\u9879\u76ee\u914d\u7f6e\u65e0\u7edf\u4e00\u6807\u51c6\u3002\u5982\u4eca\u57fa\u4e8e PEP \u6807\u51c6\u5316\u7684 <code>pyproject.toml<\/code> \u5df2\u6210 Python \u9879\u76ee\u6807\u914d\uff0c\u800c <strong>UV<\/strong> \u4f5c\u4e3a\u6781\u901f\u4e00\u4f53\u5316\u5de5\u5177\uff0c\u5f7b\u5e95\u91cd\u6784\u4e86 Python \u9879\u76ee\u5f00\u53d1\u3001\u4f9d\u8d56\u7ba1\u7406\u3001\u73af\u5883\u7ef4\u62a4\u3001\u6253\u5305\u53d1\u5e03\u7684\u5168\u6d41\u7a0b\u3002\u672c\u6587\u5e26\u4f60\u4ece\u96f6\u642d\u5efa<strong>\u57fa\u4e8e\u9879\u76ee\u7684\u73b0\u4ee3\u5316 UV \u5de5\u4f5c\u6d41<\/strong>\uff0c\u5403\u900f <code>pyproject.toml<\/code> \u6838\u5fc3\u914d\u7f6e\uff0c\u5b9e\u73b0\u9879\u76ee\u6807\u51c6\u5316\u3001\u53ef\u590d\u73b0\u3001\u53ef\u7ef4\u62a4\u7684\u5de5\u7a0b\u5316\u5f00\u53d1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4e3a\u4ec0\u4e48\u8981\u653e\u5f03\u4f20\u7edf\u5de5\u4f5c\u6d41\uff1fUV \u6838\u5fc3\u4f18\u52bf<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u4f20\u7edf\u5de5\u4f5c\u6d41\u7684\u75db\u70b9<\/h3>\n\n\n\n<p>\u4f20\u7edf Python \u5f00\u53d1\u4f9d\u8d56 <code>pip<\/code> \u5b89\u88c5\u5305\u3001<code>venv<\/code> \u521b\u5efa\u865a\u62df\u73af\u5883\u3001<code>requirements.txt<\/code> \u7ba1\u7406\u4f9d\u8d56\uff0c\u5b58\u5728\u8bf8\u591a\u786c\u4f24\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u914d\u7f6e\u788e\u7247\u5316<\/strong>\uff1a\u9879\u76ee\u5143\u6570\u636e\u3001\u4f9d\u8d56\u3001\u6253\u5305\u914d\u7f6e\u5206\u6563\u5728\u591a\u4e2a\u6587\u4ef6\uff0c\u65e0\u7edf\u4e00\u5165\u53e3<\/li>\n\n\n\n<li><strong>\u4f9d\u8d56\u533a\u5206\u56f0\u96be<\/strong>\uff1a\u65e0\u6cd5\u539f\u751f\u533a\u5206\u751f\u4ea7\u4f9d\u8d56\u4e0e\u5f00\u53d1\u4f9d\u8d56\uff0c\u9700\u624b\u52a8\u7ef4\u62a4\u591a\u4efd\u4f9d\u8d56\u6587\u4ef6<\/li>\n\n\n\n<li><strong>\u73af\u5883\u590d\u73b0\u4e0d\u7a33\u5b9a<\/strong>\uff1a<code>requirements.txt<\/code> \u4ec5\u8bb0\u5f55\u7248\u672c\uff0c\u65e0\u5b8c\u6574\u9501\u7248\u672c\u673a\u5236\uff0c\u8de8\u73af\u5883\u5b89\u88c5\u7248\u672c\u4e0d\u4e00\u81f4<\/li>\n\n\n\n<li><strong>\u6548\u7387\u6781\u4f4e<\/strong>\uff1apip \u5b89\u88c5\u3001\u89e3\u6790\u4f9d\u8d56\u901f\u5ea6\u6162\uff0c\u65b0\u9879\u76ee\u521d\u59cb\u5316\u6d41\u7a0b\u7e41\u7410<\/li>\n\n\n\n<li><strong>\u4e0d\u7b26\u5408\u73b0\u4ee3\u89c4\u8303<\/strong>\uff1a\u9010\u6b65\u88ab PEP 621\u3001PEP 631 \u65b0\u6807\u51c6\u6dd8\u6c70<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 UV + pyproject.toml \u73b0\u4ee3\u5de5\u4f5c\u6d41\u4f18\u52bf<\/h3>\n\n\n\n<p>UV \u662f\u7531 Astral \u56e2\u961f\u5f00\u53d1\u7684<strong>\u6781\u901f Python \u4e00\u4f53\u5316\u5de5\u5177<\/strong>\uff0c\u96c6\u6210\u73af\u5883\u7ba1\u7406\u3001\u4f9d\u8d56\u89e3\u6790\u3001\u6253\u5305\u3001\u8fd0\u884c\u3001\u811a\u672c\u6267\u884c\u80fd\u529b\uff0c\u5b8c\u5168\u56f4\u7ed5 <code>pyproject.toml<\/code> \u6807\u51c6\u6784\u5efa\uff0c\u6838\u5fc3\u4f18\u52bf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6781\u901f\u9ad8\u6548<\/strong>\uff1a\u4f9d\u8d56\u89e3\u6790\u3001\u5b89\u88c5\u901f\u5ea6\u6bd4 pip \u5feb 10-100 \u500d\uff0c\u73af\u5883\u79d2\u7ea7\u642d\u5efa<\/li>\n\n\n\n<li><strong>\u7edf\u4e00\u6807\u51c6\u5316<\/strong>\uff1a\u552f\u4e00\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6 <code>pyproject.toml<\/code>\uff0c\u9075\u5faa PEP 621 \u5b98\u65b9\u6807\u51c6\uff0c\u66ff\u4ee3\u6240\u6709\u96f6\u6563\u914d\u7f6e<\/li>\n\n\n\n<li><strong>\u539f\u751f\u4f9d\u8d56\u5206\u7ec4<\/strong>\uff1a\u5929\u7136\u652f\u6301\u751f\u4ea7\u4f9d\u8d56\u3001\u5f00\u53d1\u4f9d\u8d56\u5206\u79bb\uff0c\u65e0\u9700\u624b\u52a8\u62c6\u5206\u6587\u4ef6<\/li>\n\n\n\n<li><strong>\u786e\u5b9a\u6027\u73af\u5883<\/strong>\uff1a\u81ea\u52a8\u751f\u6210 <code>uv.lock<\/code> \u9501\u6587\u4ef6\uff0c\u4fdd\u8bc1\u8de8\u8bbe\u5907\u3001\u8de8\u56e2\u961f\u73af\u5883\u5b8c\u5168\u4e00\u81f4<\/li>\n\n\n\n<li><strong>\u96f6\u914d\u7f6e\u5f00\u7bb1\u5373\u7528<\/strong>\uff1a\u81ea\u52a8\u7ba1\u7406\u865a\u62df\u73af\u5883\uff0c\u65e0\u9700\u624b\u52a8\u521b\u5efa\u3001\u6fc0\u6d3b<\/li>\n\n\n\n<li><strong>\u5168\u6d41\u7a0b\u95ed\u73af<\/strong>\uff1a\u8986\u76d6\u9879\u76ee\u521d\u59cb\u5316\u3001\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u6253\u5305\u3001\u53d1\u5e03\u5168\u751f\u547d\u5468\u671f<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001UV \u5b89\u88c5\u4e0e\u73af\u5883\u51c6\u5907<\/h2>\n\n\n\n<p>UV \u652f\u6301 Windows \/ macOS \/ Linux \u5168\u5e73\u53f0\uff0c\u63d0\u4f9b\u4e00\u952e\u5b89\u88c5\u547d\u4ee4\uff0c\u65e0\u9700\u989d\u5916\u4f9d\u8d56\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u5168\u5e73\u53f0\u5b89\u88c5\u547d\u4ee4<\/h3>\n\n\n\n<p><strong>Linux \/ macOS<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -LsSf https:\/\/astral.sh\/uv\/install.sh | sh<\/code><\/pre>\n\n\n\n<p><strong>Windows\uff08PowerShell\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>powershell -c \"irm https:\/\/astral.sh\/uv\/install.ps1 | iex\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 \u9a8c\u8bc1\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>uv --version<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7248\u672c\u53f7\u5373\u5b89\u88c5\u6210\u529f\uff0c\u540e\u7eed\u53ef\u901a\u8fc7 <code>uv self update<\/code> \u4e00\u952e\u5347\u7ea7 UV\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u57fa\u4e8e UV \u521d\u59cb\u5316\u6807\u51c6 Python \u9879\u76ee<\/h2>\n\n\n\n<p>UV \u63d0\u4f9b <code>uv init<\/code> \u547d\u4ee4\u4e00\u952e\u751f\u6210<strong>\u7b26\u5408\u73b0\u4ee3\u89c4\u8303\u7684\u9879\u76ee\u9aa8\u67b6<\/strong>\uff0c\u81ea\u52a8\u521b\u5efa <code>pyproject.toml<\/code>\u3001README\u3001\u5165\u53e3\u6587\u4ef6\u7b49\u6838\u5fc3\u6587\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u521d\u59cb\u5316\u65b0\u9879\u76ee<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u521b\u5efa\u9879\u76ee\u6587\u4ef6\u5939\u5e76\u8fdb\u5165\nmkdir uv-demo-project &amp;&amp; cd uv-demo-project\n\n# \u4e00\u952e\u521d\u59cb\u5316\u9879\u76ee\nuv init<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u81ea\u52a8\u751f\u6210\u7684\u9879\u76ee\u7ed3\u6784<\/h3>\n\n\n\n<p>\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff08\u6807\u51c6\u73b0\u4ee3 Python \u9879\u76ee\u7ed3\u6784\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uv-demo-project\/\n\u251c\u2500\u2500 .gitignore       # \u81ea\u52a8\u751f\u6210 git \u5ffd\u7565\u6587\u4ef6\n\u251c\u2500\u2500 README.md        # \u9879\u76ee\u8bf4\u660e\u6587\u6863\n\u251c\u2500\u2500 main.py          # \u9879\u76ee\u5165\u53e3\u6587\u4ef6\n\u2514\u2500\u2500 pyproject.toml   # \u9879\u76ee\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6\uff08\u91cd\u70b9\uff09<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u81ea\u52a8\u521b\u5efa\u865a\u62df\u73af\u5883<\/h3>\n\n\n\n<p>UV \u4e0d\u4f1a\u9ed8\u8ba4\u751f\u6210 <code>.venv<\/code>\uff0c\u5728<strong>\u9996\u6b21\u6dfb\u52a0\u4f9d\u8d56\u6216\u6267\u884c\u9879\u76ee\u547d\u4ee4<\/strong>\u65f6\u81ea\u52a8\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b89\u88c5\u7b2c\u4e00\u4e2a\u4f9d\u8d56\uff0c\u81ea\u52a8\u751f\u6210 .venv \u548c uv.lock\nuv add requests<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u9879\u76ee\u4f1a\u65b0\u589e <code>.venv\/<\/code> \u865a\u62df\u73af\u5883\u6587\u4ef6\u5939\u3001<code>uv.lock<\/code> \u5168\u5c40\u9501\u6587\u4ef6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6838\u5fc3\u7cbe\u8bb2\uff1a\u5f7b\u5e95\u5403\u900f pyproject.toml \u914d\u7f6e<\/h2>\n\n\n\n<p><code>pyproject.toml<\/code> \u662f\u73b0\u4ee3 Python \u9879\u76ee\u7684<strong>\u552f\u4e00\u914d\u7f6e\u5165\u53e3<\/strong>\uff0c\u66ff\u4ee3\u4e86\u4f20\u7edf\u7684 <code>setup.py<\/code>\u3001<code>setup.cfg<\/code>\u3001<code>requirements.txt<\/code>\u3002\u4e0b\u6587\u9010\u5b57\u6bb5\u62c6\u89e3 UV \u9879\u76ee\u6807\u51c6\u914d\u7f6e\uff0c\u8986\u76d6\u65e5\u5e38\u5f00\u53d1 99% \u573a\u666f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 \u57fa\u7840\u5b8c\u6574\u914d\u7f6e\u6a21\u677f<\/h3>\n\n\n\n<p>\u521d\u59cb\u5316\u540e\u9ed8\u8ba4\u914d\u7f6e+\u5e38\u7528\u6269\u5c55\u914d\u7f6e\uff0c\u5b8c\u6574\u53ef\u76f4\u63a5\u590d\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;project]\n# \u9879\u76ee\u540d\u79f0\uff08pip\/uv \u5b89\u88c5\u3001PyPI \u53d1\u5e03\u540d\u79f0\uff09\nname = \"uv-demo-project\"\n# \u9879\u76ee\u7248\u672c\uff08\u8bed\u4e49\u5316\u7248\u672c\u89c4\u8303\uff09\nversion = \"0.1.0\"\n# \u9879\u76ee\u7b80\u77ed\u63cf\u8ff0\ndescription = \"UV \u73b0\u4ee3 Python \u5de5\u4f5c\u6d41\u6f14\u793a\u9879\u76ee\"\n# \u8be6\u7ec6\u8bf4\u660e\u6587\u6863\u8def\u5f84\nreadme = \"README.md\"\n# \u652f\u6301\u7684 Python \u7248\u672c\uff08\u4e25\u683c\u9650\u5236\u73af\u5883\uff09\nrequires-python = \">=3.11\"\n# \u9879\u76ee\u8bb8\u53ef\u8bc1\nlicense = { file = \"LICENSE\" }\n# \u4f5c\u8005\u4fe1\u606f\nauthors = &#91;\n  { name = \"Your Name\", email = \"your@email.com\" }\n]\n# \u751f\u4ea7\u73af\u5883\u6838\u5fc3\u4f9d\u8d56\ndependencies = &#91;\n  \"requests>=2.31.0\"\n]\n\n&#91;dependency-groups]\ndev = &#91;\"mypy>=2.1.0\", \"pytest>=9.0.3\", \"ruff>=0.15.14\"]\n# UV \u4e13\u5c5e\u914d\u7f6e\uff08\u73af\u5883\u3001\u89e3\u6790\u3001\u884c\u4e3a\u914d\u7f6e\uff09\n&#91;tool.uv]\n# \u4e0d\u81ea\u52a8\u751f\u6210 __pycache__ \ncompile-bytecode = false \n# Ruff \u4ee3\u7801\u683c\u5f0f\u5316\/\u68c0\u67e5\u914d\u7f6e\n&#91;tool.ruff]\nline-length = 120 \ntarget-version = \"py311\" \n# Pytest \u6d4b\u8bd5\u914d\u7f6e\n&#91;tool.pytest.ini_options]\ntestpaths = &#91;\"tests\"] \npython_files = \"test_*.py\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 \u6838\u5fc3\u5b57\u6bb5\u9010\u884c\u8be6\u89e3<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">4.2.1 [project] \u57fa\u7840\u5143\u6570\u636e\uff08PEP 621 \u6807\u51c6\uff09<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>name<\/strong>\uff1a\u9879\u76ee\u552f\u4e00\u6807\u8bc6\uff0c\u7528\u4e8e\u5305\u5b89\u88c5\u3001\u5bfc\u5165\u3001\u53d1\u5e03\uff0c\u5fc5\u987b\u5c0f\u5199\u3001\u65e0\u7a7a\u683c<\/li>\n\n\n\n<li><strong>version<\/strong>\uff1a\u8bed\u4e49\u5316\u7248\u672c\uff0c\u9075\u5faa <code>\u4e3b\u7248\u672c.\u6b21\u7248\u672c.\u4fee\u8ba2\u53f7<\/code>\uff0c\u7528\u4e8e\u7248\u672c\u8fed\u4ee3\u7ba1\u7406<\/li>\n\n\n\n<li><strong>requires-python<\/strong>\uff1a\u9650\u5236\u9879\u76ee\u8fd0\u884c\u7684 Python \u6700\u4f4e\u7248\u672c\uff0c\u907f\u514d\u7248\u672c\u4e0d\u517c\u5bb9\u95ee\u9898<\/li>\n\n\n\n<li><strong>dependencies<\/strong>\uff1a<strong>\u751f\u4ea7\u4f9d\u8d56<\/strong>\uff0c\u9879\u76ee\u8fd0\u884c\u5fc5\u9700\u7684\u7b2c\u4e09\u65b9\u5e93\uff0cUV \u4f1a\u81ea\u52a8\u89e3\u6790\u7248\u672c\u517c\u5bb9\u5173\u7cfb<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.2.2 \u4f9d\u8d56\u5206\u7ec4\uff1a\u533a\u5206\u751f\u4ea7\/\u5f00\u53d1\u4f9d\u8d56<\/h4>\n\n\n\n<p><code>[dependency-groups]<\/code> \u662f\u73b0\u4ee3\u5de5\u4f5c\u6d41\u7684\u6838\u5fc3\uff0c\u5f7b\u5e95\u89e3\u51b3\u4f20\u7edf\u4f9d\u8d56\u6df7\u6742\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f00\u53d1\u4f9d\u8d56<\/strong>\uff1a<code>dev<\/code> \u5206\u7ec4\u914d\u7f6e\uff0c\u4ec5\u7528\u4e8e\u4ee3\u7801\u68c0\u67e5\u3001\u6d4b\u8bd5\u3001\u683c\u5f0f\u5316\uff0c\u751f\u4ea7\u73af\u5883\u4e0d\u5b89\u88c5<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.2.3 [tool.xxx] \u5de5\u5177\u4e13\u5c5e\u914d\u7f6e<\/h4>\n\n\n\n<p>\u6240\u6709\u7b2c\u4e09\u65b9\u5de5\u5177\u914d\u7f6e\u7edf\u4e00\u6536\u7eb3\u5728 <code>[tool.xxx]<\/code> \u4e0b\uff0cUV\u3001Ruff\u3001Pytest\u3001Mypy \u7b49\u5747\u652f\u6301\uff0c\u5b9e\u73b0<strong>\u4e00\u5355\u914d\u7f6e\uff0c\u5168\u5c40\u751f\u6548<\/strong>\uff0c\u65e0\u9700\u5206\u6563\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 \u5173\u952e\u6587\u4ef6\uff1auv.lock \u9501\u6587\u4ef6\u4f5c\u7528<\/h3>\n\n\n\n<p>\u6267\u884c <code>uv add<\/code> \/ <code>uv sync<\/code> \u540e\u81ea\u52a8\u751f\u6210 <code>uv.lock<\/code>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bb0\u5f55<strong>\u6240\u6709\u4f9d\u8d56\u7684\u7cbe\u786e\u7248\u672c\u3001\u54c8\u5e0c\u503c\u3001\u4f9d\u8d56\u6811<\/strong><\/li>\n\n\n\n<li>\u4fdd\u8bc1\u56e2\u961f\u534f\u4f5c\u3001\u670d\u52a1\u5668\u90e8\u7f72\u3001\u672c\u5730\u5f00\u53d1\u73af\u5883<strong>100% \u7248\u672c\u4e00\u81f4<\/strong><\/li>\n\n\n\n<li>\u5fc5\u987b\u63d0\u4ea4\u5230 Git \u4ed3\u5e93\uff0c\u662f\u9879\u76ee\u53ef\u590d\u73b0\u7684\u6838\u5fc3\u4fdd\u969c<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001UV \u65e5\u5e38\u9879\u76ee\u5de5\u4f5c\u6d41\u5b9e\u64cd\uff08\u5168\u751f\u547d\u5468\u671f\uff09<\/h2>\n\n\n\n<p>\u638c\u63e1\u914d\u7f6e\u540e\uff0c\u719f\u7ec3\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5373\u53ef\u5b8c\u6210\u9879\u76ee\u5168\u65e5\u5e38\u8fd0\u7ef4\uff0c\u5f7b\u5e95\u66ff\u4ee3 pip\/venv\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 \u4f9d\u8d56\u7ba1\u7406\uff08\u6838\u5fc3\u64cd\u4f5c\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">5.1.1 \u6dfb\u52a0\u4f9d\u8d56<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6dfb\u52a0\u751f\u4ea7\u4f9d\u8d56\uff08\u81ea\u52a8\u5199\u5165 pyproject.toml dependencies\uff09\nuv add \u5305\u540d\nuv add requests numpy\n\n# \u6dfb\u52a0\u5f00\u53d1\u4f9d\u8d56\uff08\u81ea\u52a8\u5199\u5165 dev \u5206\u7ec4\uff09\nuv add --dev pytest ruff mypy<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.1.2 \u5220\u9664\u4f9d\u8d56<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5220\u9664\u751f\u4ea7\/\u5f00\u53d1\u4f9d\u8d56\uff0c\u81ea\u52a8\u66f4\u65b0\u914d\u7f6e\u548c\u9501\u6587\u4ef6\nuv remove \u5305\u540d<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.1.3 \u540c\u6b65\u73af\u5883\uff08\u591a\u4eba\u534f\u4f5c\/\u6362\u8bbe\u5907\u5fc5\u5907\uff09<\/h4>\n\n\n\n<p>\u62c9\u53d6\u9879\u76ee\u4ee3\u7801\u540e\uff0c\u65e0\u9700\u624b\u52a8\u5b89\u88c5\u4f9d\u8d56\uff0c\u4e00\u952e\u540c\u6b65\u5b8c\u6574\u73af\u5883\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6839\u636e pyproject.toml + uv.lock \u7cbe\u51c6\u8fd8\u539f\u73af\u5883\nuv sync\n\n# \u4ec5\u540c\u6b65\u751f\u4ea7\u4f9d\u8d56\uff08\u90e8\u7f72\u73af\u5883\u4f7f\u7528\uff09\nuv sync --no-dev<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 \u865a\u62df\u73af\u5883\u7ba1\u7406<\/h3>\n\n\n\n<p>UV \u81ea\u52a8\u7ba1\u7406 <code>.venv<\/code>\uff0c\u65e0\u9700\u624b\u52a8\u521b\u5efa\uff0c\u5e38\u7528\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u624b\u52a8\u521b\u5efa\/\u91cd\u5efa\u865a\u62df\u73af\u5883\nuv venv\n\n# \u6fc0\u6d3b\u73af\u5883\uff08Linux\/macOS\uff09\nsource .venv\/bin\/activate\n\n# \u6fc0\u6d3b\u73af\u5883\uff08Windows\uff09\n.venv\\Scripts\\activate\n\n# \u9000\u51fa\u73af\u5883\ndeactivate<\/code><\/pre>\n\n\n\n<p>\ud83d\udca1 \u5c0f\u8d34\u58eb\uff1aUV \u652f\u6301<strong>\u65e0\u9700\u6fc0\u6d3b\u73af\u5883\u76f4\u63a5\u8fd0\u884c<\/strong>\uff0c\u901a\u8fc7 <code>uv run<\/code> \u81ea\u52a8\u8c03\u7528\u5f53\u524d\u9879\u76ee\u865a\u62df\u73af\u5883\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uv run python main.py\nuv run pytest\nuv run ruff check<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.3 \u9879\u76ee\u6253\u5305\u4e0e\u53d1\u5e03<\/h3>\n\n\n\n<p>UV \u539f\u751f\u652f\u6301\u6253\u5305\uff0c\u65e0\u9700 setuptools\uff0c\u4e00\u952e\u751f\u6210\u6807\u51c6\u5b89\u88c5\u5305\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u751f\u6210 sdist \u6e90\u7801\u5305 + wheel \u4e8c\u8fdb\u5236\u5305\nuv build\n\n# \u53d1\u5e03\u5230 PyPI\nuv publish<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u65e7\u9879\u76ee\u8fc1\u79fb\uff1a\u4ece requirements.txt \u8fc1\u79fb\u5230 UV<\/h2>\n\n\n\n<p>\u9488\u5bf9\u4f20\u7edf\u65e7\u9879\u76ee\uff0c\u4e00\u952e\u8fc1\u79fb\u81f3\u73b0\u4ee3 UV \u5de5\u4f5c\u6d41\uff0c\u65e0\u9700\u91cd\u6784\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u521d\u59cb\u5316 uv \u9879\u76ee\nuv init\n\n# 2. \u4ece requirements.txt \u5bfc\u5165\u751f\u4ea7\u4f9d\u8d56\nuv add -r requirements.txt\n\n# 3. \u4ece dev-requirements.txt \u5bfc\u5165\u5f00\u53d1\u4f9d\u8d56\nuv add --dev -r dev-requirements.txt\n\n# 4. \u540c\u6b65\u73af\u5883\uff0c\u751f\u6210 lock \u6587\u4ef6\nuv sync<\/code><\/pre>\n\n\n\n<p>\u8fc1\u79fb\u5b8c\u6210\u540e\uff0c\u5373\u53ef\u5220\u9664\u8001\u65e7\u7684<code>requirements.txt<\/code> \u7cfb\u5217\u6587\u4ef6\uff0c\u7edf\u4e00\u4f7f\u7528 <code>pyproject.toml<\/code> \u7ba1\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001UV \u9879\u76ee\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u4e25\u683c\u533a\u5206\u4f9d\u8d56\u5206\u7ec4<\/strong>\uff1a\u4e1a\u52a1\u4f9d\u8d56\u653e\u751f\u4ea7\u7ec4\uff0c\u5de5\u5177\u4f9d\u8d56\u653e\u5f00\u53d1\u7ec4\uff0c\u90e8\u7f72\u65f6\u4f7f\u7528 <code>uv sync --no-dev<\/code> \u7cbe\u7b80\u73af\u5883<\/li>\n\n\n\n<li><strong>\u5f3a\u5236\u63d0\u4ea4 lock \u6587\u4ef6<\/strong>\uff1a\u56e2\u961f\u534f\u4f5c\u9879\u76ee\u5fc5\u987b\u63d0\u4ea4 <code>uv.lock<\/code>\uff0c\u4fdd\u8bc1\u73af\u5883\u4e00\u81f4\u6027<\/li>\n\n\n\n<li><strong>\u9075\u5faa\u8bed\u4e49\u5316\u7248\u672c<\/strong>\uff1a\u4e25\u683c\u6309\u7167 <code>\u4e3b.\u6b21.\u4fee<\/code> \u89c4\u5219\u8fed\u4ee3\u9879\u76ee\u7248\u672c<\/li>\n\n\n\n<li><strong>\u7edf\u4e00\u5de5\u5177\u914d\u7f6e<\/strong>\uff1a\u6240\u6709\u4ee3\u7801\u68c0\u67e5\u3001\u683c\u5f0f\u5316\u3001\u6d4b\u8bd5\u5de5\u5177\u914d\u7f6e\u5168\u90e8\u5199\u5165 <code>pyproject.toml<\/code>\uff0c\u4e0d\u4f7f\u7528\u96f6\u6563\u914d\u7f6e\u6587\u4ef6<\/li>\n\n\n\n<li><strong>\u5b9a\u671f\u66f4\u65b0\u4f9d\u8d56<\/strong>\uff1a\u4f7f\u7528 <code>uv update<\/code> \u6279\u91cf\u66f4\u65b0\u4f9d\u8d56\uff0c\u914d\u5408 lock \u6587\u4ef6\u4fdd\u8bc1\u7a33\u5b9a\u6027<\/li>\n\n\n\n<li><strong>\u5ffd\u7565 .venv \u76ee\u5f55<\/strong>\uff1a\u5728 <code>.gitignore<\/code> \u4e2d\u914d\u7f6e\u5ffd\u7565\u865a\u62df\u73af\u5883\u6587\u4ef6\u5939\uff0c\u4ec5\u4fdd\u7559\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u957f\u671f\u4ee5\u6765\uff0cPython \u5f00\u53d1\u8005\u9971\u53d7 pip + venv + requirements.txt \u788e\u7247\u5316\u5de5\u4f5c\u6d41\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[24],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"_links":{"self":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":6,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":254,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions\/254"}],"wp:attachment":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}