python打包和分發

setuptools打包

都是在hello目錄下新建setup.py的文件。python

distutils雖然在python的標準庫中,可是已經中止開發了,setuptools提供向distutils的兼容,而且有一些有用的,有效率的命令。linux

經常使用的setuptools打包命令有:git

  • 源碼打包(tar.gz):python setup.py sdist
  • 可執行文件打包
    • windows下使用的格式:python setup.py bdist_wininst
    • linux redhat系列格式:python setup.py bdist_rpm
    • 通用egg格式:python setup.py bdist
    • 通用whell格式:python setup.py bdist_wheel
  1. 使用distutilswindows

    例子:服務器

    from distutils.core import setup
    
    setup(name="hgf",
            version="0.1",
            description="brief introduce",
            author="hgf",
            author_email="hgf@a.com",
            packages=['hello']
    )

    說明:測試

    1. 使用distutils打包python,須要知名的參數有nameauthorversionurl
  2. 使用setuptoolsui

    例子:this

    import setuptools
    
    setuptools.setup(
        name="hello",
        version="0.1",
        author="hgf",
        author_email="hgfgood@gmail.com",
        description="this is a hello test about setuptools",
        license="GPL",
        package=["test"],
        entry_point={
            "consol_script":[
                "sayhello = test.hello:say_hello"
            ]
        }
    )

    說明,能夠不用entry_pointurl

使用pbr打包

pbr在setuptools的基礎上作了一些改進:code

  1. 基於requirements.txt的自動依賴安裝
  2. 利用sphinx實現文檔自動化
  3. 基於git history自動生成 authorsChangeLog
  4. 基於git 自動建立文件列表
  5. 基於git tags的版本管理
  6. 基於setuptools的思想,將配置信息所有放到setup.cfg文件裏面

pbr的打包例子:

setup.py:

import setuptools

setuptools.setup(setup_requires=['pbr'],pbr=True)

setup.cfg

[metadata]
name=hello
author=hgf
author_email=hgfdodo@gmail.com
licens=MIT
description-file=README.rst
requires-python= >=2.6
classifier=
    Development Status :: 4 - Beta
    Environment :: Console
    Intended Audience :: Developers
    Intended Audience :: Information Technology
    license ::OSI Approved :: Apache Software license
    Operating System :: Os Independent
    Programming Lauguage :: python

[files]
packages =
    test

注意:使用pbr打包,源程序必定要使用git,而且源碼的根目錄下必定要有 ** README.rst ** 文件

使用pypi服務器共享包

  1. https://testpypi.python.org/pypi上註冊一個本身的賬號
  2. 在開發主機的~/.pypi中加入做者信息
    [distutils]
    index-server =
        pypi
        testpypi
    
    [pypi]
    username = hgfgood
    password = password
    
    [testpypi]
    username = hgfgood
    password = password
    repository = https://testpypi.python.org/pypi

說明: 1. 設置索引服務器 2. 分別寫索引服務器的用戶名密碼 3. testpypi是測試服務器

  1. 在pypi的索引中註冊本身的項目 python setup.py register -r hello

  2. 上傳分發代碼 python setup.py sdist upload -r hello(tar源碼版) python setup.py bdist_wheel upload -r hello(wheel版)

相關文章
相關標籤/搜索