简体   繁体   中英

Absolute and relative import of Python modules: a matplotlib example

Many questions have been asked regarding how to import Python modules, specifically on whether to use absolute or explicit relative import ( here for example). The import style, as suggested by the Python Software Foundation, can be found here . In short, it recommends absolute import.

I'm writing this question because I assume the guys who develop matplotlib know what they are doing.

Given this assumption and assuming I understand the major/obvious differences between these two kinds of import, I would be interested in understanding the tiny differences between them that influenced the matplotlib's developers to write something like this:

import matplotlib
import matplotlib.cbook as cbook
from matplotlib.cbook import mplDeprecation
from matplotlib import docstring, rcParams
from .transforms import (Bbox, IdentityTransform, TransformedBbox,
                         TransformedPath, Transform)
from .path import Path 

This is the beginning of artist.py , contained inside the matplotlib module (ie matplotlib.artist ). I'm looking at matplotlib-1.5.1.

I would like to focus the attention on modules matplotlib.cbook , matplotlib.transforms , and matplotlib.path . All three of them are pure Python modules (ie module_name.py files).

Why from matplotlib.cbook import mplDeprecation has been chosen rather than from .cbook import mplDeprecation and why from .path import Path was preferred to from matplotlib.path import Path ?

Perhaps there is no particular reason and these choices just reflect different styles of different developers; perhaps there is something I'm missing.

An important thing to remember about the matplotlib code base is that it is very old (have git history back to 2003 and have lost another few years), large (>93k lines of python, 17k lines of c++), and has over 450 contributors.

Having a look at git-blame (off of the 2.x branch but the imports are pretty stable) shows:

08:29 $ git blame matplotlib/artist.py | head -n 18


5fca7e31 (Thomas A Caswell         2013-09-25 11:36:00 -0500    1) from __future__ import (absolute_import, division, print_function,
5fca7e31 (Thomas A Caswell         2013-09-25 11:36:00 -0500    2)                         unicode_literals)
f4adec7b (Michael Droettboom       2013-08-14 10:18:10 -0400    3) 
07e22753 (Matthew Brett            2016-06-06 12:08:35 -0700    4) import six
0ea5fff3 (Thomas A Caswell         2015-12-01 14:40:34 -0500    5) from collections import OrderedDict
f4adec7b (Michael Droettboom       2013-08-14 10:18:10 -0400    6) 
453e0ece (Nelle Varoquaux          2012-08-27 23:16:43 +0200    7) import re
453e0ece (Nelle Varoquaux          2012-08-27 23:16:43 +0200    8) import warnings
731f6c86 (Michael Droettboom       2013-09-27 09:59:48 -0400    9) import inspect
e1d30c85 (Jens Hedegaard Nielsen   2015-08-18 19:52:48 +0100   10) import numpy as np
b44e8f20 (John Hunter              2008-12-08 23:28:55 +0000   11) import matplotlib
99b89a87 (John Hunter              2008-06-03 20:28:14 +0000   12) import matplotlib.cbook as cbook
c137a718 (Thomas A Caswell         2014-11-23 00:37:28 -0500   13) from matplotlib.cbook import mplDeprecation
527b7d9a (Michael Droettboom       2010-06-11 18:17:52 +0000   14) from matplotlib import docstring, rcParams
b2408c33 (Cimarron Mittelsteadt    2014-09-12 15:58:25 -0700   15) from .transforms import (Bbox, IdentityTransform, TransformedBbox,
b2408c33 (Cimarron Mittelsteadt    2014-09-12 15:58:25 -0700   16)                          TransformedPath, Transform)
f4adec7b (Michael Droettboom       2013-08-14 10:18:10 -0400   17) from .path import Path
f2a0c7ae (John Hunter              2007-03-20 21:48:31 +0000   18) 

You can see that these lines were last touched by a number of people (apparently including me) over a range of years.

I would not read too much into this difference, but if you want to dive deeper try looking at the commit messages on those changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM