如何在ReadTheDocs中使用matplotlib的plot-directive和python-3?

时间:2014-12-01 14:41:48

标签: python-3.x matplotlib read-the-docs

我有一个 python-3 项目,该项目使用 plot-directive 来动态生成和嵌入matplotlib的图表,并且我使用ReadTheDocs自动生成项目的文档 plot-directive确实是works ok in python-2,但它目前在python-3中失败。

具体而言,我在RTD日志上的失败是:

构建标准错误

html
-----

Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/wltp/envs/master/lib/python3.4/site-packages/sphinx/application.py", line 325, in setup_extension
    mod = __import__(extension, None, None, ['setup'])
ImportError: No module named 'matplotlib'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/wltp/envs/master/lib/python3.4/site-packages/sphinx/cmdline.py", line 253, in main
    warningiserror, tags, verbosity, parallel)
  File "/home/docs/checkouts/readthedocs.org/user_builds/wltp/envs/master/lib/python3.4/site-packages/sphinx/application.py", line 119, in __init__
    self.setup_extension(extension)
  File "/home/docs/checkouts/readthedocs.org/user_builds/wltp/envs/master/lib/python3.4/site-packages/sphinx/application.py", line 328, in setup_extension
    err)
sphinx.errors.ExtensionError: Could not import extension matplotlib.sphinxext.plot_directive (exception: No module named 'matplotlib')

Extension error:
Could not import extension matplotlib.sphinxext.plot_directive (exception: No module named 'matplotlib')

罪魁祸首可以追溯到matplotlib没有被编译,因为mnissing' freetype' C lib:

设置输出

...

requirements
-----
...
BUILDING MATPLOTLIB
            matplotlib: yes [1.4.2]
                python: yes [3.4.0 (default, Apr 11 2014, 13:05:11)  [GCC
                        4.8.2]]
              platform: yes [linux]

REQUIRED DEPENDENCIES AND EXTENSIONS
                 numpy: yes [not found. pip may install it below.]
                   six: yes [six was not found.]
              dateutil: yes [dateutil was not found. It is required for date
                        axis support. pip/easy_install may attempt to
                        install it after matplotlib.]
                  pytz: yes [pytz was not found. pip will attempt to install
                        it after matplotlib.]
               tornado: yes [tornado was not found. It is required for the
                        WebAgg backend. pip/easy_install may attempt to
                        install it after matplotlib.]
             pyparsing: yes [pyparsing was not found. It is required for
                        mathtext support. pip/easy_install may attempt to
                        install it after matplotlib.]
                 pycxx: yes [Official versions of PyCXX are not compatible
                        with matplotlib on Python 3.x, since they lack
                        support for the buffer object.  Using local copy]
                libagg: yes [pkg-config information for 'libagg' could not
                        be found. Using local copy.]
              freetype: no  [The C/C++ header for freetype2 (ft2build.h)
                        could not be found.  You may need to install the
                        development package.]    
    OPTIONAL LATEX DEPENDENCIES
                dvipng: yes [version 1.14]
           ghostscript: yes [version 9.10]
                 latex: yes [version 3.1415926]
               pdftops: no

============================================================================
                        * The following required packages can not be built:
                        * freetype

为了让文档生成通过,我被强制禁止"禁用" plot-directive通过使用./conf.py文件中的以下代码将其模拟为instructed in the RTD FAQ

我已尝试使用virtualenv(有或没有网站包可见性),rtd特定requirements.txt的各种组合,但没有成功。

有人找到了办法吗?

对于那些愿意深入研究这个问题的人来说,这些是一些提示:

2 个答案:

答案 0 :(得分:3)

根据rtfd issue #896,从今天开始,这个问题正式得到解决。 所有必需的依赖项(matplotlib,scipy和numpy)也为python-3安装,因此不再需要进行模拟。

要使用它,请在Advanced settings中做出以下选择:

  • 检查:Install Project: Install your project inside a virtualenv using setup.py install
  • 选择:Python interpreter: CPython 3.x
  • 检查:Use system packages: Give the virtual environment access to the global site-packages dir

...虽然经过一些培训,但很容易编写 python-2/3 兼容代码。

答案 1 :(得分:1)

我有同样的问题,它似乎与RTD解释器有关。我的项目是在Python 3中,我在RTD设置中选择了它并且它失败了(找不到matplotlib)。我能够通过选择Python 2.x解释器(在高级设置中;也允许全局站点包)来解决它,而不是Python 3.x.我使用特定于RTD的需求文件(除了全局站点包之外,它只有我需要的东西:sphinxcontrib-napoleon)。现在可以看到Matplotlib,但现在我遇到了Python 2向后兼容性问题。但至少这个问题已经解决了。