如何python打包自己的分发包并安装之poetry入门指南

python打包自己的分发包并安装使用,可以发布自己写的脚本
更新于: 2022-06-20 09:55:03

选择工具:poetry

理由:感觉会是未来流行的方式,与前端 npm 类似。

Token生成

安装工具:安装文档

poetry 安装截图

1.1 用 shell 来安装

# 安装
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
# 让当前 shell 生效
source $HOME/.poetry/env
# 检测
poetry --version

1.2 用 pip 安装

pip install --user poetry

1.2.1 pip安装慢可以编辑  .pypirc

# 常见的源:
tsinghua :https://pypi.tuna.tsinghua.edu.cn/simple
Alibaba cloud :http://mirrors.aliyun.com/pypi/simple/
University of science and technology of China : https://pypi.mirrors.ustc.edu.cn/simple/
douban :http://pypi.douban.com/simple/
[global]
  index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
  trusted-host = mirrors.aliyun.com

1.2.2 或者用 brew 安装

brew install poetry

1.3 升级你的 poetry

poetry self update --preview

2.1 基本用法-针对新项目

poetry new poetry-demo
.
├── README.rst
├── poetry_demo
│   └── __init__.py
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_poetry_demo.py

2.1 基本用法-针对已经存在的项目

cd pre-existing-project
poetry init

2.2 添加组件(第3方的包)

添加包的截图
# 安装到 deps
poetry add pendulum
# 安装到 dev-deps
poetry add -D pytest # -D for development dependency

# 添加包的各种方式
poetry add requests  # ==> pip install requests 
poetry add requests@^2.20.0 # 安装大于2.20.0版本的包 
poetry add "requests=2.20.0" # ==> pip install requests==2.20.0 
poetry add "uvicorn[standard]" # ==> pip install uvicorn[standard] 
 
# 从 git 仓库安装 
poetry add git+https://github.com/sdispater/pendulum.git 
poetry add git+ssh://git@github.com/sdispater/pendulum.git 
poetry add git+https://github.com/sdispater/pendulum.git#develop 
poetry add git+https://github.com/sdispater/pendulum.git#2.0.5 
 
# 从本地文件安装 
poetry add ./my-package/ 
poetry add ../my-package/dist/my-package-0.1.0.tar.gz 
poetry add ../my-package/dist/my_package-0.1.0.whl 

2.3 运行一个 scrip,如测试

poetry run pytest
poetry run python your_script.py

2.4 安装 pyproject.toml 中写好的依赖

poetry install

2.5 打包/发布

# 打包
poetry build
# 发布到 PyPI
poetry publish
# 发布到其它 repositry
poetry publish -r my-repository

3 常用命令集合

poetry new
poetry init
poetry add
poetry show
poetry install
poetry build
poetry publish
poetry update
poetry remove
poetry config --list
poetry run python -V
poetry search requests pendulum
poetry check
poetry version
poetry cache list
poetry export -f requirements.txt --output requirements.txt

4.1 全局配置

MacOS: 位置在这里 ~/Library/Application Support/pypoetry
默认文件: ~/.config/pypoetry
# 列出所有配置
poetry config --list
# 列出单个配置
poetry config virtualenvs.path

4.2 好奇

这个能不能用 ~/Library/Caches/pypoetry 或者 env.home 之类的环境参数 

cache-dir = "/Users/alo7.aric/Library/Caches/pypoetry"
experimental.new-installer = true
installer.parallel = true
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.path = "{cache-dir}/virtualenvs"  # /Users/alo7.aric/Library/Caches/pypoetry/virtualenvs

5. 安装包慢的时候,可以添加源

# 添加清华镜像
[[tool.poetry.source]]
name = "tsinghua"
url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/"

6. 本地开发调试

poetry install
# 类似于
pip install -e .

参考