Пример минимального пакета#
Структура проекта:#
my_python_packages/
├── nlp/
│ ├── __init__.py
│ ├── cleaner.py
│ └── tokenizer.py
├── pyproject.toml
├── README.md
└── LICENSE
Шаг 1: Создание файлов#
1. nlp/__init__.py#
from .cleaner import TextCleaner
from .tokenizer import simple_tokenize
__version__ = "0.1.0"
2. nlp/cleaner.py#
class TextCleaner:
def __init__(self, text):
self.text = text
def clean(self):
return self.text.lower().strip()
3. nlp/tokenizer.py#
def simple_tokenize(text):
return text.split()
4. pyproject.toml#
[project]
name = "nlp"
version = "0.1.0"
description = "Simple NLP tools like cleaner and tokenizer"
authors = [{name="Твоё Имя", email="you@example.com"}]
license = "MIT"
readme = "README.md"
requires-python = ">=3.7"
dependencies = []
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
5. README.md#
Простой текстовый файл с описанием пакета.
# nlp
Simple tools for text cleaning and tokenization.
6. LICENSE#
(Можешь выбрать любую лицензию, например, MIT.)
Шаг 2: Сборка пакета#
Перейди в корень проекта (где
pyproject.toml).Установи
build, если он ещё не установлен:pip install build
Сгенерируй пакет:
python -m build
В корне появится каталог
dist/с файлами.tar.gzи.whl.
Шаг 3: Установка пакета#
Установи пакет локально:
pip install dist/nlp-0.1.0-py3-none-any.whl
Теперь ты можешь использовать пакет в другом проекте, например:
from nlp import TextCleaner, simple_tokenize
text = "Hello, World!"
cleaner = TextCleaner(text)
clean_text = cleaner.clean()
tokens = simple_tokenize(clean_text)
print(tokens)
Шаг 4: Обновление пакета#
Когда ты добавляешь новые классы/методы, увеличивай версию в
__init__.pyиpyproject.toml.Снова собирай и устанавливай новый
.whlфайл:python -m build pip install dist/nlp-0.2.0-py3-none-any.whl