第1章 创建虚拟环境
以下操作在mac系统上执行,Linux系统类似,而Windows的路径有点区别,主要是activate文件是在Scripts下,同时需要先执行set-executionpolicy remotesigned之后,再执行./.venv/Scripts/Activate.ps1
使用命令python -m venv .venv创建虚拟环境
huangsiyangdeiMac:projects huangsiyang$ python -m venv .venv
在刚才创建的虚拟环境路径下,使用命令source bin/activate激活虚拟环境:
huangsiyangdeiMac:projects huangsiyang$ source .venv/bin/activate
终端变成如下:
(.venv) huangsiyangdeiMac:projects huangsiyang$
执行pip3 --version,可以看到路径发生了变化,如:
(.venv) huangsiyangdeiMac:projects huangsiyang$ pip --version
pip 19.2.3 from /Users/huangsiyang/projects/.venv/lib/python3.7/site-packages/pip (python 3.7)
升级pip版本,如:
(.venv) huangsiyangdeiMac:projects huangsiyang$ pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/4d/16/0a14ca596f30316efd412a60bdfac02a7259bf8673d4d917dc60b9a21812/pip-22.0.4-py3-none-any.whl (2.1MB)
|████████████████████████████████| 2.1MB 679kB/s
Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-22.0.4
再查看pip版本,如:
(.venv) huangsiyangdeiMac:projects huangsiyang$ pip --version
pip 22.0.4 from /Users/huangsiyang/projects/.venv/lib/python3.7/site-packages/pip (python 3.7)
离开虚拟环境,如:
(.venv) huangsiyangdeiMac:projects huangsiyang$ deactivate
huangsiyangdeiMac:projects huangsiyang$
再次查看pip版本,如:
huangsiyangdeiMac:projects huangsiyang$ pip --version
pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)