animalize

导航

Windows下用Python 3.4+自带的venv模块创建虚拟环境

Python 3.4+自带了venv模块,用于创建虚拟环境,每个虚拟环境都可以安装一套独立的第三方模块。

本文在Windows 10上操作。

 

1、创建一个虚拟环境:

D:\>mkdir test_venv
D:\>cd test_venv
D:\test_venv>python -m venv test

第三行,使用venv模块创建一个名为test的虚拟环境。

执行后,生成了一个test目录,内有1个文件、3个目录:

 test
   │  pyvenv.cfg
   │  
   ├─Include
   ├─Lib
   └─Scripts

 

2、启用虚拟环境:

D:\test_venv>test\Scripts\activate.bat
(test) D:\test_venv>

执行那个activate.bat文件,启用后,提示符前面会出现虚拟环境的名字(test)。

 

3、用pip给虚拟环境安装模块:

用pip list看一下已有的模块,能看到只有两个Python自带的模块:

(test) D:\test_venv>pip list
pip (8.1.1)
setuptools (20.10.1)
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

提示pip有新版本,按提示用'python -m pip install --upgrade pip'命令更新就好了。

在虚拟环境里,千万别用'pip install --upgrade pip'更新pip,这会破坏pip。

 

现在,给虚拟环境安装tornado试试:

(test) D:\test_venv>pip install tornado
Collecting tornado
  Using cached tornado-4.4.1-cp35-cp35m-win_amd64.whl
Installing collected packages: tornado
Successfully installed tornado-4.4.1

已成功安装tornado。

 

4、退出虚拟环境:

(test) D:\test_venv>test\Scripts\deactivate.bat
D:\test_venv>

执行deactivate.bat后,退出虚拟环境,此时命令行提示符前的(test)消失了。

posted on 2016-09-13 16:00  animalize  阅读(2914)  评论(0编辑  收藏  举报