Python3如何上传自己的PyPI项目

Python3如何上传自己的PyPI项目,第1张

概述有过一定的 Python 经验的开发者都知道,当引入第三方包时,我们常常会使用 pip install 命令来下载并导入包。 那么,如何写一个自己的包,上传到 PyPI 呢,其他开发者也可以通过 pi

有过一定的 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 中创建一些文件。其目录结构如下:

/example_pkg    __init__.py  setup.py  liCENSE  README.md
创建 README.md 文件

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

# 测试这只是一个测试。- 测试 1- 测试 2- 测试 3
创建 setup.py 文件

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

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

 1 import setuptools 2  3 with open("README.md",r") as fh: 4     long_description = fh.read() 5  6 setuptools.setup( 7     name=example-pkg-your-username, 8     version=0.0.1 9     author=Example Author10     author_email=[email protected]11     description=A small example package12     long_description=long_description,1)">13     long_description_content_type=text/markdown14     url=https://github.com/pypa/sampleproject15     packages=setuptools.find_packages(),1)">16     classifIErs=[17         Programming Language :: Python :: 318         license :: OSI Approved :: MIT license19         Operating System :: OS Independent20     ],1)">21 )

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

创建 liCENSE

liCENSE 是项目所遵循的许可证,以 MIT 为例:

copyright (c) 2018 The Python Packaging AuthorityPermission is hereby granted,1)">free of charge,to any person obtaining a copyof this software and associated documentation files (the Software),to dealin the Software without restriction,including without limitation the rightsto use,copy,modify,merge,publish,distribute,sublicense,and/or sellcopIEs of the Software,and to permit persons to whom the Software isfurnished to do so,subject to the following conditions:The above copyright notice and this permission notice shall be included  allcopIEs or substantial portions of the Software.THE SOFTWARE IS PROVIDED AS ISinstall 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-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-any.whl100%|██████████████████████████████████████| 5.49k/5.49k [00:01<00,1)">4.66kB/s]Uploading example-pkg-heyulong-tar.gz4.23k/4.23k [2.21kB/s]

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

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

pip install example-pkg-heyulong

 

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

总结

以上是内存溢出为你收集整理的Python3如何上传自己的PyPI项目全部内容,希望文章能够帮你解决Python3如何上传自己的PyPI项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1189694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存