Python的模块化机制是值得所有编程语言学习的,一般来说,开发者想得到的库都会有Python版本的。这种优势便得益于Python的易打包性。本文介绍的方法至少有两种优势:

  1. 将自己的代码变成开源库,直接通过pip install xxx即可安装,很容易做开源工作;
  2. 将自己经常用的代码段打包成whl,使代码复用性更高,并且彻底解决python繁杂令人诟病的多级目录引用不方便的问题。

假设咱们要打包一个函数:

def add_one(n):
    return n + 1

咱们先定库的名字为MuzhanTest,咱们想实现的效果是:

pip install MuzhanTest
python
>>> import MuzhanTest
>>> MuzhanTest.add_one(3)
4

那么,我们要做哪些工作呢?第一步,达到如下的目录结构:

PackagingDemo
├── README.md
└── src
    └── MuzhanTest
        ├── __init__.py
        └── add_one.py

这里的README.md随便描述一下函数功能即可,不影响whl的编译。这里的__init__.py是为了把当前文件夹等效为.py文件,想进一步了解__init__.py用法的可戳《【Python技巧】init.py搞定import》。这里的init文件这么写:(只有一行)

from .add_one import *

这里的add_one.py只有两行:

def add_one(n):
    return n + 1

那么,我们就开始打包咯!
先写一个LICENSE吧,复制下面内容粘贴就行(如果你打算上传whl到Pypi的话,你自己可以修改为其他的你需要的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.

然后,你需要有一个pyproject.toml文件来描述项目信息:

requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "MuzhanTest"
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"

如果你不打算上传到pypi,你只需在这里修改name=xxx即可。如果你打算开源并且上传到pypi,那请你把上述表格填写尽量充分。
现在,我们再看一下目录结构:

PackagingDemo
├── LICENSE
├── README.md
├── pyproject.toml
└── src
    └── MuzhanTest
        ├── __init__.py
        └── add_one.py

多了LICENSEpyproject.toml两个文件而已。接下来,我们可以把代码开始打包了,先回到./PackagingDemo目录下,再:

pip install --upgrade build
python -m build

编译成功以后,目录结构将变为:

PackagingDemo
├── LICENSE
├── dist
│   ├── MuzhanTest-0.0.1-py3-none-any.whl
│   └── MuzhanTest-0.0.1.tar.gz
├── pyproject.toml
└── src
    ├── MuzhanTest
    │   ├── __init__.py
    │   └── add_one.py
    └── MuzhanTest.egg-info
        ├── PKG-INFO
        ├── SOURCES.txt
        ├── dependency_links.txt
        └── top_level.txt

我们能快速找到MuzhanTest-0.0.1-py3-none-any.whl,并且安装之:

pip install MuzhanTest-0.0.1-py3-none-any.whl

安装成功后:

python
>>> import MuzhanTest
>>> MuzhanTest.add_one(3)
4

到此,你的whl就已经打包成功了~
如果你想把whl上传到pypi,让大家都能pip install到你的whl,你只需在pypi注册,然后跟随流程即可。

参考:https://packaging.python.org/en/latest/tutorials/packaging-projects/

Logo

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

更多推荐