python环境下制作pypi软件包

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

根据 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

  

扫描二维码关注公众号,回复: 70878 查看本文章

例子:

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="[email protected]",

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

  

猜你喜欢

转载自www.cnblogs.com/schangech/p/8951939.html
今日推荐