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
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中创建虚拟环境的方法是使用`venv`模块。从Python3.3版本开始,标准库中就内置了`venv`模块,可以用它来创建虚拟环境,完全替代了第三方包`virtualenv`。可以按照以下步骤创建虚拟环境: 1. 在命令行中进入项目所在的目录。 2. 运行以下命令来创建虚拟环境: ``` python3 -m venv myenv ``` 这将在当前目录下创建一个名为`myenv`的虚拟环境。 3. 激活虚拟环境,可以运行以下命令: - 在Windows系统中: ``` myenv\Scripts\activate ``` - 在macOS和Linux系统中: ``` source myenv/bin/activate ``` 激活虚拟环境后,命令行提示符会显示虚拟环境的名称。 4. 在激活的虚拟环境中,可以安装和管理项目所需的Python包,而不会影响到系统环境或其他虚拟环境。 5. 当你完成工作后,可以使用以下命令退出虚拟环境: ``` deactivate ``` 这将恢复到系统环境。 引用提供了关于使用`venv`模块创建虚拟环境的信息。希望这对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Python】创建虚拟环境的四种方式(venv | pipenv | conda | poetry)](https://blog.csdn.net/qq_42951560/article/details/124224972)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Python - 虚拟环境 venv](https://blog.csdn.net/qq_33801641/article/details/120300551)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值