Python虚环境

引子

在平时的学习中,需要用到python语言写的不同的工具,这些工具大都需要安装requirement.txt中要求的版本的库,有时候不同的工具要求的库的版本不同时,需要来回切换,有些还需要切换python的版本,所以我们需要一个办法来应对这种情况,python虚环境(venv)就是一个不错的解决方法

参考链接

看了许多文章视频,我按自己吸收到的经验排序来给出链接:

https://www.bilibili.com/video/BV1V7411n7CM?p=6&vd_source=3ae03810e5d4ba6e6b93c5edd6c76912

https://www.bilibili.com/video/BV1oQ4y1n7sx/?spm_id_from=333.788&vd_source=3ae03810e5d4ba6e6b93c5edd6c76912

https://blog.csdn.net/weixin_47520540/article/details/133614562#:~:text=%E8%99%9A%E6%8B%9F%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E7%9A%84%E5%8C%BA%E5%88%AB.

(venv,conda,pipenv,poetry)https://blog.csdn.net/qq_42951560/article/details/124224972

https://www.cnblogs.com/doublexi/p/15791048.html

(anaconda同样是一个不错的管理工具,推荐miniconda,anaconda比较臃肿,如果不是做科学计算等相关工作,用miniconda即可)https://www.cnblogs.com/doublexi/p/15796263.html

https://blog.csdn.net/ming12131342/article/details/140233867

(pipx好像也不错)https://blog.csdn.net/pythondh1/article/details/139227698

介绍venv

什么是环境

image-20240927100237926

这是一个python环境(主要的结构),而虚拟环境就是一个环境的副本,如果每次复制都要连标准库和第三方的库一起复制一次的话,会导致内存资源的浪费,且不方便管理,所以创建一个虚环境,只包含你(开始是有个默认的pip)的第三方库。而且是不包含标准库的,直接引用原来的标准库就行,然后在虚环境有自带的pip.exe直接执行就行,不用指定路径什么的。

下面来举个例子用一下试试,打开命令行

C:\>python -m venv -h
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip]
[--prompt PROMPT] [--upgrade-deps]
ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
ENV_DIR A directory to create the environment in.

optional arguments:
-h, --help show this help message and exit
--system-site-packages
Give the virtual environment access to the system site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks are not the default for the platform.
--copies Try to use copies rather than symlinks, even when symlinks are the default for the platform.
--clear Delete the contents of the environment directory if it already exists, before environment
creation.
--upgrade Upgrade the environment directory to use this version of Python, assuming Python has been
upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default)
--prompt PROMPT Provides an alternative prompt prefix for this environment.
--upgrade-deps Upgrade core dependencies: pip setuptools to the latest version in PyPI

Once an environment has been created, you may wish to activate it, e.g. by sourcing an activate script in its bin
directory.

绝大多数情况下是默认参数即可,偶尔用到system-site-packages,因为有时候你需要用到原来的第三方库,再安装可能太费时了,用这个参数就能引用原来的库了,下面我们来创建一个虚环境试试

python -m venv venvdemo

venvdemo是我创建的环境的文件夹的名字(在大多数工具自己生成的环境目录 ,都是隐藏文件夹也就是带个点的如:.venv)。

image-20240927105303844

创建完成后我们可以看到生成了这些文件,下面来看看这些文件的内容

Include空的

Lib:只有一个site-packages,然后里面是一些默认的安装工具

image-20240927105650070

Scripts:有启动虚环境的脚本(多版本,linux的,cmd的,powershell的),还有默认的python和pip的执行文件

image-20240927105747811

使用

因为是在cmd窗口,我们执行bat文件,进入虚环境

image-20240927110057945

使用和平时的python没区别,主要是环境路径的不同,我们可以打印下路径看看

image-20240927111041062

和原来的差别就是第三方库的位置变了,也就是改变环境变量

image-20240927111323864

可以看到path中多了一个venvdemo的环境,而且在最前面,就相当于截胡了原来的环境

必要性

以安装django为例,先在虚环境安装一下

pip install django 

image-20240927111916930

可以看到安装django不只安装了一个包,这些包之间都是存在版本依赖的,改变了可能导致django用不了,各种冲突报错。而且我们只是安装了django其它的是附带的,到时候排错也不好排,而且卸载django时,他是只卸载django,其它的他是不会卸载的,这是我们安装django后的pip list

image-20240927112228116

给它卸载了再看看

image-20240927112247515

可以看到除了django其它都在,所以如果不控制包的范围,到时候管理起来特别麻烦。

保存和复制

这里也就是requirement.txt的来源,pip有个参数叫freeze,它会把我们安装的包按要求的格式输出,也就是我们正常pip install一个包时指定的格式

image-20240927120453979

这时把它重定向到我们的requirement.txt就形成了我们平时在python项目中看到的那个requirement.txt

pip freeze > requirement.txt //命名没要求的,只是管理是命名成requirement.txt

image-20240927120609267

pycharm

其实,不一定要进入虚环境才能做到包管理,在Scripts目录下使用命令也是一样的效果,因为执行路径是优先从当前目录寻找的,所以在pycharm中,我们新建一个项目,要找一个已有的环境,可以按如下操作

image-20240927112850744

这里你把python.exe文件定下来它整个虚拟环境的位置就定下来了,再介绍其它的参数

image-20240927120150563

vscode+python

在vscode中我们可以下载俩个插件

image-20240927090115562

第一个是语法检查,第二个是python环境的支持等,安装完后,我们可以在右下角选择python主文件,有时候打开一个工具,它会自动生成虚环境,并询问是否要下载requirement.txt的包,这一点是非常方便的。

image-20240927093030048