{{format('0')}} {{format('414')}} {{format('3199')}}

【PY模块】poetry 虚拟环境管理器 [ Python极客 ]

大数据男孩 文章 正文

想做一个技术博客,奈何实力不够
分享

明妃

{{nature("2022-08-14 17:23:20")}}更新

说明

poetry是一个Python虚拟环境依赖管理的工具,oetrypipenv类似,另外还提供了打包发布的功能。

安装

安装不成功,大多数是网站被墙了

macOS & Linux

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -

windows

powershell 使用该命令

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -

[mark]()

自动添加环境变量,需要重新 打开 powershell ,查看是否安装成功

[mark]()

[mark]()

配置

使用前修改这些配置后,更好用

查看当前配置

poetry config --list

-------------------
PS C:\Users\ASUS> poetry config --list
cache-dir = "D:\\Python\\peotry_env"
experimental.new-installer = true
installer.parallel = true
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.path = "{cache-dir}\\virtualenvs"  # D:\Python\peotry_env\virtualenvs

[mark]()

修改配置

poetry config cache-dir D:\\Python\\peotry_env

poetry config virtualenvs.in-project true

创建项目

该命令,会建立一个目录结构

poetry new demo

结构

demo
├── pyproject.toml
├── README.rst
├── demo
│   └── __init__.py
└── tests
    ├── __init__.py
    └── test_demo.py

其中 pyproject.toml 文件将协调项目及其依赖项

[tool.poetry]
name = "demo"
version = "0.1.0"
description = ""
authors = ["‘zbigdataboy‘ <876545500@qq.com>"]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

初始化项目

这是在 项目已经存在的情况下使用,为了创建 pyproject.toml 管理项目

poetry init

[mark]()

添加依赖 & 库

如果项目不存在 虚拟环境,将会创建,同时还会 创建 poetry.lock ,用以记录项目依赖的版本

poetry add requests

[mark]()

卸载依赖

poetry remove requests

[mark]()

安装依赖项

用以使用项目时,依赖于开发时一样

poetry install

修改 依赖安装源

项目的 pyproject.toml 文末追加

[[tool.poetry.source]]
name = "tsinghua"
url = "https://pypi.tuna.tsinghua.edu.cn/simple"

虚拟环境执行项目

poetry 会自动在相应目录寻找该项目的环境

poetry run python app.py

显示的激活

poetry shell

其他命令

# 显示当前环境信息
poetry env info

# 列出与项目关联的环境(一个项目可能需要不同版本依赖做测试)
poetry env list

# 列出当前配置
poetry config --list

导出 requirements.txt 文件

暂时只支持 requirements.txt 格式

# 导出 requirements.txt 文件  -f 导出格式 --output 导出文件名
poetry export -f requirements.txt --output requirements.txt
评论 0
0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}
TOP 2
Spark 2.0 单机模式与集群模式 安装

{{nature('2020-01-02 16:47:07')}} {{format('12522')}}人已阅读

TOP 3
Office 2016 Pro Plus 激活

{{nature('2019-12-11 20:43:10')}} {{format('9410')}}人已阅读

TOP 4
Linux上 MySQL 开启远程登陆的两种方法

{{nature('2019-12-26 17:20:52')}} {{format('7405')}}人已阅读

TOP 5
Linux 安装 MySQL 5.7

{{nature('2019-12-26 16:03:55')}} {{format('4880')}}人已阅读

目录

标签云

Python 包管理 模块

一言

# {{hitokoto.data.from || '来自'}} #
{{hitokoto.data.hitokoto || '内容'}}
作者:{{hitokoto.data.from_who || '作者'}}
自定义UI
配色方案

侧边栏