1 进入CMD
, 使用命令python -m venv
创建虚拟环境
C:\Users\xupeng>python -m venv d:\test_venv
C:\Users\xupeng>
就会在d:\test_venv
下创建虚拟环境。
2 进入Scripts目录
,并执行命令activate.bat
进行激活
activate.bat
其实就位于Scripts
目录下:
C:\Users\xupeng>cd d:\test_venv
C:\Users\xupeng>d:
d:\test_venv>cd Scripts
d:\test_venv\Scripts>activate.bat
(test_venv) d:\test_venv\Scripts>
注意上面调用activate.bat
后,下一行的命令提示前出现了(test_venv)
这个标识,这就表示,之后所有的命令都是对这个虚拟环境test_venv
进行操作了。这也是使用activate.bat的作用
。activate.bat其实就位于Scripts下。
activate.bat只需要执行一次即可。之后进入到虚拟环境下中的Scripts目录,进行的所有操作就是对这个虚拟环境进行的操作。
3 在Scripts目录下,执行命令pip list查看当前虚拟环境中的安装包
(test_venv) d:\test_venv\Scripts>pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (28.8.0)
You are using pip version 9.0.1, however version 21.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
显示当前虚拟环境中只预装了pip
和setuptools
这两个安装包。
4 依旧在当前目录下,使用命令pip install
安装包
(test_venv) d:\test_venv\Scripts>pip install zipp
Collecting zipp
Using cached https://files.pythonhosted.org/packages/0f/8c/715c54e9e34c0c4820f616a913a7de3337d0cd79074dd1bed4dd840f16ae/zipp-3.4.1-py3-none-any.whl
Installing collected packages: zipp
Successfully installed zipp-3.4.1
You are using pip version 9.0.1, however version 21.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
安装成功后,会在Lib\site-packages\
目录下保存相关文件:
再次使用命令pip list
查看当前已安装包:
(test_venv) d:\test_venv\Scripts>pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (28.8.0)
zipp (3.4.1)
You are using pip version 9.0.1, however version 21.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
可以看到zipp确实已经安装成功了。
5 重要提醒
注意,之后的操作一定要在虚拟目录中的Scripts下
进行操作,否则操作的就不是当前虚拟环境,而是全局环境了。
非Scripts目录下操作结果:
D:\test_venv>pip list
Package Version
-------------------- ---------
Appium-Python-Client 1.1.0
argon2-cffi 20.1.0
attrs 20.3.0
backcall 0.2.0
...
webencodings 0.5.1
Werkzeug 1.0.1
Scripts目录下操作结果:
D:\test_venv\Scripts>pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (28.8.0)
zipp (3.4.1)
You are using pip version 9.0.1, however version 21.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
6 通过Pycharm来打开该虚拟环境,查看相关数据
打开工程后,使用快捷键Ctrl+Alt+S
进入设置界面
:
可以看到当前并未设置Python解释器
,也无任何的已安装包。单击右侧的设置按钮
,单机Add,进入设置Python解释器界面
:点选Existing environment
,下面会自动识别出解释器位置。单机OK即可。
再次回到Python解释器
界面:
即可看到,当前虚拟环境的解释器就是其自身的解释器,同时已经安装的包也和前面CMD中的输出一致了。
7 Pycharm创建虚拟环境
也可以直接通过Pycharm创建虚拟环境,可参考Python Pycharm创建虚拟环境.