Python (python -m venv)命令创建虚拟环境

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.

显示当前虚拟环境中只预装了pipsetuptools这两个安装包。

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创建虚拟环境.

  • 28
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值