python库打包

一、背景

想让自己写的python库可以使用pip install xxx安装。

二、环境准备

  • 注册PYPI账号
  • 已经写好的能正常使用的库/方法/项目(可以本地调用)
  • 安装依赖库setuptoolstwinw
pip install setuptools
pip install twine  # 简化将库发布到PYPI流程的工具
pip install wheel  # 制作whl文件需要

三、步骤

1. 调整目录结构

参考python项目打包结构

|--mypackage
	__init__.py
	__version__.py
	|--common
	|--config
	|--src
setup.py
requirements.txt
LICENSE
README.rst
MANIFEST.in
.gitnore

目录文件的作用:

  • MANIFEST.in
    当你使用 Python 的打包工具(如 setuptools 或 distutils)创建分发包时,
    它们只会自动包含项目目录中的 Python 模块和包。但是,在某些情况下,你可
    能还需要在分发包中包含一些其他的文件,如文档、配置文件、数据文件等。这
    时,就可以使用 MANIFEST.in 文件来指定要包含的其他文件。
include DrissionPage/configs/configs.ini
include DrissionPage/*.pyi
include DrissionPage/*/*.py
include DrissionPage/*/*.pyi
  • setup.py
# -*- coding:utf-8 -*-
from setuptools import setup, find_packages

with open("README.md", "r", encoding='utf-8') as fh:
    long_description = fh.read()

setup(
    name="DrissionPage",
    version="3.2.31",
    author="g1879",
    author_email="[email protected]",
    description="Python based web automation tool. It can control the browser and send and receive data packets.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    license="BSD",
    keywords="DrissionPage",
    url="https://gitee.com/g1879/DrissionPage",
    include_package_data=True,
    packages=find_packages(),
    zip_safe=False,
    install_requires=[
        'lxml',
        'requests',
        'cssselect',
        'DownloadKit>=1.0.0',
        'FlowViewer>=0.3.0',
        'websocket-client',
        'click',
        'tldextract'
    ],
    classifiers=[
        "Programming Language :: Python :: 3.6",
        "Development Status :: 4 - Beta",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
    ],
    python_requires='>=3.6',
    entry_points={
        'console_scripts': [
            'dp = DrissionPage.commons.cli:main',
        ],
    },
)

字段说明:

  • version:这个简单,就是包的发布的版本,可以直接写在这,也可以从其他地方引用过来。

  • long_description:默认是rst(reStructuredText )格式的,也可以是其他格式,使用long_description_content_type字段指定,这个里面的内容是显示在pypi包首页上。

  • packages:申明你的包里面要包含的目录,比如 [‘mypackage’, ‘mypackage_test’] 可以是这种使用我的示例,让setuptools自动决定要包含哪些包

  • install_requires:申明依赖包,安装包时pip会自动安装

  • LICENSE

BSD 3-Clause License

Copyright (c) 2020, g1879
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  • README.md
项目简介,markdown格式

2. 打包上传

项目按照上述结构组织好之后,就可以进行打包了

  • check setup.py
python setup.py check

若没问题,无报错信息。

  • 生成打包文件
python setup.py bdist_wheel  # 编译生成wheel文件,会产生build,dist,XX.egg-info三个文件夹
pip wheel --wheel-dir=whl文件保存路径 setup.py文件所在路径  # 直接生成whl文件,不产生中间编译结果
python setup.py sdist # 生成tar包,即源码包
  • 上传生成的包
python setup.py sdist upload  # 上传source包
python setup.py bdist_wheel upload  # 上传二进制包
  • 包的安装
# 源码包安装,解压+编译+安装
python setup.py build
python setup.py install
# whl包
pip install XX.whl
  • 测试库是否可以正常安装
python setup.py develop
# 该方法不会真正的安装包,而是在系统环境中创建一个软链接指向包实际所在目录。
# 这边在修改包之后不用再安装就能生效,便于调试

你可能感兴趣的:(python,开发语言)