python (库、应用程序 ) (打包、上传、构建、安装) => LTS

本文档详述了如何使用Python的setup.py进行库的打包和发布流程,涵盖了不同平台的打包方式,如tar.gz、exe、rpm及whl等,并介绍了如何设置requirements.txt和MANIFEST.in文件。此外,还提供了利用Twine上传到PyPI的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先你要有一个setup.py (不管用上古的distutils还是标准的setuptools还是其他先进的打包工具)

库的打包

打包成tar.gz python setup.py sdist
打包成exe python setup.py bdist_wininst
打包成rpm python setup.py bdist_rpm(rpm 指令支持)
打包成whl python setup.py bdist_wheel (需要 pip install wheel)
Wheel Python Limit:

--universal        Specifies that a pure-python wheel is "universal"
                   (i.e., it works on any version of Python).  This
                   equates to the tag "py2.py3".
--python-tag XXX   Specifies the precise python version tag to use for
                   a pure-python wheel.
--py-limited-api {cp32|cp33|cp34|...}
                   Specifies Python Py_LIMITED_API compatibility with
                   the version of CPython passed and later versions.
                   The wheel will be tagged cpNN.abi3.{arch} on CPython 3.
                   This flag does not affect Python 2 builds or alternate
                   Python implementations.

                   To conform to the limited API, all your C
                   extensions must use only functions from the limited
                   API, pass Extension(py_limited_api=True) and e.g.
                   #define Py_LIMITED_API=0x03020000 depending on
                   the exact minimun Python you wish to support.

setup.py 上联requirements.txt 下联MANIFEST.in, 中走LICENSE
pip freeze > requirements.txt
参考
requirements.txt

alembic==0.8.6
bleach==1.4.3
click==6.6
dominate==2.2.1
Flask==0.11.1
Flask-Bootstrap==3.3.6.0
Flask-Login==0.3.2
Flask-Migrate==1.8.1
Flask-Moment==0.5.1
Flask-PageDown==0.2.1

(可以不带版本号)

MANIFEST.in

include LICENSE
include README.rst
include requirements.txt
recursive-include polls/static *
recursive-include polls/templates *

添加非py文件

import os
from setuptools import find_packages, setup

with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
    README = readme.read()
with open('requirements.txt') as f:
    required = f.read().splitlines()

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name = 'minghu6',
    version = '1.0.0',
    install_requires = required,
    packages = find_packages(),
    entry_points = {
        'console_scripts' : ['captcha=minghu6.tools.captcha:interactive',
                             'ffmpeg_fix=minghu6.tools.ffmpeg_fix:interactive',
                             'fileformat=minghu6.tools.fileformat:interactive',
                             'launch=minghu6.tools.launch:interactive',
                             'proxy_ip=minghu6.tools.proxy_ip:interactive',
                             'yinyuetai=minghu6.tools.Tai_downloader:interactive',
                             'text_editor=minghu6.tools.textEditor:interactive',
                             'youtube=minghu6.tools.Tube_downloader:interactive',
                             'countlines=minghu6.tools.count_lines:interactive',
                             'add_pypath=minghu6.tools.add_py_path:interactive',
                             'tieba=minghu6.tools.Tieba_downloader:interactive',
                             ],
    },
    include_package_data = True,
    licence = 'BSD License', 
    description = 'A Core Utils Set for minghu6.',
    long_description = README,
    url = 'https://github.com/minghu6/minghu6_py',
    author = 'minghu6',
    author_email = 'a19678zy@163.com',
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
    ],
    )

以setuptools为例,
其中, console_scripts是会打包成所在
对应平台的二进制文件, 比如Windows: pip.exe
'<binary-file-name> = <setup.py 下的pacage路径>:<入口函数名>'

另外注意 classifiers 是部署应用的必要环境,而不是支持的环境,所以不必过于细致,限制了
潜在的应用场合

Copyright (c) 2015, minghu6 <a19678zy@outlook.com>
All rights reserved.

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

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

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

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

以BSD (3-clauses)为例

上传至PyPI
  1. 注册PyPI账户PyPI Doc ref
  2. 安装 twine (pip install twine)
  3. write a ~/.pypirc file like so.

    [distutils]
    index-servers=pypi
    
    [pypi]
    repository = https://upload.pypi.org/legacy/
    username = <username>
    password = <password>
    
  4. twine upload dist/*

通过pip安装
假设包名为minghu6
pip install -v minghu6==1.0.1
通过travis 自动部署

安装travis,
根据官方教程,

注意:

1.通过travis的 Job log(包括raw) 查看部署的详细信息(trace)
2.tags:true 需要与 all_branches: true 同时开启
3.使用travis lint 进行格式检查,可能会超时报错,多试几次
Add badge for pypi

仅支持rst格式, 且需要注意冒号缩进

.. image:: https://landscape.io/github/minghu6/minghu6_py/master/landscape.svg?style=flat
   :target: https://landscape.io/github/minghu6/minghu6_py/master
   :alt: Code Health

PyPI version

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值