版权声明:本文为 [onefine] 原创文章,转载请注明出处。 https://blog.csdn.net/jiduochou963/article/details/86547563
Python虚拟环境安装,以Windows为例
virtualenv
virtualenv优点
- 使不同应用开发环境独立
- 环境升级不影响其他应用,也不会影响全局的python环境
- 它可以防止系统中出现包管理混乱和版本的冲突
测试pip是否正常
C:\Users\ONEFINE>pip
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to
WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
(a)bort).
--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the
certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for
download. Implied with --no-index.
--no-color Suppress colored output
C:\Users\ONEFINE>
安装virtualenv
C:\Users\ONEFINE>pip install virtualenv
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/6a/d1/e0d142ce7b8a5c76adbfad01d853bca84c7c0240e35577498e20bc2ade7d/virtualenv-16.2.0-py2.py3-none-any.whl (1.9MB)
100% |████████████████████████████████| 1.9MB 33kB/s
Requirement already satisfied: setuptools>=18.0.0 in c:\users\onefine\appdata\local\programs\python\python37\lib\site-packages (from virtualenv) (39.0.1)
Installing collected packages: virtualenv
Successfully installed virtualenv-16.2.0
C:\Users\ONEFINE>
创建virtualenv
默认情况安装在当前命令的运行目录下边,
virtualenv 名称
例如,在指定目录下创建一个名为ve_test_0
的虚拟环境:
D:\>mkdir test
D:\>cd test
D:\test>virtualenv ve_test_0
Using base prefix 'c:\\users\\onefine\\appdata\\local\\programs\\python\\python37'
New python executable in D:\test\ve_test_0\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
D:\test>
查看一下:
D:\test>dir ve_test_0
D:\test\ve_test_0 的目录
2019/01/18 21:24 <DIR> .
2019/01/18 21:24 <DIR> ..
2018/12/24 12:20 <DIR> Include
2019/01/18 21:24 <DIR> Lib
2019/01/18 21:24 <DIR> Scripts
2019/01/18 21:24 <DIR> tcl
0 个文件 0 字节
6 个目录 61,376,417,792 可用字节
D:\test>
进入virtualenv
运行ve_test_0\Scripts>dir下面的activate.bat
文件
D:\test>cd ve_test_0\Scripts
D:\test\ve_test_0\Scripts>dir
驱动器 D 中的卷没有标签。
卷的序列号是 D644-E329
D:\test\ve_test_0\Scripts 的目录
2019/01/18 21:24 <DIR> .
2019/01/18 21:24 <DIR> ..
2019/01/18 21:24 2,186 activate
2019/01/18 21:24 758 activate.bat
2019/01/18 21:24 1,544 activate.ps1
2019/01/18 21:24 997 activate.xsh
2019/01/18 21:24 1,512 activate_this.py
2019/01/18 21:24 512 deactivate.bat
2019/01/18 21:24 102,775 easy_install-3.7.exe
2019/01/18 21:24 102,775 easy_install.exe
2019/01/18 21:24 102,757 pip.exe
2019/01/18 21:24 102,757 pip3.7.exe
2019/01/18 21:24 102,757 pip3.exe
2019/01/18 21:24 99,992 python.exe
2019/01/18 21:24 59,032 python3.dll
2019/01/18 21:24 3,771,032 python37.dll
2019/01/18 21:24 7,843,328 python37_d.dll
2019/01/18 21:24 69,632 python3_d.dll
2019/01/18 21:24 98,456 pythonw.exe
2019/01/18 21:24 135,680 python_d.exe
2019/01/18 21:24 102,753 wheel.exe
19 个文件 12,701,235 字节
2 个目录 61,376,417,792 可用字节
D:\test\ve_test_0\Scripts>activate.bat
(ve_test_0) D:\test\ve_test_0\Scripts>
查看当前虚拟环境中安装的库:
pip list
(ve_test_0) D:\test\ve_test_0\Scripts>pip list
Package Version
---------- -------
pip 18.1
setuptools 40.6.3
wheel 0.32.3
(ve_test_0) D:\test\ve_test_0\Scripts>
退出虚拟环境:
(ve_test_0) D:\test\ve_test_0\Scripts>deactivate.bat
D:\test\ve_test_0\Scripts>
virtualenv创建的虚拟环境运行时需要知道安装时的目录,比较麻烦,我们可以使用另一个开发库————virtualenvwrapper
virtualenvwrapper
virtualenvwrapper 时一个基于virtualenv之上的工具,它将所欲的虚拟环境统一管理。
安装
D:\test\ve_test_0\Scripts>pip install virtualenvwrapper-win
Collecting virtualenvwrapper-win
Downloading https://files.pythonhosted.org/packages/f5/23/4cba98733b9122219ce67177d745e4984b524b867cf3728eaa807ea21919/virtualenvwrapper-win-1.2.5.tar.gz
Collecting virtualenv (from virtualenvwrapper-win)
Using cached https://files.pythonhosted.org/packages/6a/d1/e0d142ce7b8a5c76adbfad01d853bca84c7c0240e35577498e20bc2ade7d/virtualenv-16.2.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools>=18.0.0 in d:\test\ve_test_0\lib\site-packages (from virtualenv->virtualenvwrapper-win) (40.6.3)
Building wheels for collected packages: virtualenvwrapper-win
Running setup.py bdist_wheel for virtualenvwrapper-win ... done
Stored in directory: C:\Users\ONEFINE\AppData\Local\pip\Cache\wheels\47\d1\ae\85075521c1fbe2edb6784af24ccc79af10bbb205cc711a689c
Successfully built virtualenvwrapper-win
Installing collected packages: virtualenv, virtualenvwrapper-win
Successfully installed virtualenv-16.2.0 virtualenvwrapper-win-1.2.5
D:\test\ve_test_0\Scripts>
注意:如果是在Linux环境下,直接pip install virtualenvwrapper
即可。
新建虚拟环境
mkvirtualenv 名称
virtualenvwrapper
会将所有新建的virtualenv
放在同一个目录下边:
D:\test\ve_test_0\Scripts>mkvirtualenv ve_test_1
C:\Users\ONEFINE\Envs is not a directory, creating
Using base prefix 'c:\\users\\onefine\\appdata\\local\\programs\\python\\python37'
New python executable in C:\Users\ONEFINE\Envs\ve_test_1\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
(ve_test_1) D:\test\ve_test_0\Scripts>
可以看到,新建的virtualenv
被放在C:\Users\ONEFINE\Envs\
下,并且安装完成之后就自动进入了新建的ve_test_1
虚拟环境下。
查看一下安装目录:
D:\test\ve_test_0\Scripts>dir cd c:\Users\ONEFINE\Envs\ve_test_1\Scripts
驱动器 D 中的卷没有标签。
卷的序列号是 D644-E329
D:\test\ve_test_0\Scripts 的目录
c:\Users\ONEFINE\Envs\ve_test_1\Scripts 的目录
2019/01/18 21:48 <DIR> .
2019/01/18 21:48 <DIR> ..
2019/01/18 21:48 2,214 activate
2019/01/18 21:48 964 activate.bat
2019/01/18 21:48 1,544 activate.ps1
2019/01/18 21:48 1,011 activate.xsh
2019/01/18 21:48 1,512 activate_this.py
2019/01/18 21:48 603 deactivate.bat
2019/01/18 21:48 102,789 easy_install-3.7.exe
2019/01/18 21:48 102,789 easy_install.exe
2019/01/18 21:48 102,771 pip.exe
2019/01/18 21:48 102,771 pip3.7.exe
2019/01/18 21:48 102,771 pip3.exe
2019/01/18 21:48 99,992 python.exe
2019/01/18 21:48 59,032 python3.dll
2019/01/18 21:48 3,771,032 python37.dll
2019/01/18 21:48 7,843,328 python37_d.dll
2019/01/18 21:48 69,632 python3_d.dll
2019/01/18 21:48 98,456 pythonw.exe
2019/01/18 21:48 135,680 python_d.exe
2019/01/18 21:48 102,767 wheel.exe
19 个文件 12,701,658 字节
2 个目录 68,484,141,056 可用字节
D:\test\ve_test_0\Scripts>
D:\test\ve_test_0\Scripts>c:
C:\Users\ONEFINE\Envs>\Users\ONEFINE\Envs\ve_test_1\Scripts\activate.bat
(ve_test_1) C:\Users\ONEFINE\Envs>deactivate.bat
C:\Users\ONEFINE\Envs>dir \Users\ONEFINE\Envs\
驱动器 C 中的卷是 OS
卷的序列号是 BE92-F6A8
C:\Users\ONEFINE\Envs 的目录
2019/01/18 21:48 <DIR> .
2019/01/18 21:48 <DIR> ..
2019/01/18 21:48 <DIR> ve_test_1
0 个文件 0 字节
3 个目录 68,478,033,920 可用字节
C:\Users\ONEFINE\Envs>
查看当前所有的虚拟环境
C:\Users\ONEFINE>workon
Pass a name to activate one of the following virtualenvs:
==============================================================================
ve_test_1
C:\Users\ONEFINE>
可见,这里列出的只是通过virtualenvwrapper
安装的。
注:如果提示workon命令不存在,重新打开命令行即可使用。
扫描二维码关注公众号,回复:
5088555 查看本文章
进入某一个已经安装好的虚拟环境:
C:\Users\ONEFINE>workon ve_test_1
(ve_test_1) C:\Users\ONEFINE>
(ve_test_1) C:\Users\ONEFINE>deactivate
C:\Users\ONEFINE>
查看当前虚拟环境中安装的开发包
C:\Users\ONEFINE>workon ve_test_1
(ve_test_1) C:\Users\ONEFINE>pip list
Package Version
---------- -------
pip 18.1
setuptools 40.6.3
wheel 0.32.3
(ve_test_1) C:\Users\ONEFINE>
在虚拟环境中安装开发包
(ve_test_1) C:\Users\ONEFINE>pip install requests
Collecting requests
Using cached https://files.pythonhosted.org/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl
Collecting urllib3<1.25,>=1.21.1 (from requests)
Using cached https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached https://files.pythonhosted.org/packages/9f/e0/accfc1b56b57e9750eba272e24c4dddeac86852c2bebd1236674d7887e8a/certifi-2018.11.29-py2.py3-none-any.whl
Collecting idna<2.9,>=2.5 (from requests)
Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl
Installing collected packages: urllib3, chardet, certifi, idna, requests
Successfully installed certifi-2018.11.29 chardet-3.0.4 idna-2.8 requests-2.21.0 urllib3-1.24.1
(ve_test_1) C:\Users\ONEFINE>
查看一下:
(ve_test_1) C:\Users\ONEFINE>pip list
Package Version
---------- ----------
certifi 2018.11.29
chardet 3.0.4
idna 2.8
pip 18.1
requests 2.21.0
setuptools 40.6.3
urllib3 1.24.1
wheel 0.32.3
(ve_test_1) C:\Users\ONEFINE>
卸载虚拟环境中已经安装的包
(ve_test_1) C:\Users\ONEFINE>pip uninstall requests
Uninstalling requests-2.21.0:
Would remove:
c:\users\onefine\envs\ve_test_1\lib\site-packages\requests-2.21.0.dist-info\*
c:\users\onefine\envs\ve_test_1\lib\site-packages\requests\*
Proceed (y/n)? y
Successfully uninstalled requests-2.21.0
(ve_test_1) C:\Users\ONEFINE>
再次查看:
(ve_test_1) C:\Users\ONEFINE>pip list
Package Version
---------- ----------
certifi 2018.11.29
chardet 3.0.4
idna 2.8
pip 18.1
setuptools 40.6.3
urllib3 1.24.1
wheel 0.32.3
(ve_test_1) C:\Users\ONEFINE>deactivate
C:\Users\ONEFINE>