app开发者平台在数字化时代的重要性与发展趋势解析
1085
2022-11-23
python工程打包上传pypi
文章目录
1. 概要2. 创建包文件3. 创建setup.py4. 创建README.md5. 创建许可证6. 生成分发档案7. 上传PYPI8. 成果检验参考
1. 概要
本博文将简单介绍下,如何将自己的python代码打包发布到pypi,从而实现“pip install {your package name}”。
2. 创建包文件
3. 创建setup.py
setup.py是setuptools的构建脚本。它告诉setuptools你的包(例如名称和版本)以及要包含的代码文件。
import setuptoolswith 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=" 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告诉索引并点一些关于你的包的其他元数据。在这种情况下,该软件包仅与Python3兼容,根据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 withoutmodification, are permitted provided that the following conditions aremet: * 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 NOTLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6. 生成分发档案
下一步是为包生成分发包。这些是上传到包索引的档案,可以通过pip安装。 现在从setup.py位于的同一目录运行此命令:
python setup.py sdist bdist_wheel
7. 上传PYPI
您需要做的第一件事是在 PyPI上注册一个帐户。Test PyPI是用于测试和实验的包索引的单独实例。这对于像我们不一定想要上传到真实索引的本教程那样很棒。要注册帐户,请访问在该项目命令行下输入:
twine upload dist/*
8. 成果检验
参考
https://zhuanlan.zhihu.com/p/61174349
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~