Structurer les importations

Créer des paquetages Python

James Fulton

Climate informatics researcher

Sans importations de paquet

import mysklearn
help(mysklearn.preprocessing)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'mysklearn' has no 
  attribute 'preprocessing'

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py
|   |-- normalize.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Créer des paquetages Python

Sans importations de paquet

import mysklearn.preprocessing
help(mysklearn.preprocessing)
Help on package mysklearn.preprocessing in 
mysklearn:

NAME
    mysklearn.preprocessing - A subpackage 
      for standard preprocessing operations.

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py
|   |-- normalize.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Créer des paquetages Python

Sans importations de paquet

import mysklearn.preprocessing
help(mysklearn.preprocessing.normalize)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 
  'mysklearn.preprocessing' has no attribute
  'normalize'

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py
|   |-- normalize.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Créer des paquetages Python

Sans importations de paquet

import mysklearn.preprocessing.normalize
help(mysklearn.preprocessing.normalize)
Help on module mysklearn.preprocessing.normalize
in mysklearn.preprocessing:

NAME
    mysklearn.preprocessing.normalize - A module
      for normalizing data.

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py
|   |-- normalize.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Créer des paquetages Python

Importer des sous-paquets dans des paquets

mysklearn/__init__.py

Importation absolue

from mysklearn import preprocessing
  • La plus utilisée – plus explicite

Importation relative

from . import preprocessing
  • Parfois utilisée – plus courte et parfois plus simple

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py        <--
|-- preprocessing
|   |-- __init__.py
|   |-- normalize.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Créer des paquetages Python

Importer des modules

Nous avons importé preprocessing dans mysklearn

import mysklearn
help(mysklearn.preprocessing)
Help on package mysklearn.preprocessing in 
mysklearn:

NAME
    mysklearn.preprocessing - A subpackage 
      for standard preprocessing operations.

Mais preprocessing n'est pas lié à normalize

import mysklearn
help(mysklearn.preprocessing.normalize)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 
  'mysklearn.preprocessing' has no attribute
  'normalize'
Créer des paquetages Python

Importer des modules

mysklearn/preprocessing/__init__.py

Importation absolue

from mysklearn.preprocessing import normalize

Importation relative

from . import normalize

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py    <--
|   |-- normalize.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Créer des paquetages Python

Restructurer les importations

import mysklearn
help(mysklearn.preprocessing.normalize.normalize_data)
Help on function normalize_data in module
mysklearn.preprocessing.normalize:

normalize_data(x)
    Normalize the data array.
Créer des paquetages Python

Importer une fonction dans un sous-paquet

mysklearn/preprocessing/__init__.py

Importation absolue

from mysklearn.preprocessing.normalize import \
    normalize_data

Importation relative

from .normalize import normalize_data

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py    <--
|   |-- normalize.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Créer des paquetages Python

Importer une fonction dans un sous-paquet

import mysklearn
help(mysklearn.preprocessing.normalize_data)
Help on function normalize_data in module
mysklearn_imp.preprocessing.normalize:

normalize_data(x)
    Normalize the data array.
Créer des paquetages Python

Importer entre modules frères

Dans normalize.py

Importation absolue

from mysklearn.preprocessing.funcs import (
    mymax, mymin
)

Importation relative

from .funcs import mymax, mymin

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py
|   |-- normalize.py   <--
|   |-- funcs.py
|   |-- standardize.py
|-- regression
|   |-- __init__.py
|   |-- regression.py
|-- utils.py
Créer des paquetages Python

Importer entre modules éloignés

Une exception personnalisée MyException est dans utils.py

Dans normalize.py, standardize.py et regression.py

Importation absolue

from mysklearn.utils import MyException

Importation relative

from ..utils import MyException

Arborescence d'un paquet avec sous-paquets

mysklearn/
|-- __init__.py
|-- preprocessing
|   |-- __init__.py
|   |-- normalize.py   <--
|   `-- standardize.py <--
|-- regression
|   |-- __init__.py
|   |-- regression.py  <--
`-- utils.py
Créer des paquetages Python

Aide-mémoire : importations relatives

  • from . import module
    • Depuis le répertoire courant, importer module
  • from .. import module
    • Depuis un répertoire au-dessus, importer module
  • from .module import function
    • Depuis un module du répertoire courant, importer function
  • from ..subpackage.module import function
    • Depuis un sous-paquet un niveau au-dessus, dans module de ce sous-paquet, importer function
Créer des paquetages Python

Passons à la pratique !

Créer des paquetages Python

Preparing Video For Download...