注册账号(重要)
创建项目
创建一个名为 example_pkg
的项目,目录结构如下
example_pkg
/example_pkg
__init__.py
编辑文件 example_pkg/__init__.py
name = "example_pkg"
创建包文件
/example_pkg
/example_pkg
__init__.py
setup.py
LICENSE
README.md
创建 setup.py
按照自己的信息,逐项填写即可
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="example_pkg",
version="0.0.1",
author="Example Author",
author_email="[email protected]",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/example-project",
packages=setuptools.find_packages(),
classifiers=(
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
),
)
创建 README.md
建议写的详细些,展示你项目的主要介绍
# Example Package
This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
to write your content.
创建 LICENSE
可以忽略
可参考:https://choosealicense.com/
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.
生成发布压缩包
确保已经安装setuptools
和 wheel
python3 -m pip install --user --upgrade setuptools wheel
setup.py
文件同目录命令行下运行
python3 setup.py sdist bdist_wheel
产生两个文件
dist/
example_pkg-0.0.1-py3-none-any.whl
example_pkg-0.0.1.tar.gz
tar.gz 源文件
.whl 分发文件
上传文件
安装twine
pip install twine
上传
twine upload dist/*
没有报错就成功了
Uploading distributions to https://test.pypi.org/legacy/
Enter your username: [your username]
Enter your password:
Uploading example_pkg-0.0.1-py3-none-any.whl
100%|█████████████████████| 4.65k/4.65k [00:01<00:00, 2.88kB/s]
Uploading example_pkg-0.0.1.tar.gz
100%|█████████████████████| 4.25k/4.25k [00:01<00:00, 3.05kB/s]
安装你刚刚上传的包
pip install -i https://test.pypi.org/simple/ example_pkg
导入测试
>>> import example_pkg
>>> example_pkg.name
'example_pkg'