{"id":221461,"date":"2021-07-10T09:51:28","date_gmt":"2021-07-10T01:51:28","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=221461"},"modified":"2021-06-25T09:52:19","modified_gmt":"2021-06-25T01:52:19","slug":"python-virtualenv-cmd","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-virtualenv-cmd.html","title":{"rendered":"Python\u865a\u62df\u73af\u5883virtualenv\u4f7f\u7528\u8bf4\u660e"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u662f\u5173\u4e8ePython\u865a\u62df\u73af\u5883\u7684\u76f8\u5173\u77e5\u8bc6,\u6587\u7ae0\u56f4\u7ed5\u7740Python\u865a\u62df\u73af\u5883virtualenv\u662f\u5982\u4f55\u4f7f\u7528\u7684\u5c55\u5f00,\u6587\u4e2d\u6709\u975e\u5e38\u8be6\u7ec6\u7684\u89e3\u91ca\u53ca\u4ee3\u7801\u793a\u4f8b,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e86\u89e3\u865a\u62df\u73af\u5883<\/strong><\/div>\n

\u5982\u679c\u5728\u4e00\u53f0\u673a\u5668\u4e0a\uff0c\u60f3\u5f00\u53d1\u591a\u4e2a\u4e0d\u540c\u7684\u9879\u76ee\uff0c\u9700\u8981\u7528\u5230\u540c\u4e00\u4e2a\u5305\u7684\u4e0d\u540c\u7248\u672c\uff0c\u5982\u679c\u8fd8\u4f7f\u7528\u4e0a\u9762\u7684\u547d\u4ee4\uff0c\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e0b\u5b89\u88c5\u6216\u8005\u66f4\u65b0\uff0c\u5176\u5b83\u7684\u9879\u76ee\u5fc5\u987b\u5c31\u65e0\u6cd5\u8fd0\u884c\u4e86\uff0c\u600e\u4e48\u529e\u5462\uff1f<\/p>\n

\u89e3\u51b3\u65b9\u6848\uff1a\u865a\u62df\u73af\u5883<\/strong><\/span><\/div>\n

\u90a3\u4e48\u4ec0\u4e48\u662f\u865a\u62df\u73af\u5883\u5462\uff1f<\/p>\n

\u865a\u62df\u73af\u5883\u5176\u5b9e\u5c31\u662f\u5bf9\u771f\u5b9e python \u73af\u5883\u7684\u590d\u5236\uff0c\u8fd9\u6837\u6211\u4eec\u5728\u590d\u5236\u7684 python \u73af\u5883\u4e2d\u5b89\u88c5\u5305\u5c31\u4e0d\u4f1a\u5f71\u54cd\u5230\u771f\u5b9e\u7684python\u73af\u5883\u3002\u901a\u8fc7\u5efa\u7acb\u591a\u4e2a\u865a\u62df\u73af\u5883\uff0c\u5728\u4e0d\u540c\u7684\u865a\u62df\u73af\u5883\u4e2d\u5f00\u53d1\u9879\u76ee\u5c31\u5b9e\u73b0\u4e86\u9879\u76ee\u4e4b\u95f4\u7684\u9694\u79bb\u3002<\/p>\n

\u865a\u62df\u73af\u5883\u5e93\u7684\u5b89\u88c5<\/strong><\/div>\n

\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528 virtualenv \u6765\u521b\u5efa\u865a\u62df\u73af\u5883\u3002<\/p>\n

pip \u9ed8\u8ba4\u7684\u6e90\u5b89\u88c5\u7b2c\u4e09\u5e93\u4f1a\u6709\u70b9\u6162\uff0c\u53ef\u4ee5\u914d\u7f6e\u4e00\u4e0b\u5176\u4ed6\u7684\u955c\u50cf\u6e90\u3002<\/p>\n

Windows\u4e0a\u5b89\u88c5<\/strong><\/div>\n

\u5728 cmd \u7a97\u53e3\u67e5\u770b\u7cfb\u7edf\u662f\u5426\u5b89\u88c5\u4e86virtualenv\uff1a<\/p>\n

\r\nvirtualenv --version\r\n<\/pre>\n

pip \u5b89\u88c5 virtualenv<\/p>\n

\r\npip3 install virtualenv\r\npip3 install virtualenvwrapper-win\r\n<\/pre>\n

virtualenvwrapper-win\u865a\u62df\u73af\u5883\u6269\u5c55\u5305\uff08\u6307\u4ee4\u4fbf\u6377\uff09<\/p>\n

\u865a\u62df\u73af\u5883\u521b\u5efa<\/strong><\/div>\n

\u5728 cmd\u7a97\u53e3 \u4e0b\u521b\u5efa python3 \u865a\u62df\u73af\u5883 py_django\uff1a<\/p>\n

\r\nmkvirtualenv -p python3 py_django<\/pre>\n

\"\"<\/p>\n

\u6ce8\u610f\uff1a<\/p>\n

Windows \u521b\u5efa\u7684\u865a\u62df\u73af\u5883\u9ed8\u8ba4\u5728C:\\Users\\Administrator\\Envs, Administrator \u4ee3\u8868\u6211\u5f53\u524d\u7535\u8111\u4f7f\u7528\u7684\u7528\u6237\u4f60\u4eec\u7684\u53ef\u80fd\u5728\u5176\u4ed6\u7528\u6237\u4e0b\uff0c\u5982\u9700\u6307\u5b9a\u521b\u5efa\u7684\u865a\u62df\u73af\u5883\u7684\u5b58\u653e\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u5728\u7535\u8111\u7684\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u6dfb\u52a0WORKON_HOME\u7cfb\u7edf\u53d8\u91cf<\/p>\n

\"\"<\/p>\n

Linux\u4e0a\u5b89\u88c5<\/strong><\/div>\n
\u5b89\u88c5\u865a\u62df\u73af\u5883<\/strong><\/span><\/div>\n
\r\nsudo pip3 install virtualenv\r\nsudo pip3 install virtualenvwrapper\r\n<\/pre>\n

\u9700\u4fdd\u8bc1 Linux\u4e0a\u6709 Python3\u7684\u73af\u5883\u3002<\/p>\n

\u5b89\u88c5\u5b8c\u865a\u62df\u73af\u5883\u540e\u8f93\u5165\u5982\u4e0b\u6307\u4ee4\u6d4b\u8bd5<\/p>\n

\r\nmkvirtualenv test\r\n<\/pre>\n

\u5982\u679c\u63d0\u793a\u627e\u4e0d\u5230mkvirtualenv\u547d\u4ee4\uff0c\u987b\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/p>\n

\r\n# 1\u3001\u521b\u5efa\u76ee\u5f55\u7528\u6765\u5b58\u653e\u865a\u62df\u73af\u5883\r\nmkdir $HOME\/.virtualenvs\r\n \r\n# 2\u3001\u6253\u5f00~\/.bashrc\u6587\u4ef6\r\nvim ~\/.bashrc\r\n \r\n# 2.1 \u6dfb\u52a0\u5982\u4e0b\u4e24\u884c\r\nexport WORKON_HOME=$HOME\/.virtualenvs\r\nsource \/usr\/local\/bin\/virtualenvwrapper.sh\r\n \r\n# 3\u3001\u8fd0\u884c\r\nsource ~\/.bashrc\r\n<\/pre>\n

WORKON_HOME \u5b58\u653e\u865a\u62df\u73af\u5883\u76ee\u5f55\u4f4d\u7f6e\u53ef\u4ee5\u81ea\u5df1\u5355\u72ec\u8bbe\u7f6e\u3002<\/p>\n

\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/span><\/div>\n
\r\nmkvirtualenv py_flask\r\n<\/pre>\n
\u8fdb\u5165\u865a\u62df\u73af\u5883<\/strong><\/span><\/div>\n
\r\nworkon py_flask\r\n<\/pre>\n
\u9000\u51fa\u865a\u62df\u73af\u5883<\/strong><\/span><\/div>\n

\u5982\u679c\u6240\u5728\u73af\u5883\u4e3a\u771f\u5b9e\u73af\u5883\uff0c\u4f1a\u63d0\u793adeactivate\uff1a\u672a\u627e\u5230\u547d\u4ee4<\/p>\n

\r\ndeactivate py_flask\r\n<\/pre>\n
\u865a\u62df\u73af\u5883\u6307\u4ee4<\/strong><\/div>\n

Windows \u548c Linux \u5e73\u53f0\u4e0a\u7684\u6307\u4ee4\u90fd\u4e00\u81f4\u3002<\/p>\n

\"\"<\/p>\n

\u6ce8\u610f\uff1a<\/p>\n

\r\n\u5148\u9000\u51fa\uff1adeactivate\r\n\u518d\u5220\u9664\uff1armvirtualenv \u865a\u62df\u73af\u5883\u540d\u79f0\r\n<\/pre>\n
\u865a\u62df\u73af\u5883\u603b\u7ed3<\/strong><\/div>\n

\u4e00\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u4e0d\u4f1a\u8ba9\u4e3b\u89e3\u91ca\u5668\u597d\u591a\u5e93\u4e71<\/p>\n

\u4e8c\u3001\u4f8b\u5982\u5728\u5355\u72ec\u7684\u865a\u62df\u73af\u5883\u4e0b\u5b89\u88c5 Flask\uff0c\u5c31\u77e5\u9053\u5728\u540e\u7eed\u5f00\u53d1\u4e2d\u6240\u4f7f\u7528\u7684\u6240\u6709\u5e93\uff0c\u7528pip freeze\u547d\u4ee4\u5c31\u77e5\u9053\u4f60\u7684\u9879\u76ee\u9700\u8981\u4ec0\u4e48\u73af\u5883\uff0c\u800c\u5728\u4e3b\u89e3\u91ca\u5668\u6709\u597d\u591a\u9879\u76ee\u6ca1\u7528\u5230\u7684\u5e93\u3002\u4f7f\u7528\u865a\u62df\u73af\u5883\u5230\u90e8\u7f72\u5230\u670d\u52a1\u5668\u4e0a\u5219\u65b9\u4fbf\u73af\u5883\u914d\u7f6e\uff0c\u4e0d\u9700\u8981\u4e00\u4e2a\u4e00\u4e2a\u7b5b\u9009\u6240\u9700\u5e93\u3002<\/p>\n

\u5728\u865a\u62df\u73af\u5883\u4e2d\u5229\u7528<\/strong><\/span><\/div>\n

pip freeze \u67e5\u770b\u9879\u76ee\u6240\u9700\u5e93\u73af\u5883\u3002<\/p>\n

pip freeze > requirements.txt\u5bfc\u51fa\u9879\u76ee\u6240\u9700\u5e93\u4fe1\u606f\u3002<\/p>\n

pip install -r requirements.tx\u6279\u91cf\u5b89\u88c5\u9879\u76ee\u6240\u9700\u5e93\u3002<\/p>\n

\u4e09\u3001\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u5f00\u53d1\u591a\u4e2a\u9879\u76ee\uff0c\u800c\u9047\u5230\u9879\u76ee\u6240\u9700\u540c\u4e00\u5e93\u7684\u7248\u672c\u4e0d\u4e00\u81f4\u3002\u56e0\u6b64\u4f7f\u7528\u865a\u62df\u73af\u5883\u8ba9\u6bcf\u4e2a\u9879\u76ee\u72ec\u7acb\u9694\u79bb\u51fa\u6765\uff0c\u53ef\u4ee5\u5355\u72ec\u4f7f\u7528\u4e0d\u540c\u7248\u672c\u5e93\u3002<\/p>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8ePython\u865a\u62df\u73af\u5883virtualenv\u662f\u5982\u4f55\u4f7f\u7528\u7684\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5982\u679c\u5728\u4e00\u53f0\u673a\u5668\u4e0a\uff0c\u60f3\u5f00\u53d1\u591a\u4e2a\u4e0d\u540c\u7684\u9879\u76ee\uff0c\u9700\u8981\u7528\u5230\u540c\u4e00\u4e2a\u5305\u7684\u4e0d\u540c\u7248\u672c\uff0c\u5982\u679c\u8fd8\u4f7f\u7528\u4e0a\u9762\u7684\u547d\u4ee4\uff0c\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e0b\u5b89\u88c5\u6216 […]<\/p>\n","protected":false},"author":668,"featured_media":100858,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=221461"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221461\/revisions"}],"predecessor-version":[{"id":221467,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221461\/revisions\/221467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/100858"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=221461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=221461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=221461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}