有过一定的 python 经验的开发者都知道,当引入第三方包时,我们常常会使用 pip install 命令来下载并导入包。

那么,如何写一个自己的包,上传到 pypi 呢,其他开发者也可以通过 pip install 命令下载并导入?

本文提供了最简单的示例。

准备好项目目录

创建一个项目目录,其目录结构如下:

/packaging_tutorial
 /example_pkg
  __init__.py

其中,packaging_tutorial 是一个文件目录,example_pkg 是一个你希望上传的 python 包。

注:本人使用的是 virtualenv + virtualenvwrapper 构建的 python 虚拟环境,因此 python 和 pip 命令(而非 python3 和 pip3)直接对应的是我所指定的虚拟环境(python 3.6.7)。

创建一些必要文件

再向 packaging_tutorial 中创建一些文件。其目录结构如下:

/packaging_tutorial
 /example_pkg
  __init__.py
 setup.py
 license
 readme.md

创建 readme.md 文件

在 readme.md 可以输入一些介绍项目的文档。

# 测试

这只是一个测试。

- 测试 1
- 测试 2
- 测试 3

创建 setup.py 文件

setup.py 是 setuptools 的构建脚本,它提供了包的各种信息。

在 setup.py 中输入以下代码:

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",
  ],
)

各个配置的字段的含义应该是不言而喻的,如果想了解更多,参见官网解释。

创建 license

license 是项目所遵循的许可证,以 mit 为例:

copyright (c) 2018 the python packaging authority

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.

存档发布版本

一般来说,pip 默认应该都安装了 setuptools 和 wheel。如果没有安装,则安装之:

pip install setuptools wheel -i https://pypi.douban.com/simple

如果安装需要更新,则更新之:

pip install --upgrade setuptools wheel -i https://pypi.douban.com/simple

安装好最新版本后,在 setup.py 所在目录下输入:

python setup.py sdist bdist_wheel

这个命令会在生成一个 dist 目录,里面有两个文件:

dist/
 example_pkg_your_username-0.0.1-py3-none-any.whl
 example_pkg_your_username-0.0.1.tar.gz

tar.gz 是源文件存档,whl 是构建的发布版本。

上传发布版本

安装 twine:

pip install twine -i https://pypi.douban.com/simple

安装好之后,执行 twine 命令(这里,需要注意你已经注册了 pypi 的账号):

twine upload dist/*
enter your username: heyulong 
enter your password: 
uploading distributions to https://upload.pypi.org/legacy/
uploading example_pkg_heyulong-0.0.1-py3-none-any.whl
100%|██████████████████████████████████████| 5.49k/5.49k [00:01<00:00, 4.66kb/s]
uploading example-pkg-heyulong-0.0.1.tar.gz
100%|██████████████████████████████████████| 4.23k/4.23k [00:01<00:00, 2.21kb/s]

执行完之后,即可在 pypi 官网上看到自己上传的项目了。

安装你上传的 pypi 项目,比如我的:

pip install example-pkg-heyulong

这里简单介绍了上传 pypi 项目的过程。更多细节请关注 pypi 官网。

以上就是python 如何上传包到pypi的详细内容,更多关于python 上传包到pypi的资料请关注www.887551.com其它相关文章!