Unable to mock open, even when using the example from the documentation
<p>I've copied and pasted the following code directly from the Python <a href="https://docs.python.org/3/library/unittest.mock.html#mock-open" rel="nofollow">mock docs</a>:</p>

<pre><code>from unittest.mock import patch, mock_open

with patch('__main__.open', mock_open(read_data='bibble')) as m:
    with open('foo') as h:
        result = h.read()

m.assert_called_once_with('foo')
assert result == 'bibble'
</code></pre>

<p>When I run this I get the following error:</p>

<pre><code>AttributeError: &lt;module '__main__' from 'path/to/file'&gt; does not have the attribute 'open'
</code></pre>

<p>Given that this is the example given in the documentation, I'm not sure where else to turn. I'm running Python 3.4.5.</p>

<p>I figured it out.</p>

<p><code>open</code> is a builtin, so I needed to patch <code>builtins.open</code>, not <code>__main__.open</code>.</p>

<p>Skipped over this info in the docs.</p>

python python-3.x io mocking