【Python】Python3.7.3 - 虚拟环境:pyvenv过时;使用python -m venv命令

版权声明:转载必须保留原出处,没有书面许可不可用于商用目的, https://blog.csdn.net/qq_43401808/article/details/89632033

系统参考

[tony@tony-controller bin]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

[tony@tony-controller bin]$ uname -a
Linux tony-controller 3.10.0-957.10.1.el7.x86_64 #1 SMP Mon Mar 18 15:06:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

[tony@tony-controller bin]$ pwd
/home/tony/python37/bin

[tony@tony-controller bin]$ ./python3 --version
Python 3.7.3

pyvenv脚本过时说明

尽管Python 3.7.3的bin目录下依然有pyvenv的脚本,但是在打印的help信息的第一行明确警告说:这个脚本是过时的,推荐使用 python3.7 -m venv命令。

注:主要原因是避免混淆新创建的虚拟环境到底用的是哪个Python解释器,使用python3.7 -m venv这个格式可以明确的指导使用的是当前调用的解释器。
注:PEP 405对Python的虚拟环境有详细说明。

[tony@tony-controller bin]$ ./pyvenv --help
WARNING: the pyenv script is deprecated in favour of `python3.7 -m venv`
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.

python -m venv 创建新的虚拟环境

这个命令的选项与pyvenv的选项一样,可以一次性同时创建多个虚拟环境。

# 创建两个虚拟环境
[tony@tony-controller bin]$ ./python3 -m venv /tmp/venv1_py37 /tmp/venv2_py37

# 查看虚拟环境目录
[tony@tony-controller bin]$ ls -l /tmp/venv*
/tmp/venv1_py37:
total 4
drwxrwxr-x. 2 tony tony 173 Apr 28 09:43 bin
drwxrwxr-x. 2 tony tony   6 Apr 28 09:43 include
drwxrwxr-x. 3 tony tony  23 Apr 28 09:43 lib
lrwxrwxrwx. 1 tony tony   3 Apr 28 09:43 lib64 -> lib
-rw-rw-r--. 1 tony tony  84 Apr 28 09:43 pyvenv.cfg

/tmp/venv2_py37:
total 4
drwxrwxr-x. 2 tony tony 173 Apr 28 09:43 bin
drwxrwxr-x. 2 tony tony   6 Apr 28 09:43 include
drwxrwxr-x. 3 tony tony  23 Apr 28 09:43 lib
lrwxrwxrwx. 1 tony tony   3 Apr 28 09:43 lib64 -> lib
-rw-rw-r--. 1 tony tony  84 Apr 28 09:43 pyvenv.cfg

# 选项帮助信息
[tony@tony-controller bin]$ ./python3 -m venv --help
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.

猜你喜欢

转载自blog.csdn.net/qq_43401808/article/details/89632033