文章目录

1. 概要

本博文将简单介绍下,如何将自己的python代码打包发布到pypi,从而实现“pip install {your package name}”。

2. 创建包文件

python工程打包上传pypi_ide

3. 创建setup.py

setup.py是setuptools的构建脚本。它告诉setuptools你的包(例如名称和版本)以及要包含的代码文件。

import setuptools

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

setuptools.setup(
name="gdalTools",
version="0.1",
author="******",
author_email="*******@qq.com",
description="some useful tools in gdal",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/******/******.git",
packages=setuptools.find_packages(),
classifiers=(
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
),
)
  • name是包的分发名称;
  • version 是包版本;
  • author用于识别包的作者;
  • description是一个简短的,一句话的包的总结;
  • url是项目主页的URL。对于许多项目,这只是一个指向GitHub,GitLab,Bitbucket或类似代码托管服务的链接
  • packages是应包含在分发包中的所有Python 导入包的列表。我们可以使用自动发现所有包和子包,而不是手动列出每个包。在这种情况下,包列表将是example_pkg,因为它是唯一存在的包;
  • classifiers告诉索引并点一些关于你的包的其他元数据。在这种情况下,该软件包仅与Python
    3兼容,根据MIT许可证进行许可,并且与操作系统无关。您应始终至少包含您的软件包所使用的Python版本,软件包可用的许可证以及您的软件包将使用的操作系统。

4. 创建README.md

打开README.md并输入以下内容。可以自定义此项,放入一些对项目的介绍。

5. 创建许可证

Copyright (c) 2005-2020, NumPy Developers.
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 NumPy Developers nor the names of any
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
OWNER 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.

6. 生成分发档案

下一步是为包生成分发包。这些是上传到包索引的档案,可以通过pip安装。
现在从setup.py位于的同一目录运行此命令:

python setup.py sdist bdist_wheel

python工程打包上传pypi_ide_02
此命令应输出大量文本,一旦完成,应在dist目录中生成两个文件:
python工程打包上传pypi_python_03

7. 上传PYPI

您需要做的第一件事是在 PyPI上注册一个帐户。Test PyPI是用于测试和实验的包索引的单独实例。这对于像我们不一定想要上传到真实索引的本教程那样很棒。要注册帐户,请访问​​https://pypi.org/​​ 在该项目命令行下输入:

twine upload dist/*

输入 PyPI注册的用户名和密码。命令完成后,您应该看到与此类似的输出:
python工程打包上传pypi_python_04

8. 成果检验

python工程打包上传pypi_ide_05

参考

​https://zhuanlan.zhihu.com/p/61174349​