{"id":1673,"date":"2023-03-07T22:35:23","date_gmt":"2023-03-07T14:35:23","guid":{"rendered":"https:\/\/www.strongd.net\/?p=1673"},"modified":"2023-03-07T22:35:23","modified_gmt":"2023-03-07T14:35:23","slug":"%e9%97%9c%e6%96%bcpython-3%e8%99%9b%e6%93%ac%e7%92%b0%e5%a2%83%ef%bc%88venv%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=1673","title":{"rendered":"\u95dc\u65bcPython 3\u865b\u64ec\u74b0\u5883\uff08venv\uff09"},"content":{"rendered":"<p>Python\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u9ad8\u7d1a\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bcWeb\u958b\u767c\u3001\u6578\u64da\u79d1\u5b78\u3001\u4eba\u5de5\u667a\u80fd\u7b49\u9818\u57df\u3002\u70ba\u4e86\u4fdd\u6301Python\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u9760\u6027\uff0cPython 3\u5f15\u5165\u4e86venv\uff08\u865b\u64ec\u74b0\u5883\uff09\u7684\u6982\u5ff5\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u4eba\u54e1\u53ef\u4ee5\u70ba\u6bcf\u500bPython\u61c9\u7528\u7a0b\u5e8f\u5efa\u7acb\u7368\u7acb\u7684\u74b0\u5883\uff0c\u9019\u4e9b\u74b0\u5883\u4e4b\u9593\u76f8\u4e92\u7368\u7acb\uff0c\u4e0d\u6703\u5e72\u64fe\u5f7c\u6b64\u3002<\/p>\n<p>venv\u662fPython 3\u7684\u5167\u7f6e\u6a21\u584a\uff0c\u53ef\u4ee5\u901a\u904e\u547d\u4ee4\u884c\u5275\u5efa\u548c\u7ba1\u7406\u865b\u64ec\u74b0\u5883\u3002\u4e0b\u9762\u6211\u5011\u5c07\u4e00\u6b65\u6b65\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528venv\u5275\u5efa\u865b\u64ec\u74b0\u5883\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6253\u958b\u547d\u4ee4\u884c\u7d42\u7aef\uff0c\u9032\u5165\u4f60\u60f3\u8981\u5275\u5efa\u865b\u64ec\u74b0\u5883\u7684\u76ee\u9304\u3002\u7136\u5f8c\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>&#8220;`<br \/>\npython3 -m venv myenv<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u547d\u4ee4\u5c07\u5728\u7576\u524d\u76ee\u9304\u4e2d\u5275\u5efa\u4e00\u500b\u540d\u70ba\u201cmyenv\u201d\u7684\u865b\u64ec\u74b0\u5883\u3002\u4f60\u53ef\u4ee5\u5c07\u201cmyenv\u201d\u66ff\u63db\u70ba\u4f60\u60f3\u8981\u7684\u4efb\u4f55\u540d\u7a31\u3002<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u555f\u52d5\u865b\u64ec\u74b0\u5883\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>&#8211; \u5728Windows\u4e2d\uff1a<\/p>\n<p>&#8220;`<br \/>\nmyenv\\Scripts\\activate.bat<br \/>\n&#8220;`<\/p>\n<p>&#8211; \u5728Mac\u6216Linux\u4e2d\uff1a<\/p>\n<p>&#8220;`<br \/>\nsource myenv\/bin\/activate<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6703\u6fc0\u6d3b\u865b\u64ec\u74b0\u5883\uff0c\u4f60\u5c07\u770b\u5230\u547d\u4ee4\u884c\u524d\u9762\u51fa\u73fe\u4e86\u865b\u64ec\u74b0\u5883\u7684\u540d\u7a31\u3002<\/p>\n<p>\u73fe\u5728\u4f60\u53ef\u4ee5\u5b89\u88dd\u4f60\u9700\u8981\u7684Python\u5eab\u6216\u6a21\u584a\u3002\u5728\u865b\u64ec\u74b0\u5883\u4e2d\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>&#8220;`<br \/>\npip install &lt;package_name&gt;<br \/>\n&#8220;`<\/p>\n<p>\u7576\u4f60\u5b8c\u6210\u958b\u767c\u5de5\u4f5c\u5f8c\uff0c\u4f60\u53ef\u4ee5\u9000\u51fa\u865b\u64ec\u74b0\u5883\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>&#8220;`<br \/>\ndeactivate<br \/>\n&#8220;`<\/p>\n<p>\u9019\u5c07\u9000\u51fa\u865b\u64ec\u74b0\u5883\uff0c\u4f60\u5c07\u56de\u5230\u539f\u4f86\u7684Python\u74b0\u5883\u3002<\/p>\n<p>\u7e3d\u4e4b\uff0cvenv\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u4eba\u54e1\u7ba1\u7406Python\u61c9\u7528\u7a0b\u5e8f\u7684\u4f9d\u8cf4\u6027\u4e26\u907f\u514d\u7248\u672c\u885d\u7a81\u3002\u5982\u679c\u4f60\u9084\u6c92\u6709\u4f7f\u7528venv\uff0c\u90a3\u9ebc\u73fe\u5728\u662f\u6642\u5019\u958b\u59cb\u4f7f\u7528\u5b83\u4e86\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u9ad8\u7d1a\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bcWeb\u958b\u767c\u3001\u6578\u64da\u79d1\u5b78\u3001\u4eba\u5de5\u667a\u80fd\u7b49\u9818\u57df\u3002\u70ba\u4e86\u4fdd\u6301Python\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u9760\u6027\uff0cPython 3\u5f15\u5165\u4e86venv\uff08\u865b\u64ec\u74b0\u5883\uff09\u7684\u6982\u5ff5\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u4eba\u54e1\u53ef\u4ee5\u70ba\u6bcf\u500bPython\u61c9\u7528\u7a0b\u5e8f\u5efa\u7acb\u7368\u7acb\u7684\u74b0\u5883\uff0c\u9019\u4e9b\u74b0\u5883\u4e4b\u9593\u76f8\u4e92\u7368\u7acb\uff0c\u4e0d\u6703\u5e72\u64fe\u5f7c\u6b64\u3002 venv\u662fPython 3\u7684\u5167\u7f6e\u6a21\u584a\uff0c\u53ef\u4ee5\u901a\u904e\u547d\u4ee4\u884c\u5275\u5efa\u548c\u7ba1\u7406\u865b\u64ec\u74b0\u5883\u3002\u4e0b\u9762\u6211\u5011\u5c07\u4e00\u6b65\u6b65\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528venv\u5275\u5efa\u865b\u64ec\u74b0\u5883\u3002 \u9996\u5148\uff0c\u6253\u958b\u547d\u4ee4\u884c\u7d42\u7aef\uff0c\u9032\u5165\u4f60\u60f3\u8981\u5275\u5efa\u865b\u64ec\u74b0\u5883\u7684\u76ee\u9304\u3002\u7136\u5f8c\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a &#8220;` python3 -m venv myenv &#8220;` \u9019\u500b\u547d\u4ee4\u5c07\u5728\u7576\u524d\u76ee\u9304\u4e2d\u5275\u5efa\u4e00\u500b\u540d\u70ba\u201cmyenv\u201d\u7684\u865b\u64ec\u74b0\u5883\u3002\u4f60\u53ef\u4ee5\u5c07\u201cmyenv\u201d\u66ff\u63db\u70ba\u4f60\u60f3\u8981\u7684\u4efb\u4f55\u540d\u7a31\u3002 \u63a5\u4e0b\u4f86\uff0c\u555f\u52d5\u865b\u64ec\u74b0\u5883\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a &#8211; \u5728Windows\u4e2d\uff1a &#8220;` myenv\\Scripts\\activate.bat &#8220;` &#8211; \u5728Mac\u6216Linux\u4e2d\uff1a &#8220;` source myenv\/bin\/activate &#8220;` \u9019\u6703\u6fc0\u6d3b\u865b\u64ec\u74b0\u5883\uff0c\u4f60\u5c07\u770b\u5230\u547d\u4ee4\u884c\u524d\u9762\u51fa\u73fe\u4e86\u865b\u64ec\u74b0\u5883\u7684\u540d\u7a31\u3002 \u73fe\u5728\u4f60\u53ef\u4ee5\u5b89\u88dd\u4f60\u9700\u8981\u7684Python\u5eab\u6216\u6a21\u584a\u3002\u5728\u865b\u64ec\u74b0\u5883\u4e2d\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a &#8220;` pip install &lt;package_name&gt; &#8220;` \u7576\u4f60\u5b8c\u6210\u958b\u767c\u5de5\u4f5c\u5f8c\uff0c\u4f60\u53ef\u4ee5\u9000\u51fa\u865b\u64ec\u74b0\u5883\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a &#8220;` deactivate &#8220;` \u9019\u5c07\u9000\u51fa\u865b\u64ec\u74b0\u5883\uff0c\u4f60\u5c07\u56de\u5230\u539f\u4f86\u7684Python\u74b0\u5883\u3002 \u7e3d\u4e4b\uff0cvenv\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u4eba\u54e1\u7ba1\u7406Python\u61c9\u7528\u7a0b\u5e8f\u7684\u4f9d\u8cf4\u6027\u4e26\u907f\u514d\u7248\u672c\u885d\u7a81\u3002\u5982\u679c\u4f60\u9084\u6c92\u6709\u4f7f\u7528venv\uff0c\u90a3\u9ebc\u73fe\u5728\u662f\u6642\u5019\u958b\u59cb\u4f7f\u7528\u5b83\u4e86\uff01<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[235],"tags":[],"class_list":["post-1673","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1673"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1673\/revisions"}],"predecessor-version":[{"id":1674,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1673\/revisions\/1674"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}