<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto">Import matplotlib.pyplot fails on MacOSX</p>
<p dir="auto"><strong>Code for reproduction</strong></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span></pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;/Users/Apollo/PycharmProjects/CT_registration/main.py&quot;, line 114, in &lt;module&gt;
    moving_npa=fixed(sitk.GetArrayViewFromImage(movingImage)))
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/ipywidgets/widgets/interaction.py&quot;, line 531, in __call__
    show_inline_matplotlib_plots()
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/ipywidgets/widgets/interaction.py&quot;, line 52, in show_inline_matplotlib_plots
    from ipykernel.pylab.backend_inline import flush_figures
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/ipykernel/pylab/backend_inline.py&quot;, line 9, in &lt;module&gt;
    from matplotlib.backends.backend_agg import new_figure_manager, FigureCanvasAgg # analysis: ignore
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py&quot;, line 32, in &lt;module&gt;
    from matplotlib.backend_bases import (
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/backend_bases.py&quot;, line 53, in &lt;module&gt;
    from matplotlib import (
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/textpath.py&quot;, line 17, in &lt;module&gt;
    import matplotlib.font_manager as font_manager
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py&quot;, line 1469, in &lt;module&gt;
    _rebuild()
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py&quot;, line 1450, in _rebuild
    fontManager = FontManager()
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py&quot;, line 1086, in __init__
    self.ttflist = createFontList(self.ttffiles)
  File &quot;/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py&quot;, line 587, in createFontList
    font = ft2font.FT2Font(fpath)
TypeError: First argument must be a path or file object reading bytes

Process finished with exit code 1"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "/Users/Apollo/PycharmProjects/CT_registration/main.py", line 114, in &lt;module&gt;
    moving_npa=fixed(sitk.GetArrayViewFromImage(movingImage)))
  File "/Users/Apollo/venv/lib/python2.7/site-packages/ipywidgets/widgets/interaction.py", line 531, in __call__
    show_inline_matplotlib_plots()
  File "/Users/Apollo/venv/lib/python2.7/site-packages/ipywidgets/widgets/interaction.py", line 52, in show_inline_matplotlib_plots
    from ipykernel.pylab.backend_inline import flush_figures
  File "/Users/Apollo/venv/lib/python2.7/site-packages/ipykernel/pylab/backend_inline.py", line 9, in &lt;module&gt;
    from matplotlib.backends.backend_agg import new_figure_manager, FigureCanvasAgg # analysis: ignore
  File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 32, in &lt;module&gt;
    from matplotlib.backend_bases import (
  File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 53, in &lt;module&gt;
    from matplotlib import (
  File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/textpath.py", line 17, in &lt;module&gt;
    import matplotlib.font_manager as font_manager
  File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1469, in &lt;module&gt;
    _rebuild()
  File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1450, in _rebuild
    fontManager = FontManager()
  File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1086, in __init__
    self.ttflist = createFontList(self.ttffiles)
  File "/Users/Apollo/venv/lib/python2.7/site-packages/matplotlib/font_manager.py", line 587, in createFontList
    font = ft2font.FT2Font(fpath)
TypeError: First argument must be a path or file object reading bytes

Process finished with exit code 1
</code></pre></div>
<p dir="auto"><strong>Expected outcome</strong></p>


<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system: Mac OS X Lion 10.7.5</li>
<li>Matplotlib version: 2.2.2</li>
<li>Matplotlib backend: MacOSX</li>
<li>Python version: 2.7.1</li>
<li>Jupyter version: jupyter-client==5.2.3 jupyter-core==4.4.0</li>
<li>Other libraries: SimpleITK, ipywidgets</li>
</ul>
<p dir="auto">I installed matplotlib from source. I am on a virtual environment.</p>
<h3 dir="auto">Bug report</h3>
<p dir="auto">As originally reported in <a href="http://gnats.netbsd.org/53425" rel="nofollow">http://gnats.netbsd.org/53425</a> , with matplotlib version 2.2.2, attempting to <code class="notranslate">import matplotlib.pyplot</code> on a NetBSD system fails with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/pkg/lib/python2.7/site-packages/matplotlib/pyplot.py&quot;, line 31, in &lt;module&gt;
    import matplotlib.colorbar
  File &quot;/usr/pkg/lib/python2.7/site-packages/matplotlib/colorbar.py&quot;, line 36, in &lt;module&gt;
    import matplotlib.contour as contour
  File &quot;/usr/pkg/lib/python2.7/site-packages/matplotlib/contour.py&quot;, line 20, in &lt;module&gt;
    import matplotlib.font_manager as font_manager
  File &quot;/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py&quot;, line 1469, in &lt;module&gt;
    _rebuild()
  File &quot;/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py&quot;, line 1450, in _rebuild
    fontManager = FontManager()
  File &quot;/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py&quot;, line 1086, in __init__
    self.ttflist = createFontList(self.ttffiles)
  File &quot;/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py&quot;, line 587, in createFontList
    font = ft2font.FT2Font(fpath)
TypeError: First argument must be a path or file object reading bytes"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/pyplot.py", line 31, in &lt;module&gt;
    import matplotlib.colorbar
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in &lt;module&gt;
    import matplotlib.contour as contour
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/contour.py", line 20, in &lt;module&gt;
    import matplotlib.font_manager as font_manager
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1469, in &lt;module&gt;
    _rebuild()
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1450, in _rebuild
    fontManager = FontManager()
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1086, in __init__
    self.ttflist = createFontList(self.ttffiles)
  File "/usr/pkg/lib/python2.7/site-packages/matplotlib/font_manager.py", line 587, in createFontList
    font = ft2font.FT2Font(fpath)
TypeError: First argument must be a path or file object reading bytes
</code></pre></div>
<p dir="auto">I tracked down the cause of this to <code class="notranslate">mpl_PyFile_Dup()</code> attempting to call the "flush" method of a file object that is open only for reading.  Python implements this by calling <code class="notranslate">fflush()</code>, and calling <code class="notranslate">fflush()</code> on a file descriptor not open for writing is treated as an error on BSD systems (but silently ignored on Linux). The <code class="notranslate">fflush()</code> call returns -1 with errno=EBADF, and this ultimately gets translated into the rather unhelpful error message "TypeError: First argument must be a path or file object reading bytes" show above.</p>
<p dir="auto">The call chain leading to the error is PyFT2Font_init() -&gt; convert_open_args() -&gt; mpl_PyFile_Dup().</p>
<p dir="auto"><strong>Code for reproduction</strong></p>
<p dir="auto">On a BSD system, run</p>
<p dir="auto">python2.7 -c 'import matplotlib.pyplot'</p>
<p dir="auto"><strong>Actual outcome</strong></p>
<p dir="auto">The stack trace shown above is printed.</p>
<p dir="auto"><strong>Expected outcome</strong></p>
<p dir="auto">The command silently exits with a success status.</p>
<p dir="auto"><strong>Matplotlib version</strong></p>
<ul dir="auto">
<li>Operating system: NetBSD 7.1_STABLE</li>
<li>Matplotlib version:  2.2.2</li>
<li>Matplotlib backend (<code class="notranslate">print(matplotlib.get_backend())</code>): GTKAgg</li>
<li>Python version:  2.7.15</li>
<li>Jupyter version (if applicable): N/A</li>
<li>Other libraries: N/A</li>
</ul>
<p dir="auto">matplotlib was installed via pkgsrc.</p>
<p dir="auto">I have a patch which I will attempt to submit as a pull request.</p>