{"id":82772,"date":"2023-11-23T22:55:04","date_gmt":"2023-11-23T14:55:04","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=82772"},"modified":"2023-11-23T22:55:04","modified_gmt":"2023-11-23T14:55:04","slug":"python-virtualenv","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-virtualenv.html","title":{"rendered":"Python \u4e2d virtualenv \u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"
\u5728\u5f00\u53d1Python\u5e94\u7528\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u5b89\u88c5\u7684Python3\u53ea\u6709\u4e00\u4e2a\u7248\u672c\uff1a3.4\u3002\u6240\u6709\u7b2c\u4e09\u65b9\u7684\u5305\u90fd\u4f1a\u88abpip<\/span>\u5b89\u88c5\u5230Python3\u7684site-packages<\/span>\u76ee\u5f55\u4e0b\u3002<\/p>\n \u5982\u679c\u6211\u4eec\u8981\u540c\u65f6\u5f00\u53d1\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\uff0c\u90a3\u8fd9\u4e9b\u5e94\u7528\u7a0b\u5e8f\u90fd\u4f1a\u5171\u7528\u4e00\u4e2aPython\uff0c\u5c31\u662f\u5b89\u88c5\u5728\u7cfb\u7edf\u7684Python 3\u3002\u5982\u679c\u5e94\u7528A\u9700\u8981jinja 2.7\uff0c\u800c\u5e94\u7528B\u9700\u8981jinja 2.6\u600e\u4e48\u529e\uff1f<\/p>\n \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u5e94\u7528\u53ef\u80fd\u9700\u8981\u5404\u81ea\u62e5\u6709\u4e00\u5957\u201c\u72ec\u7acb\u201d\u7684Python\u8fd0\u884c\u73af\u5883\u3002virtualenv\u5c31\u662f\u7528\u6765\u4e3a\u4e00\u4e2a\u5e94\u7528\u521b\u5efa\u4e00\u5957\u201c\u9694\u79bb\u201d\u7684Python\u8fd0\u884c\u73af\u5883\u3002<\/p>\n \u9996\u5148\uff0c\u6211\u4eec\u7528pip<\/span>\u5b89\u88c5virtualenv\uff1a<\/p>\n \u7136\u540e\uff0c\u5047\u5b9a\u6211\u4eec\u8981\u5f00\u53d1\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\uff0c\u9700\u8981\u4e00\u5957\u72ec\u7acb\u7684Python\u8fd0\u884c\u73af\u5883\uff0c\u53ef\u4ee5\u8fd9\u4e48\u505a\uff1a<\/p>\n \u7b2c\u4e00\u6b65\uff0c\u521b\u5efa\u76ee\u5f55\uff1a<\/p>\n \u7b2c\u4e8c\u6b65\uff0c\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684Python\u8fd0\u884c\u73af\u5883\uff0c\u547d\u540d\u4e3avenv<\/span>\uff1a<\/p>\n \u547d\u4ee4virtualenv<\/span>\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684Python\u8fd0\u884c\u73af\u5883\uff0c\u6211\u4eec\u8fd8\u52a0\u4e0a\u4e86\u53c2\u6570--no-site-packages<\/span>\uff0c\u8fd9\u6837\uff0c\u5df2\u7ecf\u5b89\u88c5\u5230\u7cfb\u7edfPython\u73af\u5883\u4e2d\u7684\u6240\u6709\u7b2c\u4e09\u65b9\u5305\u90fd\u4e0d\u4f1a\u590d\u5236\u8fc7\u6765\uff0c\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u4e00\u4e2a\u4e0d\u5e26\u4efb\u4f55\u7b2c\u4e09\u65b9\u5305\u7684\u201c\u5e72\u51c0\u201d\u7684Python\u8fd0\u884c\u73af\u5883\u3002<\/p>\n \u65b0\u5efa\u7684Python\u73af\u5883\u88ab\u653e\u5230\u5f53\u524d\u76ee\u5f55\u4e0b\u7684venv<\/span>\u76ee\u5f55\u3002\u6709\u4e86venv<\/span>\u8fd9\u4e2aPython\u73af\u5883\uff0c\u53ef\u4ee5\u7528source<\/span>\u8fdb\u5165\u8be5\u73af\u5883\uff1a<\/p>\n \u6ce8\u610f\u5230\u547d\u4ee4\u63d0\u793a\u7b26\u53d8\u4e86\uff0c\u6709\u4e2a(venv)<\/span>\u524d\u7f00\uff0c\u8868\u793a\u5f53\u524d\u73af\u5883\u662f\u4e00\u4e2a\u540d\u4e3avenv<\/span>\u7684Python\u73af\u5883\u3002<\/p>\n \u4e0b\u9762\u6b63\u5e38\u5b89\u88c5\u5404\u79cd\u7b2c\u4e09\u65b9\u5305\uff0c\u5e76\u8fd0\u884cpython<\/span>\u547d\u4ee4\uff1a<\/p>\n \u5728venv\u73af\u5883\u4e0b\uff0c\u7528pip\u5b89\u88c5\u7684\u5305\u90fd\u88ab\u5b89\u88c5\u5230venv\u8fd9\u4e2a\u73af\u5883\u4e0b\uff0c\u7cfb\u7edfPython\u73af\u5883\u4e0d\u53d7\u4efb\u4f55\u5f71\u54cd\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cvenv<\/span>\u73af\u5883\u662f\u4e13\u95e8\u9488\u5bf9myproject<\/span>\u8fd9\u4e2a\u5e94\u7528\u521b\u5efa\u7684\u3002<\/p>\n \u9000\u51fa\u5f53\u524d\u7684venv\u73af\u5883\uff0c\u4f7f\u7528deactivate<\/span>\u547d\u4ee4\uff1a<\/p>\n \u6b64\u65f6\u5c31\u56de\u5230\u4e86\u6b63\u5e38\u7684\u73af\u5883\uff0c\u73b0\u5728pip\u6216python\u5747\u662f\u5728\u7cfb\u7edfPython\u73af\u5883\u4e0b\u6267\u884c\u3002<\/p>\n \u5b8c\u5168\u53ef\u4ee5\u9488\u5bf9\u6bcf\u4e2a\u5e94\u7528\u521b\u5efa\u72ec\u7acb\u7684Python\u8fd0\u884c\u73af\u5883\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u5e94\u7528\u7684Python\u73af\u5883\u8fdb\u884c\u9694\u79bb\u3002<\/p>\n virtualenv\u662f\u5982\u4f55\u521b\u5efa\u201c\u72ec\u7acb\u201d\u7684Python\u8fd0\u884c\u73af\u5883\u7684\u5462\uff1f\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u628a\u7cfb\u7edfPython\u590d\u5236\u4e00\u4efd\u5230virtualenv\u7684\u73af\u5883\uff0c\u7528\u547d\u4ee4source venv\/bin\/activate<\/span>\u8fdb\u5165\u4e00\u4e2avirtualenv\u73af\u5883\u65f6\uff0cvirtualenv\u4f1a\u4fee\u6539\u76f8\u5173\u73af\u5883\u53d8\u91cf\uff0c\u8ba9\u547d\u4ee4python\u548cpip\u5747\u6307\u5411\u5f53\u524d\u7684virtualenv\u73af\u5883\u3002<\/p>\n virtualenv\u4e3a\u5e94\u7528\u63d0\u4f9b\u4e86\u9694\u79bb\u7684Python\u8fd0\u884c\u73af\u5883\uff0c\u89e3\u51b3\u4e86\u4e0d\u540c\u5e94\u7528\u95f4\u591a\u7248\u672c\u7684\u51b2\u7a81\u95ee\u9898\u3002<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttps:\/\/www.liaoxuefeng.com\/wiki\/0014316089557264a6b348958f449949df42a6d3a2e542c000\/001432712108300322c61f256c74803b43bfd65c6f8d0d0000<\/a><\/p>\n$ pip3 install virtualenv\r\n<\/pre>\n
Mac:~ michael$ mkdir myproject\r\nMac:~ michael$ cd myproject\/\r\nMac:myproject michael$\r\n<\/pre>\n
Mac:myproject michael$ virtualenv --no-site-packages venv\r\nUsing base prefix '\/usr\/local\/...\/Python.framework\/Versions\/3.4'\r\nNew python executable in venv\/bin\/python3.4\r\nAlso creating executable in venv\/bin\/python\r\nInstalling setuptools, pip, wheel...done.\r\n<\/pre>\n
Mac:myproject michael$ source venv\/bin\/activate\r\n(venv)Mac:myproject michael$\r\n<\/pre>\n
(venv)Mac:myproject michael$ pip install jinja2\r\n...\r\nSuccessfully installed jinja2-2.7.3 markupsafe-0.23\r\n(venv)Mac:myproject michael$ python myapp.py\r\n...\r\n<\/pre>\n
(venv)Mac:myproject michael$ deactivate \r\nMac:myproject michael$\r\n<\/pre>\n
\n