Python虚拟环境
第一种方法:
在系统级别的python下,执行pip install virtualenv
C:\Users\Administrator>pip install virtualenv -i https://pypi.doubanio.com/simple
Looking in indexes: https://pypi.doubanio.com/simple
Collecting virtualenv
Downloading https://pypi.doubanio.com/packages/9b/67/f28095ba538be54fc48ec7774d5778a7246972cf8c701430ec0c96bf0860/virtualenv-20.0.20-py2.py3-none-any.whl (4.7 MB)
|████████████████████████████████| 4.7 MB 6.4 MB/s
Collecting filelock<4,>=3.0.0
Downloading https://pypi.doubanio.com/packages/93/83/71a2ee6158bb9f39a90c0dea1637f81d5eef866e188e1971a1b1ab01a35a/filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting appdirs<2,>=1.4.3
Downloading https://pypi.doubanio.com/packages/3b/00/2344469e2084fb287c2e0b57b72910309874c3245463acd6cf5e3db69324/appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting distlib<1,>=0.3.0
Downloading https://pypi.doubanio.com/packages/7d/29/694a3a4d7c0e1aef76092e9167fbe372e0f7da055f5dcf4e1313ec21d96a/distlib-0.3.0.zip (571 kB)
|████████████████████████████████| 571 kB 595 kB/s
Requirement already satisfied: six<2,>=1.9.0 in c:\users\administrator\appdata\roaming\python\python38\site-packages (from virtualenv) (1.14.0)
Could not build wheels for distlib, since package 'wheel' is not installed.
Could not build wheels for six, since package 'wheel' is not installed.
Installing collected packages: filelock, appdirs, distlib, virtualenv
Running setup.py install for distlib ... done
Successfully installed appdirs-1.4.4 distlib-0.3.0 filelock-3.0.12 virtualenv-20.0.20
确认已安装
C:\Users\Administrator>pip list
Package Version
---------------------- ----------
appdirs 1.4.4
astroid 2.4.1
certifi 2020.4.5.1
chardet 3.0.4
distlib 0.3.0
filelock 3.0.12
six 1.14.0
virtualenv 20.0.20
新建一个虚拟环境目录,virtualenv [虚拟环境名]
C:\Users\Administrator>E:
E:\>virtualenv django
created virtual environment CPython3.8.2.final.0-64 in 1230ms
creator CPython3Windows(dest=E:\django, clear=False, global=False)
seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=C:\Users\Administrator\AppData\Local\pypa\virtualenv\seed-app-data\v1.0.1)
activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
先切换到虚拟环境目录,cd django\Scripts
,再进入虚拟环境,activate
E:\>cd django\Scripts
E:\django\Scripts>activate
(django) E:\django\Scripts>
以后项目所需要的库,都在此虚拟环境下安装
(django) E:\django\Scripts>pip list
Package Version
---------- -------
pip 20.1
setuptools 46.1.3
wheel 0.34.2
退出虚拟环境deactivate
(django) E:\django\Scripts>deactivate
E:\django\Scripts>
第二种方法:
直接安装virtualenvwrapper
,用pip install virtualenvwrapper-win
C:\Users\Administrator>pip install virtualenvwrapper-win -i https://pypi.doubanio.com/simple
Looking in indexes: https://pypi.doubanio.com/simple
Collecting virtualenvwrapper-win
Downloading https://pypi.doubanio.com/packages/d3/07/7599a80e13e58e0bb561ed03c55a2a84872b64ae50cbc40d9bf7ebabbdd9/virtualenvwrapper-win-1.2.6.tar.gz (21 kB)
Requirement already satisfied: virtualenv in c:\python\python38\lib\site-packages (from virtualenvwrapper-win) (20.0.20)
Requirement already satisfied: appdirs<2,>=1.4.3 in c:\python\python38\lib\site-packages (from virtualenv->virtualenvwrapper-win) (1.4.4)
Requirement already satisfied: six<2,>=1.9.0 in c:\users\administrator\appdata\roaming\python\python38\site-packages (from virtualenv->virtualenvwrapper-win) (1.14.0)
Requirement already satisfied: filelock<4,>=3.0.0 in c:\python\python38\lib\site-packages (from virtualenv->virtualenvwrapper-win) (3.0.12)
Requirement already satisfied: distlib<1,>=0.3.0 in c:\python\python38\lib\site-packages (from virtualenv->virtualenvwrapper-win) (0.3.0)
Could not build wheels for virtualenvwrapper-win, since package 'wheel' is not installed.
Could not build wheels for virtualenv, since package 'wheel' is not installed.
Could not build wheels for appdirs, since package 'wheel' is not installed.
Could not build wheels for six, since package 'wheel' is not installed.
Could not build wheels for filelock, since package 'wheel' is not installed.
Could not build wheels for distlib, since package 'wheel' is not installed.
Installing collected packages: virtualenvwrapper-win
Running setup.py install for virtualenvwrapper-win ... done
Successfully installed virtualenvwrapper-win-1.2.6
创建虚拟环境,mkvirtualenv [虚拟环境名]
,创建好后自动进入,默认在C:\Users\用户名\Envs
C:\Users\Administrator>mkvirtualenv xxx-env
C:\Users\Administrator\Envs is not a directory, creating
created virtual environment CPython3.8.2.final.0-64 in 740ms
creator CPython3Windows(dest=C:\Users\Administrator\Envs\xxx-env, clear=False, global=False)
seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=C:\Users\Administrator\AppData\Local\pypa\virtualenv\seed-app-data\v1.0.1)
activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
(xxx-env) C:\Users\Administrator>pip list
Package Version
---------- -------
pip 20.1
setuptools 46.1.3
wheel 0.34.2
添加环境变量,可以改变虚拟环境目录
退出虚拟环境deactivate
(xxx-env) C:\Users\Administrator>deactivate
C:\Users\Administrator>
再次进入用workon [虚拟环境名]
C:\Users\Administrator>workon xxx-env
(xxx-env) C:\Users\Administrator>
列出所有虚拟环境lsvirtualenv
C:\Users\Administrator>lsvirtualenv
dir /b /ad "C:\Users\Administrator\Envs"
==============================================================================
xxx-env
删除虚拟环境rmvirtualenv [虚拟环境名]
C:\Users\Administrator>rmvirtualenv xxx-env
Deleted C:\Users\Administrator\Envs\xxx-env
===========
xxx-env
删除虚拟环境`rmvirtualenv [虚拟环境名]`
```bash
C:\Users\Administrator>rmvirtualenv xxx-env
Deleted C:\Users\Administrator\Envs\xxx-env