[{"data":1,"prerenderedAt":413},["ShallowReactive",2],{"content-query-2FPCkPieEQ":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":7,"date":9,"url":10,"tags":11,"en-title":13,"body":14,"_type":407,"_id":408,"_source":409,"_file":410,"_stem":411,"_extension":412},"/blog/virtual-environment","blog",false,"","虚拟环境","2022-10-12 20:14:47",null,[12],"code","virtual environment",{"type":15,"children":16,"toc":401},"root",[17,25,38,395],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"虚拟环境创建与清除",[23],{"type":24,"value":21},"text",{"type":18,"tag":26,"props":27,"children":29},"h3",{"id":28},"使用-virtualenv",[30,32],{"type":24,"value":31},"使用 ",{"type":18,"tag":12,"props":33,"children":35},{"className":34},[],[36],{"type":24,"value":37},"virtualenv",{"type":18,"tag":39,"props":40,"children":41},"ol",{},[42,81,119,155,173,191,221,250,287,322,344,366],{"type":18,"tag":43,"props":44,"children":45},"li",{},[46,52,54,59,61],{"type":18,"tag":47,"props":48,"children":49},"strong",{},[50],{"type":24,"value":51},"安装 virtualenv",{"type":24,"value":53},"： 如果你还没有安装 ",{"type":18,"tag":12,"props":55,"children":57},{"className":56},[],[58],{"type":24,"value":37},{"type":24,"value":60},"，可以通过以下命令安装：",{"type":18,"tag":62,"props":63,"children":67},"pre",{"code":64,"language":65,"meta":7,"className":66,"style":7},"pip install virtualenv\n","python","language-python shiki shiki-themes vitesse-light vitesse-dark monokai",[68],{"type":18,"tag":12,"props":69,"children":70},{"__ignoreMap":7},[71],{"type":18,"tag":72,"props":73,"children":76},"span",{"class":74,"line":75},"line",1,[77],{"type":18,"tag":72,"props":78,"children":79},{},[80],{"type":24,"value":64},{"type":18,"tag":43,"props":82,"children":83},{},[84,89,91,105,109,111,117],{"type":18,"tag":47,"props":85,"children":86},{},[87],{"type":24,"value":88},"创建一个新的虚拟环境",{"type":24,"value":90},"： 你可以指定一个路径来存储虚拟环境的文件。以下命令将在指定位置创建一个新的虚拟环境：",{"type":18,"tag":62,"props":92,"children":94},{"code":93,"language":65,"meta":7,"className":66,"style":7},"virtualenv /path/to/your/env\n",[95],{"type":18,"tag":12,"props":96,"children":97},{"__ignoreMap":7},[98],{"type":18,"tag":72,"props":99,"children":100},{"class":74,"line":75},[101],{"type":18,"tag":72,"props":102,"children":103},{},[104],{"type":24,"value":93},{"type":18,"tag":106,"props":107,"children":108},"br",{},[],{"type":24,"value":110},"替换 ",{"type":18,"tag":12,"props":112,"children":114},{"className":113},[],[115],{"type":24,"value":116},"/path/to/your/env",{"type":24,"value":118}," 为你想要创建环境的具体路径。",{"type":18,"tag":43,"props":120,"children":121},{},[122,127,129,132,134,142,145,147],{"type":18,"tag":47,"props":123,"children":124},{},[125],{"type":24,"value":126},"激活虚拟环境",{"type":24,"value":128},"：",{"type":18,"tag":106,"props":130,"children":131},{},[],{"type":24,"value":133},"在 Windows 上，使用以下命令：",{"type":18,"tag":62,"props":135,"children":137},{"code":136},"\\path\\to\\your\\env\\Scripts\\Activate\n或者：& \"d:\\xnhj\\hanlp\\env\\Scripts\\Activate.ps1\"\n",[138],{"type":18,"tag":12,"props":139,"children":140},{"__ignoreMap":7},[141],{"type":24,"value":136},{"type":18,"tag":106,"props":143,"children":144},{},[],{"type":24,"value":146},"在 macOS 或 Linux 上，使用以下命令：",{"type":18,"tag":62,"props":148,"children":150},{"code":149},"source /path/to/your/env/bin/activate\n",[151],{"type":18,"tag":12,"props":152,"children":153},{"__ignoreMap":7},[154],{"type":24,"value":149},{"type":18,"tag":43,"props":156,"children":157},{},[158,163,165],{"type":18,"tag":47,"props":159,"children":160},{},[161],{"type":24,"value":162},"安装所需的包",{"type":24,"value":164},"： 激活虚拟环境后，你可以安装任何所需的包，而不会影响全局 Python 环境。例如，安装 TensorFlow 和 HanLP：",{"type":18,"tag":62,"props":166,"children":168},{"code":167},"pip install tensorflow\npip install hanlp[full]\n",[169],{"type":18,"tag":12,"props":170,"children":171},{"__ignoreMap":7},[172],{"type":24,"value":167},{"type":18,"tag":43,"props":174,"children":175},{},[176,181,183],{"type":18,"tag":47,"props":177,"children":178},{},[179],{"type":24,"value":180},"退出虚拟环境",{"type":24,"value":182},"： 当你完成工作并想退出虚拟环境时，可以简单地运行：",{"type":18,"tag":62,"props":184,"children":186},{"code":185},"deactivate\n",[187],{"type":18,"tag":12,"props":188,"children":189},{"__ignoreMap":7},[190],{"type":24,"value":185},{"type":18,"tag":43,"props":192,"children":193},{},[194,199,200,208,211,213,219],{"type":18,"tag":47,"props":195,"children":196},{},[197],{"type":24,"value":198},"创建环境",{"type":24,"value":128},{"type":18,"tag":62,"props":201,"children":203},{"code":202},"conda create --name myenv\n",[204],{"type":18,"tag":12,"props":205,"children":206},{"__ignoreMap":7},[207],{"type":24,"value":202},{"type":18,"tag":106,"props":209,"children":210},{},[],{"type":24,"value":212},"这会创建一个名为 ",{"type":18,"tag":12,"props":214,"children":216},{"className":215},[],[217],{"type":24,"value":218},"myenv",{"type":24,"value":220}," 的新环境。",{"type":18,"tag":43,"props":222,"children":223},{},[224,229,230,238,241,243,248],{"type":18,"tag":47,"props":225,"children":226},{},[227],{"type":24,"value":228},"指定 Python 版本",{"type":24,"value":128},{"type":18,"tag":62,"props":231,"children":233},{"code":232},"conda create --name myenv python=3.8\n",[234],{"type":18,"tag":12,"props":235,"children":236},{"__ignoreMap":7},[237],{"type":24,"value":232},{"type":18,"tag":106,"props":239,"children":240},{},[],{"type":24,"value":242},"这将创建一个名为 ",{"type":18,"tag":12,"props":244,"children":246},{"className":245},[],[247],{"type":24,"value":218},{"type":24,"value":249}," 的环境，并在其中安装 Python 3.8。",{"type":18,"tag":43,"props":251,"children":252},{},[253,258,259,267,270,272,277,279,285],{"type":18,"tag":47,"props":254,"children":255},{},[256],{"type":24,"value":257},"激活环境",{"type":24,"value":128},{"type":18,"tag":62,"props":260,"children":262},{"code":261},"conda activate myenv\n",[263],{"type":18,"tag":12,"props":264,"children":265},{"__ignoreMap":7},[266],{"type":24,"value":261},{"type":18,"tag":106,"props":268,"children":269},{},[],{"type":24,"value":271},"这会激活名为 ",{"type":18,"tag":12,"props":273,"children":275},{"className":274},[],[276],{"type":24,"value":218},{"type":24,"value":278}," 的环境。在 Windows 上可能需要使用 ",{"type":18,"tag":12,"props":280,"children":282},{"className":281},[],[283],{"type":24,"value":284},"activate myenv",{"type":24,"value":286}," 命令。",{"type":18,"tag":43,"props":288,"children":289},{},[290,295,296,299,301,307,309,317,320],{"type":18,"tag":47,"props":291,"children":292},{},[293],{"type":24,"value":294},"安装包",{"type":24,"value":128},{"type":18,"tag":106,"props":297,"children":298},{},[],{"type":24,"value":300},"在激活环境后，你可以使用 ",{"type":18,"tag":12,"props":302,"children":304},{"className":303},[],[305],{"type":24,"value":306},"conda install",{"type":24,"value":308}," 命令安装包。例如：",{"type":18,"tag":62,"props":310,"children":312},{"code":311},"conda install numpy\n",[313],{"type":18,"tag":12,"props":314,"children":315},{"__ignoreMap":7},[316],{"type":24,"value":311},{"type":18,"tag":106,"props":318,"children":319},{},[],{"type":24,"value":321},"这会在当前环境中安装 NumPy。",{"type":18,"tag":43,"props":323,"children":324},{},[325,330,331,339,342],{"type":18,"tag":47,"props":326,"children":327},{},[328],{"type":24,"value":329},"列出已安装的包",{"type":24,"value":128},{"type":18,"tag":62,"props":332,"children":334},{"code":333},"conda list\n",[335],{"type":18,"tag":12,"props":336,"children":337},{"__ignoreMap":7},[338],{"type":24,"value":333},{"type":18,"tag":106,"props":340,"children":341},{},[],{"type":24,"value":343},"这会列出当前环境中安装的所有包。",{"type":18,"tag":43,"props":345,"children":346},{},[347,352,353,361,364],{"type":18,"tag":47,"props":348,"children":349},{},[350],{"type":24,"value":351},"退出环境",{"type":24,"value":128},{"type":18,"tag":62,"props":354,"children":356},{"code":355},"conda deactivate\n",[357],{"type":18,"tag":12,"props":358,"children":359},{"__ignoreMap":7},[360],{"type":24,"value":355},{"type":18,"tag":106,"props":362,"children":363},{},[],{"type":24,"value":365},"这会退出当前环境，返回到基础环境。",{"type":18,"tag":43,"props":367,"children":368},{},[369,374,375,383,386,388,393],{"type":18,"tag":47,"props":370,"children":371},{},[372],{"type":24,"value":373},"删除环境",{"type":24,"value":128},{"type":18,"tag":62,"props":376,"children":378},{"code":377},"conda remove --name myenv --all\n",[379],{"type":18,"tag":12,"props":380,"children":381},{"__ignoreMap":7},[382],{"type":24,"value":377},{"type":18,"tag":106,"props":384,"children":385},{},[],{"type":24,"value":387},"这会删除名为 ",{"type":18,"tag":12,"props":389,"children":391},{"className":390},[],[392],{"type":24,"value":218},{"type":24,"value":394}," 的环境及其所有内容。",{"type":18,"tag":396,"props":397,"children":398},"style",{},[399],{"type":24,"value":400},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html .sepia .shiki span {color: var(--shiki-sepia);background: var(--shiki-sepia-bg);font-style: var(--shiki-sepia-font-style);font-weight: var(--shiki-sepia-font-weight);text-decoration: var(--shiki-sepia-text-decoration);}html.sepia .shiki span {color: var(--shiki-sepia);background: var(--shiki-sepia-bg);font-style: var(--shiki-sepia-font-style);font-weight: var(--shiki-sepia-font-weight);text-decoration: var(--shiki-sepia-text-decoration);}",{"title":7,"searchDepth":402,"depth":402,"links":403},2,[404],{"id":28,"depth":405,"text":406},3,"使用 virtualenv","markdown","content:blog:virtual-environment.md","content","blog/virtual-environment.md","blog/virtual-environment","md",1780733631918]