Структура пакета в Python

Содержание
Введение
Пример
Похожие статьи

Введение

PEP 420

Перед изучением этой статьи убедитесь, что вам знакомы темы «sys.path в Python» «Пакеты в Python» «Namespace пакеты в Python»

project_name ├── docs ├── README.rst ├── setup.py ├── src │ └── package_name │ ├── __init__.py │ ├── more_source.py │ └── subpackage1 │ └── __init__.py └── tests └── test_code.py

Корень проекта - project_name это не часть пакета

.rst означает reStructuredText

============ Project Name =========== A brief description of the project Section 1 ========= Installation or "quick start" information can go here. Subsetion --------- Some details can go here.

src содержит кода пакета

Важно помнить, что если запустить Python из project_name то эта директория будет добавлена в sys.path

Значит всё, что лежит внутри может быть импортировано. В том числе и то, что не лежит в package_name

А ведь именно то, что в package_name в конечном счёте получит пользователь вашего софта.

Тесты желательно хранить отдельно от пакета

Пример

python ├── demo_reader │ ├── README.rst │ ├── setup.py │ ├── src │ │ └── demo_reader │ │ ├── compressed │ │ │ ├── bzipped.py │ │ │ ├── gzipped.py │ │ │ └── __init__.py │ │ ├── __main__.py │ │ ├── multireader.py │ │ └── util │ │ ├── __init__.py │ │ └── writer.py │ └── tests │ └── test_multireader.py ├── test.bz2 └── test.gz

Из директории src можно выполнить

python -m demo_reader ../../test.gz

data compressed with gz by Andrei

# tests/test_multireader.py import unittest import demo_reader.multireader class TestMultireader(unittest.TestCase): def test_initialization(self): demo_reader.multireader.MultiReader('test_file.txt')

Рассмотрим пакет с setuptools

import setuptools setuptools.setup( name="demo_reader", version="1.0.0", description="Tools for reading various file formats", packages=setuptools.find_packages('src'), package_dir={'': 'src'})

Article author: Andrei Olegovich

Похожие статьи
Пакеты в Python
Namespace пакеты в Python
Правильная структура пакета
setuptools
Плагины
Python
Banner image

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: