以Unix/macOS系统为例。
前提准备:确保pip为最新版本,可使用以下命令来更新pip:
python3 -m pip install --upgrade pip
一、创建一个简单的项目
我们在目录packaging_tutorial
下进行操作。
项目名称为:example_package_wayne
。
整个文件结构为:
__init__.py:设置为空(简单起见)
calculator.py:实际的模块,这里是一个四则运算,内容为:
def calc(x, y, operator):
result = 0
if operator == "+":
result = x + y
if operator == "-":
result = x - y
if operator == "*":
result = x * y
if operator == "/":
result = x / y
return result
pyproject.toml:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "example_package_wayne"
version = "0.0.1"
authors = [
{ name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
"Homepage" = "https://github.com/pypa/sampleproject"
"Bug Tracker" = "https://github.com/pypa/sampleproject/issues"
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:
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.
二、生成发行版存档
上面列出的文件将自动包含在您的源代码发行版中。
确保安装了最新版本的PyPA
的build
,可使用以下命令来更新:
python3 -m pip install --upgrade build
现在可以在pyproject. toml所在的目录下运行如下命令:
python3 -m build
该命令执行完后,会在dist目录中生成如下红框内的两个文件:
其中,tar.gz文件是源发行版(a source distribution ),而.whl文件是构建发行版(a built distribution)。
三、本地安装/卸载该包&测试
3.1安装
在packaging_tutorial
目录下,执行如下命令:
pip3 install dist/example_package_wayne-0.0.1-py3-none-any.whl
若显示以下内容,则表示安装该包成功!
3.2 测试
在packaging_tutorial
目录下,新建目录tests
,在该目录tests
下创建文件test_calclator.py
,其内容为(这里介绍了三种导入与使用该包的方式):
# 三种导入包的方式
""" 方式一
import example_package_wayne.calculator
print(example_package_wayne.calculator.calc(8,4,"+"))
print(example_package_wayne.calculator.calc(8,4,"-"))
print(example_package_wayne.calculator.calc(8,4,"*"))
print(example_package_wayne.calculator.calc(8,4,"/"))
"""
""" 方式二
from example_package_wayne import calculator
print(calculator.calc(8,4,"+"))
print(calculator.calc(8,4,"-"))
print(calculator.calc(8,4,"*"))
print(calculator.calc(8,4,"/"))
"""
# 方式三
from example_package_wayne.calculator import calc
print(calc(8,4,"+"))
print(calc(8,4,"-"))
print(calc(8,4,"*"))
print(calc(8,4,"/"))
3.3卸载
在packaging_tutorial
目录下,执行如下命令:
pip3 uninstall example_package_wayne
参考文献:
Packaging Python Projects — Python Packaging User Guide
6. Modules — Python 3.11.3 documentation
Packaging and distributing projects — Python Packaging User Guide