首页 > 编程语言 >Python-venv-创建和管理虚拟环境

Python-venv-创建和管理虚拟环境

时间:2023-04-09 13:56:07浏览次数:47  
标签:venv Python python 虚拟环境 env pip requests tutorial

Python-venv-创建和管理虚拟环境

https://docs.python.org/3/tutorial/venv.html

概述

Python应用程序通常会使用不在标准库内的软件包和模块。应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者可以使用库的过时版本的接口编写应用程序。

这意味着一个Python安装可能无法满足每个应用程序的要求。如果应用程序A需要特定模块的1.0版本但应用程序B需要2.0版本,则需求存在冲突,安装版本1.0或2.0将导致某一个应用程序无法运行。

这个问题的解决方案是创建一个 virtual environment,一个目录树,其中安装有特定Python版本,以及许多其他包。

然后,不同的应用将可以使用不同的虚拟环境。 要解决先前需求相冲突的例子,应用程序 A 可以拥有自己的 安装了 1.0 版本的虚拟环境,而应用程序 B 则拥有安装了 2.0 版本的另一个虚拟环境。 如果应用程序 B 要求将某个库升级到 3.0 版本,也不会影响应用程序 A 的环境。

创建虚拟环境

用于创建和管理虚拟环境的模块称为 venvvenv 通常会安装你可用的最新版本的 Python。如果您的系统上有多个版本的 Python,您可以通过运行 python3 或您想要的任何版本来选择特定的Python版本。

要创建虚拟环境,请确定要放置它的目录,并将 venv 模块作为脚本运行目录路径:python -m venv tutorial-env
这将创建 tutorial-env 目录,如果它不存在的话,并在其中创建包含 Python 解释器副本和各种支持文件的目录。

虚拟环境的常用目录位置是 .venv。 这个名称通常会令该目录在你的终端中保持隐藏,从而避免需要对所在目录进行额外解释的一般名称。 它还能防止与某些工具所支持的 .env 环境变量定义文件发生冲突。

创建虚拟环境后,您可以激活它。

  • 在Windows上,运行:tutorial-env\Scripts\activate.bat
  • 在Unix或MacOS上,运行:source tutorial-env/bin/activate(这个脚本是为bash shell编写的。如果你使用 cshfish shell,你应该改用 activate.cshactivate.fish 脚本。)

激活虚拟环境将改变你所用终端的提示符,以显示你正在使用的虚拟环境,并修改环境以使 python 命令所运行的将是已安装的特定 Python 版本。 例如:

$ source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May  6 2016, 10:59:36)
  ...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python35.zip', ...,
'~/envs/tutorial-env/lib/python3.5/site-packages']
>>>

To deactivate a virtual environment,type:deactivateinto the terminal.

使用pip管理包

You can install, upgrade, and remove packages using a program called pip. By default pip will install packages from the Python Package Index. You can browse the Python Package Index by going to it in your web browser.

pip 有许多子命令: "install", "uninstall", "freeze" 等等。 (请在 安装 Python 模块 指南页查看完整的 pip 文档。)

您可以通过指定包的名称来安装最新版本的包:

(tutorial-env) $ python -m pip install novas
Collecting novas
  Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
  Running setup.py install for novas
Successfully installed novas-3.1.1.3

您还可以通过提供包名称后跟 == 和版本号来安装特定版本的包:

(tutorial-env) $ python -m pip install requests==2.6.0
Collecting requests==2.6.0
  Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0

If you re-run this command, pip will notice that the requested version is already installed and do nothing. You can supply a different version number to get that version, or you can run python -m pip install --upgrade to upgrade the package to the latest version:

(tutorial-env) $ python -m pip install --upgrade requests
Collecting requests
Installing collected packages: requests
  Found existing installation: requests 2.6.0
    Uninstalling requests-2.6.0:
      Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0

python -m pip uninstall followed by one or more package names will remove the packages from the virtual environment.

python -m pip show will display information about a particular package:

(tutorial-env) $ python -m pip show requests
---
Metadata-Version: 2.0
Name: requests
Version: 2.7.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: [email protected]
License: Apache 2.0
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
Requires:

python -m pip list will display all of the packages installed in the virtual environment:

(tutorial-env) $ python -m pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)

python -m pip freeze will produce a similar list of the installed packages, but the output uses the format that python -m pip install expects. A common convention is to put this list in a requirements.txt file:

(tutorial-env) $ python -m pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

