DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Package and Environment Management of Python and Ruby


Python

Python 通过一个 package 的 setup.py 中 entry_points 参数确定发布的是一个 library 还是 application。使用 poetry 不用手工编写 setup.py, 只在配置文件(pyproject.toml)中声明就能完成从创建脚手架、运行测试、打包、发布的完整流程。

执行 poetry build 会自动在生成的发布包里创建 setup.py 文件和 entry_points 参数参数定义(见下面一节介绍):

[tool.poetry.scripts]
dsnote = "dsnote.app:main"

本地 pakcage 的创建、开发、测试和发布流程:

poetry new dsnote
cd dsnote
# edit dsnote/app.py
# add dependency list into pyproject.toml
poetry install
# add entry_point definition above into pyproject.toml
poetry run pytest tests  # run unit test based on pytest
poetry run dsnote        # verify functions of the app
poetry version           # bump version
poetry build
poetry publish

poetry build 在 dist 目录下生成两个文件,一个 .whl 安装包(实际上是 zip 文件, 可以用 unzip -l 查看,针对不同平台生成不同的文件),以及一个 .tar.gz 源码包。

完整使用方法见 Poetry 手册 Commands。

本地安装流程:

将上面 build 生成的 .whl 文件拷贝到要安装的主机上, 目标主机的 Python 环境中要有 wheel 包,以保证 pip 能安装 .whl 文件, 然后执行 pip install dsnote-0.1.1-py3-none-any.whl, 这样就可以在当前 Python 环境里执行 dsnote 命令了。 用 pip uninstall dsnote 卸载这个包。

entry_points 定义格式

按照 Use for scripts 的说法:

Two groups of entry points have special significance in packaging: console_scripts and gui_scripts. In both groups, the name of the entry point should be usable as a command in a system shell after the package is installed.

Explain Python entry points 给出了 cursive 命令的定义方法:

setup(
...,
entry_points={
    'console_scripts': [
        'cursive = cursive.tools.cmd:cursive_command',
    ],
})

Python 的 package 是一种特殊类型的 module,而 module 是一个 .py 文件, 或者一个目录下的 init.py 文件,见 Python 官方文档中对 package 的说明:

It’s important to keep in mind that all packages are modules, but not all modules are packages. Or put another way, packages are just a special kind of module. Specifically, any module that contains a path attribute is considered a package.

Ruby

bundler 是 poetry 在 Ruby 世界的对等工具。



Published

Dec 10, 2019

Last Updated

Dec 16, 2019

Category

Tech

Tags

  • bundler 2
  • environment 2
  • package 28
  • poetry 2
  • python 136
  • ruby 9

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor