zoukankan      html  css  js  c++  java
  • python环境下制作pypi软件包

    为了开发方便,将一些公用函数做成软件包来调用

    官网地址: https://packaging.python.org/tutorials/distributing-packages/

    (留一笔,万一以后又忘记了呢) 

    根据 application 包含的代码类型以及其所支持的 python 版本, wheel 格式可细分为三种

    采用如下命令可编译成 universal wheel

    python setup.py bdist_wheel --universal
    

    采用如下命令可编译成非 universal wheel(即 pure python wheel 或 platform wheel):

    python setup.py bdist_wheel

    其它的类型的包 操作命令

    python setup.py bdist_egg # 生成类似 -0.0.1-py2.7.egg,支持 easy_install 
    python setup.py sdist     # 生成类似 -0.0.1.tar.gz,支持 pip 
    python setup.py build     #编译
    python setup.py bdist_wininst # Windows exe
    python setup.py bdist_rpm     # rpm
    

      

    参数说明:(参数请参考官网)

    name -> 为项目名称,和顶层目录名称一致;
    version -> 是项目当前的版本,1.0.0.dev1表示1.0.0版,目前还处于开发阶段
    description -> 是包的简单描述,这个包是做什么的
    long_description -> 这是项目的详细描述,出现在pypi软件的首页上
    url -> 为项目访问地址,我的项目放在github上。
    author -> 为项目开发人员名称
    author_email -> 为项目开发人员联系邮件
    license -> 为本项目遵循的授权许可
    classifiers -> 有很多设置,具体内容可以参考官方文档
    keywords -> 是本项目的关键词,理解为标签
    packages -> 是本项目包含哪些包,使用工具函数自动发现包
    package_data -> 通常包含与包实现相关的文件
    data_files -> 指定其他的一些文件(如配置文件)
    cmdclass -> build或install的时候执行的额外操作
    entry_points -> 可以定义安装该模块后执行的脚本,比如将某个函数作为linux命令
    

      

    ls -R
    .:
    build  dist  LICENSE  MANIFEST.in  README.md  README.rst  setup.cfg  setup.py  src
    
    ./build:
    bdist.linux-x86_64  lib
    
    ./build/bdist.linux-x86_64:
    
    ./build/lib:
    test123
    
    ./build/lib/test123:
    cms  __init__.py  Service.py
    
    ./build/lib/test123/cms:
    Command.py  __init__.py
    
    ./dist:
    test123-0.0.1-py2.py3-none-any.whl
    
    ./src:
    test123  test123.egg-info
    
    ./src/test123:
    cms  __init__.py  __pycache__  Service.py
    
    ./src/test123/cms:
    Command.py  __init__.py
    
    ./src/test123/__pycache__:
    __init__.cpython-36.pyc
    
    ./src/test123.egg-info:
    dependency_links.txt  PKG-INFO  SOURCES.txt  top_level.txt
    

      

    例子:

    setup.py文件(参数自己设置,默认不需要这么多)

    #!/usr/bin/env python
    # coding=utf-8
    
    from setuptools import setup, find_packages
    
    setup(
        name='test123',
        version="0.0.1",
        keywords=('test', 'personal'),
    
        author='Qirui Su',
        author_email="schangech@gmail.com",
    
        url="http://www.sendal.top",
    
        packages=find_packages('src'),
        package_dir={'': 'src'},
        include_package_data=True,
        package_data={
            'config': ['*.conf', '*.example'],
            'Docs': [],
            'Tests': []
        },
        # entry_points={
        #     'console_scripts': [
        #         'auth=Auth.Cmd.Command:main',
        #     ],
        # },
        # install_requires=[
        #     'requests',
        # ],
        platforms='any',
    
        license='http://www.apache.org/licenses/LICENSE-2.0.html',
        description='Only test build python setup packages',
        long_description=open("README.md").read(),
        classifiers=[
                    'Development Status :: 4 - Beta',
                    'Intended Audience :: Developers',
                    'Topic :: Software Development :: Build Tools',
                    'License :: OSI Approved :: MIT License',
                    'Programming Language :: Python :: 2.6',
                    'Programming Language :: Python :: 2.7',
                    'Programming Language :: Python :: 3',
                    'Programming Language :: Python :: 3.3',
                    'Programming Language :: Python :: 3.4',
                    'Programming Language :: Python :: 3.5',
        ],
    )
    

      

     软件包的__init__.py文件

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    from __future__ import absolute_import
    
    from test123.Service import main, nihao, Animal
    from test123.cms.Command import nihao123
    
    N = "suqirui"
    A = 25
    
    __all__ = ("A", "main", "nihao", "Animal", "nihao123", "test")
    
    
    def test():
        print("Hello, I'm nihao.")
    

      

    打包完成之后,自己测试效果:

    cd dist
    pip install test123-0.0.1-py2.py3-none-any.whl 
    Processing ./test123-0.0.1-py2.py3-none-any.whl
    Installing collected packages: test123
    Successfully installed test123-0.0.1
    

    python包目录结构

    ls /home/sendal/.virtualenvs/py36/lib/python3.6/site-packages/test123*
    /home/sendal/.virtualenvs/py36/lib/python3.6/site-packages/test123:
    cms  __init__.py  __pycache__  Service.py
    
    /home/sendal/.virtualenvs/py36/lib/python3.6/site-packages/test123-0.0.1.dist-info:
    DESCRIPTION.rst  INSTALLER  METADATA  metadata.json  RECORD  top_level.txt  WHEEL
    

    安装完成之后,效果图

    python
    Python 3.6.4 (default, Feb  3 2018, 09:09:12) 
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import test123
    >>> dir(test123)
    ['A', 'Animal', 'N', 'Service', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'absolute_import', 'cms', 'main', 'nihao', 'nihao123', 'test']
    >>> test123.test
    <function test at 0x7fe8532db598>
    >>> test123.test()
    Hello, I'm amoblin.
    >>> test123.nihao123()
    nihao123
    >>> test123.N
    'suqirui'
    >>> test123.A
    25
    >>>
    

      

    到这里,本地打包已经ok,如果想开源给大家使用,请上传到pypi,步骤请google一下

    补充一下上传到pypi

    注册pypi账户 https://pypi.org/

    然后在本地家目录下: 新建 $home/.pypirc文件

    [distutils]
    index-servers=pypi
    
    [pypi]
    repository = https://upload.pypi.org/legacy/
    username = 你的注册用户名
    password = 你的密码,可以留空,后面手动输入
    

    注册你的软件包(注册之前,先查一下,是否存在同名的,否则是传不上去的,提示403错误),查询地址: https://pypi.org/

    安装上传软件twine pip install twine

    你可以任选以下两种方式之一发布你的轮子。

    1. 使用命令:python setup.py sdist upload,还是和上面一样,最简单但是有安全隐患。

    2. 使用 twine: twine upload dist/*

    没有提示错误,你就可以在你的账户目录下看到上传的软件包了。

    参考:

    http://wsfdl.com/python/2015/09/06/Python%E5%BA%94%E7%94%A8%E7%9A%84%E6%89%93%E5%8C%85%E5%92%8C%E5%8F%91%E5%B8%83%E4%B8%8A.html  (将wheel包的)

    http://www.worldhello.net/2010/12/08/2178.html (将egg包的) 

    https://www.xncoding.com/2015/10/26/python/setuptools.html  (打包发布的)

  • 相关阅读:
    《程序员修炼之道——从小工到专家》读后感二
    2019.10.14课堂总结
    《程序员修炼之道——从小工到专家》读后感一
    2019.09.23课堂总结
    回文序列判断
    动手动脑
    2018/10/21动手动脑及类的创建
    动手动脑-java重载
    第二次上机实验体会
    Java第一次上机实验源代码
  • 原文地址:https://www.cnblogs.com/schangech/p/8951939.html
Copyright © 2011-2022 走看看