▚ 00 前期准备


📣 1. 以Unix/macOS系统为例。

📣 2. 确保pip为最新版本,可使用以下命令来更新pip:

python3 -m pip install --upgrade pip

在这里插入图片描述



▚ 01 创建一个简单的项目


1.1 新建项目的结构

  • 我们在目录packaging_tutorial下进行操作。
  • 项目名称为:example_package_wayne
  • 整个文件结构为:

1.2 设置文件内容

  • __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.

在这里插入图片描述



▚ 02 生成发行版存档 (whl或tar.gz)


  • 上面列出的文件将自动包含在您的源代码发行版中。

  • 确保安装了最新版本的PyPAbuild,可使用以下命令来更新:

python3 -m pip install --upgrade build
  • 现在可以在pyproject. toml所在的目录下运行如下命令:
python3 -m build
  • 编译过程中,会产生如下的输出信息:
  • 该命令执行完后,会在dist目录中生成如下红框内的两个文件:
  • 其中,tar.gz文件是源发行版a source distribution ,而.whl文件是构建发行版a built distribution

在这里插入图片描述



▚ 03 本地安装/卸载该包&测试


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


在这里插入图片描述



▚  参考文献:


Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