Python基础-02-Python虚拟环境的使用

Python的原始安装环境

image-20220618160255046

所谓虚拟环境就是将原始的环境拷贝形成的环境

image-20220618160428088

虚拟环境的Lib文件一般只包含你未来将要存放第三方库的文件夹,默认不包含标准库,但是在创建虚拟环境的时候可以选择包含标准库

把pip.exe和python.exe放在同一个文件夹的目的是为了减少多次添加环境变量,放在同一个文件夹时只需要添加一个环境变量即可

C:\Users\chuan>python -m venv -h
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip]
            [--prompt PROMPT]
            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.

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

C:\Users\chuan>

创建虚拟环境

D:\Python\venv>python -m venv venvdemo1

查看创建虚拟环境后的虚拟环境目录

D:\Python\venv\venvdemo1>dir
 驱动器 D 中的卷是 新加卷
 卷的序列号是 2E46-6483

 D:\Python\venv\venvdemo1 的目录

2022/06/18  16:15    <DIR>          .
2022/06/18  16:15    <DIR>          ..
2022/06/18  16:15    <DIR>          Include
2022/06/18  16:15    <DIR>          Lib
2022/06/18  16:15               118 pyvenv.cfg
2022/06/18  16:15    <DIR>          Scripts
               1 个文件            118 字节
               5 个目录 859,745,579,008 可用字节

D:\Python\venv\venvdemo1>

其中Lib文件夹存放的就是python库,后续我们安装的库也将会存放在这个位置

Scripts存放的就是可执行文件

D:\Python\venv\venvdemo1\Scripts>dir
 驱动器 D 中的卷是 新加卷
 卷的序列号是 2E46-6483

 D:\Python\venv\venvdemo1\Scripts 的目录

2022/06/18  16:15    <DIR>          .
2022/06/18  16:15    <DIR>          ..
2022/06/18  16:15             2,273 activate
2022/06/18  16:15               966 activate.bat
2022/06/18  16:15            19,332 Activate.ps1
2022/06/18  16:15               368 deactivate.bat
2022/06/18  16:15           106,354 pip.exe
2022/06/18  16:15           106,354 pip3.8.exe
2022/06/18  16:15           106,354 pip3.exe
2022/06/18  16:15           537,776 python.exe
2022/06/18  16:15           536,752 pythonw.exe
               9 个文件      1,416,529 字节
               2 个目录 859,745,579,008 可用字节

D:\Python\venv\venvdemo1\Scripts>

快速保存虚拟环境中的到库,并导出到文件中

命令:pip freeze > 你要保存的文件名

(venvdemo1) D:\Python\venv\venvdemo1\Scripts>pip freeze > pip_list.txt

(venvdemo1) D:\Python\venv\venvdemo1\Scripts>

image-20220618162608582

通过pip命令快速安装文件中包含的所有库

命令:pip install -r pip_list.txt

这里我们已经安装过了,所以显示如下:

image-20220618162752401

posted @ 2022-06-18 16:31  七侠镇缁衣铺头  阅读(135)  评论(0编辑  收藏  举报