Packaging python packages with Poetry

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Install

  1. Install with windows powershell

    1
    PS C:\Users> (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
    Retrieving Poetry metadata
    
    This installer is deprecated. Poetry versions installed using this script will not be able to use 'self update' command to upgrade to 1.2.0a1 or later.
    # Welcome to Poetry!
    
    This will download and install the latest version of Poetry,
    a dependency and package manager for Python.
    
    It will add the `poetry` command to Poetry's bin directory, located at:
    
    %USERPROFILE%\.poetry\bin
    
    This path will then be added to your `PATH` environment variable by
    modifying the `HKEY_CURRENT_USER/Environment/PATH` registry key.
    
    You can uninstall at any time by executing this script with the --uninstall option,
    and these changes will be reverted.
    
    Installing version: 1.1.10
      - Downloading poetry-1.1.10-win32.tar.gz (41.47MB)
      
      
    Poetry (1.1.10) is installed now. Great!
    
    To get started you need Poetry's bin directory (%USERPROFILE%\.poetry\bin) in your `PATH`
    environment variable. Future applications will automatically have the
    correct environment, but you may need to restart your current shell.
  2. Verify

    1
    PS C:\Users> poetry --version
    Poetry version 1.1.10

Basic Usage

  1. Create a new project

    1
    PS E:\> poetry new jenkins-tool
    Created package jenkins_tool in jenkins-tool
    PS E:\> tree .\jenkins-tool\ /F
    文件夹 PATH 列表
    卷序列号为 4428-676B
    E:\JENKINS-TOOL
    │  pyproject.toml
    │  README.rst
    │
    ├─jenkins_tool
    │      __init__.py
    │
    └─tests
            test_jenkins_tool.py
            __init__.py
  1. Copy script to jenkins_tool/jenkins_tool

  2. Update pyproject.toml

    1
    [tool.poetry]
    name = "jenkins-tool"
    version = "0.1.0"
    description = "Manage Jenkins Job"
    authors = ["yongfeiuall <yongfeiuall@163.com>"]
    
    [tool.poetry.dependencies]
    python = "^3.6"
    python-jenkins="^1.7.0"
    
    [tool.poetry.dev-dependencies]
    
    [build-system]
    requires = ["poetry-core>=1.0.0"]
    build-backend = "poetry.core.masonry.api"
    
    [tool.poetry.scripts]
    jt = "jenkins_tool.client:main"
  3. Build

    1
    PS E:\jenkins-tool> poetry build
    Creating virtualenv jenkins-tool-vo3-Lls6-py3.6 in C:\Users\AppData\Local\pypoetry\Cache\virtualenvs
    Building jenkins-tool (0.1.0)
      - Building sdist
      - Built jenkins-tool-0.1.0.tar.gz
      - Building wheel
      - Built jenkins_tool-0.1.0-py3-none-any.whl

Verification

  1. Unzip jenkins-tool-0.1.0.tar.gz

  2. Install PS E:\jenkins-tool\dist\jenkins-tool-0.1.0> python .\setup.py install

  3. Verify

    1
    PS E:\jenkins-tool\dist\jenkins-tool-0.1.0> jt -h
    usage: jt [-h] [-v] [-V VIEW_NAME]
    
    Manage Jenkins Job
    
    optional arguments:
      -h, --help            show this help message and exit
      -v, --version         Show version
      -V VIEW_NAME, --view VIEW_NAME
                            Get Jenkins job last build number by view
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!