{"id":182,"date":"2025-08-14T15:41:12","date_gmt":"2025-08-14T07:41:12","guid":{"rendered":"https:\/\/www.handytoo.com\/?p=182"},"modified":"2025-08-14T15:41:13","modified_gmt":"2025-08-14T07:41:13","slug":"%e6%88%91%e6%98%af%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87pyenvvirtualenv%e5%9c%a8linux%e5%ae%9e%e7%8e%b0%e5%a4%9a%e7%89%88%e6%9c%acpython%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.handytoo.com\/?p=182","title":{"rendered":"\u6211\u662f\u5982\u4f55\u901a\u8fc7pyenv+virtualenv\u5728linux\u5b9e\u73b0\u591a\u7248\u672cPython\u7ba1\u7406"},"content":{"rendered":"\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u548c\u7ebf\u4e0a\u90e8\u7f72\u7684\u65f6\u5019\u7ecf\u5e38\u56e0\u4e3a\u4e0d\u540c\u7684\u5e94\u7528\uff0c\u4f7f\u7528\u4e0d\u540c\u7684python\u548c\u4f9d\u8d56\u5305\u3002\u7ecf\u8fc7\u5b9e\u8df5\u901a\u8fc7pyenv-virtualenv\u5b9e\u73b0\u73af\u5883\u4e0d\u5171\u4eab\u4efb\u4f55\u5305\u6216\u4f9d\u8d56\uff0cPython \u89e3\u91ca\u5668\u90fd\u662f\u72ec\u7acb\u7684\u7edd\u5bf9\u548c\u9694\u79bb\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b89\u88c5\u4e0e\u914d\u7f6e\uff08Ubuntu24\uff09<\/h2>\n\n\n\n<p>1 \u5b89\u88c5 pyenv + pyenv-virtualenv<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl https:\/\/pyenv.run | bash<\/code><\/pre>\n\n\n\n<p>2 \u914d\u7f6e\u548c\u5e94\u7528 Shell \u73af\u5883<\/p>\n\n\n\n<p>\u5c06\u4ee5\u4e0b\u5185\u5bb9\u6dfb\u52a0\u5230\u00a0<code>~\/.bashrc<\/code>\u00a0\u6216\u00a0<code>~\/.zshrc<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u786e\u4fdd pyenv \u521d\u59cb\u5316\u5728 PATH \u8bbe\u7f6e\u4e4b\u540e\nexport PYENV_ROOT=\"$HOME\/.pyenv\"\nexport PATH=\"$PYENV_ROOT\/bin:$PATH\"\n\n# \u6838\u5fc3\u8bbe\u7f6e\uff1a\u786e\u4fdd shims \u4f18\u5148\u7ea7\u6700\u9ad8\nexport PATH=\"$PYENV_ROOT\/shims:$PATH\"\n\n# \u521d\u59cb\u5316 pyenv\neval \"$(pyenv init --path)\"\neval \"$(pyenv init -)\"\neval \"$(pyenv virtualenv-init -)\"<\/code><\/pre>\n\n\n\n<p>\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p>export PATH=&#8221;$PYENV_ROOT\/shims:$PATH&#8221; \u8fd9\u4e00\u6b65\u5f88\u91cd\u8981\uff0c\u5f53\u8f93\u5165python\u6216\u8005pip\u7684\u65f6\u5019\u4f1a\u901a\u8fc7\u73af\u5883\u53d8\u91cfPATH\u4f18\u5148\u627e\u5230 \u5f53\u524d\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4<\/p>\n\n\n\n<p>\u5e94\u7528\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exec $SHELL<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u521b\u5efa\u5b8c\u5168\u9694\u79bb\u7684\u865a\u62df\u73af\u5883<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5\u6240\u9700 Python \u7248\u672c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pyenv install 3.10.6   <em># \u5b89\u88c5\u7279\u5b9a\u7248\u672c<\/em><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u521b\u5efa\u865a\u62df\u73af\u5883\uff08\u5173\u952e\u6b65\u9aa4\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pyenv virtualenv 3.10.6 myproject-env<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>3.10.6<\/code>\uff1a\u6307\u5b9a\u7684 Python \u7248\u672c<\/li>\n\n\n\n<li><code>myproject-env<\/code>\uff1a\u865a\u62df\u73af\u5883\u540d\u79f0<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u6fc0\u6d3b\u73af\u5883<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pyenv activate myproject-env<\/code><\/pre>\n\n\n\n<p>\u6fc0\u6d3b\u540e\u63d0\u793a\u7b26\u4f1a\u53d8\u5316\uff1a<code>(myproject-env) user@host:~$<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9a8c\u8bc1\u7edd\u5bf9\u9694\u79bb<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u68c0\u67e5 Python \u8def\u5f84<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pyenv which python\n<em># \u8f93\u51fa: \/home\/user\/.pyenv\/versions\/myproject-env\/bin\/python<\/em><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u68c0\u67e5 pip \u8def\u5f84<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pyenv which pip\n<em># \u8f93\u51fa: \/home\/user\/.pyenv\/versions\/myproject-env\/bin\/pip<\/em><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u68c0\u67e5\u73af\u5883\u4fe1\u606f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>python -c \"import sys; print(sys.prefix)\"\n<em># \u8f93\u51fa: \/home\/user\/.pyenv\/versions\/myproject-env<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u73af\u5883\u5e38\u7528\u547d\u4ee4<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\u64cd\u4f5c\t        \u547d\u4ee4<br>\u6fc0\u6d3b\u73af\u5883\tpyenv activate &lt;env-name><br>\u9000\u51fa\u73af\u5883\tpyenv deactivate<br>\u5217\u51fa\u6240\u6709\u73af\u5883\tpyenv virtualenvs<br>\u5220\u9664\u73af\u5883\tpyenv uninstall &lt;env-name><br>\u67e5\u770b\u5305\u5b89\u88c5\u8def\u5f84  pip show &lt;\u5305\u540d\u79f0>| grep Location<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b9e\u9645\u5f00\u53d1\u548c\u7ebf\u4e0a\u90e8\u7f72\u7684\u65f6\u5019\u7ecf\u5e38\u56e0\u4e3a\u4e0d\u540c\u7684\u5e94\u7528\uff0c\u4f7f\u7528\u4e0d\u540c\u7684python\u548c\u4f9d\u8d56\u5305\u3002\u7ecf\u8fc7\u5b9e\u8df5\u901a\u8fc7pyenv-virt [&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-182","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\/182","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=182"}],"version-history":[{"count":7,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions\/189"}],"wp:attachment":[{"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.handytoo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}