Python3如何上传自己的PyPI项目
有过一定的 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=author@example.com11 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 ) 各个配置的字段的含义应该是不言而喻的,如果想了解更多,参见官网解释。 创建 LICENSELICENSE 是项目所遵循的许可证,以 MIT 为例: Copyright (c) 2018 The Python Packaging Authority Permission is hereby granted,1)">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 all copies 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.whl 100%|██████████████████████████████████████| 5.49k/5.49k [00:01<00,1)">4.66kB/s] Uploading example-pkg-heyulong-tar.gz 4.23k/4.23k [2.21kB/s] 执行完之后,即可在 PyPI 官网上看到自己上传的项目了。 安装你上传的 PyPI 项目,比如我的: pip install example-pkg-heyulong
? 这里简单介绍了上传 PyPI 项目的过程。更多细节请关注 PyPI 官网。 (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Java基础语法--一张图全了解了(上)
- StringBuilder/StringBuffer源码阅读笔记
- 在LAN上共享源代码(PHP)
- MyBatis深入理解参数
- php – mod_rewrite .htaccess如果URl不存在则导致500服务
- Java报错:java.math.BigDecimal cannot be cast to java.l
- 使用sqlhelper类查询时假如不需要参数化,那到时SqlParamet
- [Go] 在gin框架gorm下查询一对多的数据
- NumPy 教程(第 8 章): 高级索引
- php – htmlspecialchars将撇号转换为' for facebook