python包创建

1.创建一个文件夹

文件夹中需包含__init__.py 文件,该文件夹即为一个包,文件夹名称为包的名称。

2.编写__init__.py 文件

ps:__init__.py文件可以为空。
如果目录中包含了 __init__.py 时,当用 import 导入该目录时,会执行 __init__.py 里面的代码。
为方便调用包,最好在 __init__.py 文件中直接编写实现模块功能的变量、函数和类。同时也可导入其他包。例如:

__all__ = ['test11', 'test12']

# 默认只导入test11
from mypackage.subpackage_1 import test11
import pandas as pd

__all__可控制全部导入的模块。
__init__.py文件的作用可查看:
https://learnku.com/articles/23159/the-effect-of-python-init-py
http://c.biancheng.net/view/2401.html
https://www.cnblogs.com/tp1226/p/8453854.html

3.setup文件

对于一个需要被分发的包来说,其根目录包含一个 setup.py 脚本,它定义了 distutils 模块中描述的所有元数据,并将其合并为标准的 setup() 函数调用的参数。

sample:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="example-pkg-your-username",
    version="0.0.1",
    author="Example Author",
    author_email="author@example.com",
    description="A small example package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

setup 接受的最重要的参数包括
name:包的名称
version:包的版本号,(额外技巧参考:http://c.biancheng.net/view/5494.html
descriptions:包含描述包的几句话。
long_description:包含完整说明,可以使用 reStructuredText 格式。
keywords:定义包的关键字列表。
authors:作者的姓名或组织。
author_email:联系人电子邮件地址。
url:项目的 URL。
license:许可证(GPL、LGPL等)
packages:包中所有名称的列表,setuptools 提供了一个名为 find_packages 的小函数来计算它。
namespace_packages:命令空间包的列表。

分类器:
( 目前PYPI共分了9 类,参考:https://pypi.org/classifiers/
开发状态(DevelopmentStatus)
环境(Environment)
框架(Framework)
目标受众(IntendedAudience)
许可证(License)
自然语言(Natural Language)
操作系统(Operating System)
编程语言(Programming Language)
话题(Topic)

4.README文件

python包在pypi中会显示一个项目的readme文件(通常使用md编辑),pypi只支持reStructuredText标记语言。当然如果想使用其他语言,那就只能读取文件,然后将数据赋值到long_description中,这样也可以显示。这时需要使用pypandoc包将其他语言转换为reStructuredText。转换方式参考(setup中编写):

try:  

    from pypandoc import convert

    def read_md(f):

        return convert(f,'rst')

except ImportError:

    covert=None

    print('warning:pypandoc not found')

README=os.path.join(os.path.dirname(__file__),'README.md')

setup(

    name='mytestpack',

    long_description=read_md(README),

)

详细链接:

http://c.biancheng.net/view/5494.html

5.LICENSE 文件

MIT是一种简单许可证,在开源项目中广泛使用,内容如下:

MIT License

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

更多license可参考:https://choosealicense.com/

6.管理依赖

许多项目需要安装和(或)使用一些外部包。有两种方法提供依赖包:
第一种:在setup脚本中提供列表

from setuptools import setup
setup(
    name = 'some-package',
    install_requires=['falcon', 'requests', 'delorean']
    #...
)

第二种:使用 requirements.txt 文件来追踪包的依赖列表

from setuptools import setup
import os
def strip_comments(l)return l.split ('#', 1)[0].strip()
def reqs(*f):
    return list(filter(None, [strip_comments(l) for l in open(os.path.join(os.getcwd(), *f)).readlines()]))
setup(
    name='some-package',
    install.requires = reqs('requirements.txt')
    #...
)

7.源代码包和构建包源代码包与构建包

7.1 sdist
sdist命令创建源代码发行版

python setup.py sdist  

生成的文件形式为举例:mypackage-0.1.1.tar.gz的打包文件包含与项目相同的结构
7.2 bdist
分发预构建的发行版

python setup.py bdist  

会生成文件形式为wjtestpack-0.0.0.win-amd64.zip格式的压缩包,这个包里主要有site-packages的文件夹,和一些缓存字节码.pyc结尾的文件。
7.3 wheels

python setup.py bdist_wheel  

生成的文件为wjtestpack-0.0.0-py3-none-any.whl形式的文件。

参考文献:

http://c.biancheng.net/view/5494.html
https://packaging.python.org/tutorials/packaging-projects/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值