python虚拟环境模块venv使用

  1. 查看命令
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.
  1. 创建虚拟环境(假设名字为py3)
python -m venv py3

在当前路径下创建名为py3的虚拟环境,可以看到出现了名为py3的新文件夹,存储了虚拟环境的文件,删除环境时直接删除文件夹。

Tips:
如果提示

The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.8-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/root/py3/bin/python', '-Im', 'ensurepip', '--upgrade', '--default-pip']

根据提示先安装 python3.8-venv

apt install python3.8-venv -y
  1. 激活环境
# linux
source py3/bin/activate
# win
cd py3/Scripts
activate 或 activate.bat
  1. 退出环境
# linux
deactivate
# win
deactivate 或 deactivate.bat

你可能感兴趣的:(record,python,linux,开发语言)