然后可以将 requirements.txt 提交给版本控制并作为应用程序的一部分提供。然后用户可以使用 install -r安装所有必需的包:

(tutorial-env) $ python -m pip install -r requirements.txt
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
  ...
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
  ...
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
  ...
Installing collected packages: novas, numpy, requests
  Running setup.py install for novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

pip 有更多选择。有关 pip 的完整文档,请参阅 安装 Python 模块 指南。当您编写一个包并希望在 Python 包索引中使它可用时,请参考 分发 Python 模块 指南。

标签:venv,Python,python,虚拟环境,env,pip,requests,tutorial
From: https://www.cnblogs.com/yongchao/p/17300237.html

相关文章

  • Python中的抽象基类介绍
        从上一篇文章(Python中鸭子类型与多态介绍)中,我们了解到了python的鸭子类型和多态。继承提供了多态的基础,而多态则使继承更加灵活和强大。那么今天,让我们再来一起了解一下python里面的抽象基类。Python中的抽象基类(AbstractBaseClasses,简称abc)是一种特殊的类,它用于定义一......
  • Python 安装 pyenv
    安装安装到指定位置方便管理pipinstallpyenv-win-i"D:\ProgramFiles\pyenv"配置环境变量查找安装的版本这里我们安装3.11为示例安装后我们在下面的versions目录可以找到Python的程序文件......
  • 【Python】python中的argparse包在解析bool型参数时的细节问题
    1.参数定义定义了如下三个参数,其中use_entity_type和use_entity_id是bool参数。这两个bool型参数的默认值都是True。2.命令行传参这里是vscode中的launch.json文件中的参数定义,想把下面的两个参数修改成False。3.运行过程运行代码,但是发现经过parser.parse_args()之后,参数u......
  • python—numpy库学习
    importnumpyasnparr=np.array([1,2,3])print(arr.shape)print(arr)arr=np.arange(10)print(arr.shape)print(arr)#(开始,截止,步长)#[开始,截止)范围内步长为间隔的更新arr=np.arange(0,10,2.5)print(arr.shape)print(arr)#在一个区间内返回等间距数组arr=np.linspac......
  • Code-C++ Invoke Python
    Code-C++InvokePythonhttps://www.cnblogs.com/yongchao/p/17299892.html使用C或C++扩展Python扩展和嵌入Python解释器Python3.10.11Python/CAPI参考手册Python3.11.3Python/CAPI参考手册https://www.cnblogs.com/lidabo/p/17043302.htmlhttps://bl......
  • OS-Linux-Ubuntu22.04x64-Python-C++调用Python缺少Python.h
    OS-Linux-Ubuntu22.04x64-Python-C++调用Python缺少Python.h使用C或C++扩展Python扩展和嵌入Python解释器Python3.10.11Python/CAPI参考手册Python3.11.3Python/CAPI参考手册参考https://www.cnblogs.com/lidabo/p/17043302.htmlhttps://blog.csdn.net/z......
  • python 十六进制转换字符
    a=b'\xE6\x88\x91\xE6\x98\xAF\xE8\xAF\xB7\xE6\xB1\x82'print(a.decode('utf-8').encode('utf-8'))print(a.decode('gbk').encode('gbk'))print(a.decode(encoding="utf-8",errors="strict......
  • Python的time库
    time库为Python标准库,常用来处理和转换时间。官方文档:time---时间的访问和转换—Python3.11.2文档time.timetime.time()返回一个float类型的时间戳。如下:什么是时间戳?时间戳是自世界协调时(UTC)起始时间1970年1月1日0时0分0秒,即北京时间(UTC+8)1970年1月1日8时0分0秒......
  • Python中将CSV文件转换为H5AD文件
    将CSV文件转换为H5AD文件,可以按照以下步骤进行:使用pandas库将CSV文件读入到Python中,并将其转换为dataframe格式: importpandasaspddf=pd.read_csv('data.csv')使用anndata库将dataframe转换为AnnData对象: importanndataasadadata=ad.AnnData(df)将......
  • python合并多个PPT文件到 文档\演示文稿1.pptx
    #encoding=utf8#-*-coding:utf-8-*-#pipinstallpython-pptx-ihttps://pypi.tuna.tsinghua.edu.cn/simple#pipinstallpython-docx-ihttps://pypi.tuna.tsinghua.edu.cn/simple#pipinstallpandas-ihttps://pypi.tuna.tsinghua.edu.cn/simple#pipinstall......