【问题标题】:Absolute and relative import of Python modules: a matplotlib examplePython 模块的绝对和相对导入:一个 matplotlib 示例
【发布时间】:2016-08-18 20:48:51
【问题描述】:

关于如何导入 Python 模块,特别是关于是使用绝对导入还是显式相对导入(例如here),有人提出了许多问题。 Python 软件基金会建议的导入样式可以在here 找到。简而言之,它建议绝对导入。

我写这个问题是因为我假设开发 matplotlib 的人知道他们在做什么。

鉴于此假设并假设我了解这两种导入之间的主要/明显差异,我将有兴趣了解影响 matplotlib 开发人员编写如下内容的它们之间的微小差异:

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 

这是artist.py 的开头,包含在matplotlib 模块中(即matplotlib.artist)。我在看 matplotlib-1.5.1。

我想将注意力集中在模块 matplotlib.cbookmatplotlib.transformsmatplotlib.path。这三个都是纯 Python 模块(即module_name.py 文件)。

为什么选择from matplotlib.cbook import mplDeprecation 而不是from .cbook import mplDeprecation 以及为什么首选from .path import Path 而不是from matplotlib.path import Path

也许没有什么特别的原因,这些选择只是反映了不同开发者的不同风格;也许我缺少一些东西。

【问题讨论】:

    标签: python matplotlib import module


    【解决方案1】:

    关于 matplotlib 代码库要记住的重要一点是,它非常古老(git 历史可以追溯到 2003 年,并且已经丢失了几年)、大型(> 93k 行 python,17k 行 c++),并且具有超过 450 位贡献者。

    看看 git-blame(在 2.x 分支之外,但导入非常稳定)显示:

    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) 
    

    您可以看到,这些线条最后一次被许多人(显然包括我)在几年内触及。

    我不会过多解读这种差异,但如果您想深入了解,请尝试查看有关这些更改的提交消息。

    【讨论】:

    • 好吧,我想这只是由于不同的开发人员的口味。或许你可以告诉我们你的情况:你为什么选择from matplotlib.cbook ...而不是from .cbook ...
    • @Gioker 我真的不记得了。正如您指出的那样,pep8 建议绝对导入,而且更加明确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2011-04-06
    • 2016-03-07
    相关资源
    最近更新 更多