当前位置: 首页 > 科技观察

Python虚拟环境管理库-Poetry简介

时间:2023-03-18 22:24:07 科技观察

Poetry是Python中的一个依赖管理和打包工具,可以让你声明你的项目依赖的库,并为你管理。和Pipev相比,我觉得诗歌更清爽,展示也更友好。虽然我们一般不用它的打包发布,但是其他的虚拟环境管理也很方便。安装pipinstallpoetry在控制台执行poetry-V命令查看版本升级poetryselfupdatepoetry配置在使用poetry之前我们需要做一些设置,主要是调整虚拟环境设置项cache-dirType:string缓存目录配置的安装位置,使用poetry安装的包,所有源文件都缓存在这个目录下。以下是系统默认目录:macOS:~/Library/Caches/pypoetryWindows:C:\Users\AppData\Local\pypoetry\CacheUnix:~/.cache/pypoetryinstaller.parallelType:boolean这个配置会被virtualenvs忽略。createType:boolean默认为真。如果当前项目的虚拟环境不存在,则创建一个virtualenvs.in-projectType:booleanNone:poetry会在系统特定的目录下创建一个.venv目录,通过下面的路径参数指定true:poetry会在在项目根目录下创建.venv目录。我推荐这种方法在项目的根目录下创建一个虚拟环境,这样即使移动目录位置也不会影响虚拟环境的使用。false:poetry会忽略Pipev中已有的.venv目录如果要在当前项目的根目录下安装虚拟环境,需要配置环境变量PIPENV_VENV_IN_PROJECTvirtualenvs.pathType:string默认为{cache-dir}/virtualenvs,虚拟环境创建的目录。如果上面的in-project为真,则此配置Forinvalidconfiguration命令查看所有配置poetryconfig--listquerysingleconfigurationpoetryconfigvirtualenvs.pathaddorupdateconfigurationpoetryconfigvirtualenvs.in-projecttruedeleteconfigurationpoetryconfigvirtualenvs.path--unsetbasicuseinitializationprojectcreatenewprojectpoetrynewpoetry-demoexistingprojectcdpre-existing-projectpoetryinit创建成功后,会得到如下目录:poetry-demo-├──pyproject.toml├──README.rst├──poetry_demo│└──__init__.py└──tests├──__init__.py└────test_poetry_demo.pypyproject.toml将是一个非常重要的文件,包括项目配置和依赖库信息,其初始内容如下:[tool.poetry]name="poetry-demo"version="0.1.0"description=""authors=["xingxingzaixian"][tool.poetry.dependencies]python="^3.7"[tool.poetry.dev-dependencies]pytest="^5.2"[build-system]requires=["poetry-core>=1.0.0"]build-backend="poetry.core.masonry.api"指定虚拟环境的pypi源[[tool.poetry.source]]name="custom"url="https://pypi.tuna.tsinghua.edu.cn/simple"default=truepyproject.toml文件配置Introducetool.poetryname项目名称,version项目版本号必填,默认0.1.0,description项目描述必填,license开源协议作者必填,这是一个列表,至少要有一个作者信息包含,maintainers维护是强制性的ReadmeREADME文件首页项目首页地址基本命令new#创建新项目poetrynewprojectinit#初始化现有项目poetryinitproject可选选项:--name:名称。--描述:描述。--作者:作者。--python:Python版本。--dependency:使用版本约束的包。应采用foo:1.0.0格式。--dev-dependency:开发需求installpoetryinstallsstall命令从当前项目中读取pyproject.toml文件并安装它们。如果当前目录中有poetry.lock文件,它将使用其中的确切版本而不是解析它们。这确保每个使用该库的人都将获得相同版本的依赖项。如果没有poetry.lock文件,poetry会在依赖解析后创建一个。update升级安装包addpoetryaddrequests#==>pipinstallrequestspoetryaddrequests@^2.20.0#安装大于2.20.0版本的包poetryadd"requests=2.20.0"#==>pipinstallrequests==2.20.0poetryadd"uvicorn[standard]"#==>pipinstalluvicorn[标准]#从git存储库安装poetryaddgit+https://github.com/sdispater/pendulum.gitpoetryaddgit+ssh://git@github.com/sdispater/pendulum.gitpoetryaddgit+https://github.com/sdispater/pendulum.git#developpoetryaddgit+https://github.com/sdispater/pendulum.git#2.0.5#从本地文件安装poetryadd./my-package/poetryadd../my-package/dist/my-package-0.1.0.tar.gzpoetryadd../my-package/dist/my_package-0.1.0.whl安装依赖库并添加到pyproject.toml中,如果没有指定版本约束,安装最新版本removepoetryremovedeletealreadyinstalledDependencypackageshow#显示安装包及版本号poetryshow#显示非开发环境poeter的安装包yshow--no-dev#显示依赖包的层级关系poetryshow--tree#显示已安装包的当前版本和最新版本poetryshow-l#显示可更新包poetryshow-orun这个命令也是比较重要的命令,这使得我们可以在不进入虚拟环境的情况下在虚拟环境中执行命令#查询虚拟环境中的Python版本poetryrunpython-V#在虚拟环境中执行脚本poetryrunpythontest.py除了可以直接执行python命令外,我们还可以配置自定义脚本命令,在py在project.toml文件中配置[tool.poetry.scripts],如配置:[tool.poetry.scripts]test="pythontest.py"serve="pythonrun.py"可以执行如下命令:poetryrunserveshell#输入虚拟环境下的poetryshellcheck#查看pyproject.toml配置是否正确使用指定环境pythonpoetryenvuse/full/path/to/python#如果python在环境变量中,可以使用如下方法指定poetryenvusepython3.7#显示当前虚拟环境信息poetryenvinfo#只显示虚拟环境的路径poetryenvinfo--path#显示当前项目poet的所有虚拟环境列表poetryenvlist#显示当前项目poet的虚拟环境的绝对路径ryenvlist--full-path#删除虚拟环境poetryenvremove/full/path/to/pythonpoetryenvremovepython3.7cache#查看缓存列表poetrycachelist#清除指定缓存poetrycacheclear#清除所有缓存poetrycacheclear--所有依赖的版本关系poetry中支持packages类似于webpack中的版本关系,比如^2.3.0,~2.3.0,对应关系是什么?