python打包和分发

setuptools打包

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

distutils虽然在python的标准库中,但是已经停止开发了,setuptools提供向distutils的兼容,并且有一些有用的,有效率的命令。

常用的setuptools打包命令有:

  • 源码打包(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. 使用distutils

    例子:

    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. 使用setuptools

    例子:

    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_point

使用pbr打包

pbr在setuptools的基础上做了一些改进:

  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版)

转载于:https://my.oschina.net/hgfdoing/blog/514639

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值