Python Error: "AttributeError: __enter__" - Stack Overflow most recent 30 from stackoverflow.com 2026-04-14T08:52:37Z https://stackoverflow.com/feeds/question/53564755 https://creativecommons.org/licenses/by-sa/4.0/rdf https://stackoverflow.com/q/53564755 13 Python Error: "AttributeError: __enter__" Caio Alexandre https://stackoverflow.com/users/10729327 2018-11-30T20:52:06Z 2021-10-13T17:05:24Z <p>So, I can't load my json file and I don't know why, can anyone explain what I'm doing wrong?</p> <pre><code>async def give(msg, arg): if arg[0] == prefix + "dailycase": with open("commands/databases/cases.json", "r") as d: data = json.load(d) </code></pre> <p>For some reason I'm getting this error:</p> <pre><code> with open("commands/databases/cases.json", "r") as d: AttributeError: __enter__ </code></pre> https://stackoverflow.com/questions/53564755/-/53564918#53564918 22 Answer by Lukas Graf for Python Error: "AttributeError: __enter__" Lukas Graf https://stackoverflow.com/users/1599111 2018-11-30T21:06:58Z 2018-11-30T21:06:58Z <p>Most likely, you have reassigned the Python <a href="https://docs.python.org/3/library/functions.html#open" rel="noreferrer">builtin <code>open</code> function</a> to something else in your code (<em>there's almost no other plausible way this exception could be explained</em>).</p> <p>The <code>with</code> statement will then attempt to use it as a <a href="https://docs.python.org/3/reference/datamodel.html#context-managers" rel="noreferrer">context manager</a>, and will try to call its <code>__enter__</code> method when first entering the <code>with</code> block. This then leads to the error message you're seeing because your object called <code>open</code>, whatever it is, doesn't have an <code>__enter__</code> method.</p> <hr> <p>Look for places in your Python module where you are re-assigning <code>open</code>. The most obvious ones are:</p> <ul> <li>A function in the global scope, like <code>def open(..)</code></li> <li>Direct reassignment using <code>open =</code></li> <li>Imports like <code>from foo import open</code> or <code>import something as open</code></li> </ul> <p>The <strong>function</strong> is the most likely suspect, because it seems your <code>open</code> is actually a callable.</p> <p>To aid you finding what object <code>open</code> was accidentally bound to, you can also try to</p> <pre><code>print('open is assigned to %r' % open) </code></pre> <p>immediately before your <code>with</code> statement. If it doesn't say <code>&lt;built-in function open&gt;</code>, you've found your culprit. </p> https://stackoverflow.com/questions/53564755/-/64061182#64061182 21 Answer by a20 for Python Error: "AttributeError: __enter__" a20 https://stackoverflow.com/users/163382 2020-09-25T09:11:36Z 2020-11-29T05:00:51Z <p>I got this error at this line:</p> <pre><code>with concurrent.futures.ProcessPoolExecutor as executor: </code></pre> <p><strong>missing brackets</strong> was the issue</p> <pre><code>with concurrent.futures.ProcessPoolExecutor() as executor: </code></pre> https://stackoverflow.com/questions/53564755/-/65058250#65058250 2 Answer by Chris Pollitt for Python Error: "AttributeError: __enter__" Chris Pollitt https://stackoverflow.com/users/4719181 2020-11-29T08:23:55Z 2020-11-29T08:23:55Z <p>In my case, I was intentionally defining a custom <strong>with</strong> function called <em>stopwatch</em></p> <pre><code>with stopwatch('upload %d bytes' % len(data)): ...code... </code></pre> <p>And so had to add:</p> <pre><code>import contextlib </code></pre> <p>and prefix the custom function definition as follows:</p> <pre><code>@contextlib.contextmanager def stopwatch(message): ...code... </code></pre> https://stackoverflow.com/questions/53564755/-/69559564#69559564 -1 Answer by pbatey for Python Error: "AttributeError: __enter__" pbatey https://stackoverflow.com/users/2683294 2021-10-13T17:05:24Z 2021-10-13T17:05:24Z <p>My problem was that I was expecting <code>os.open</code> to work like the built-in <code>open</code>...</p> <p>This results in <code>AttributeError: __enter__</code></p> <pre><code>import os with os.open('out.txt', os.CREAT) as f: f.write('hello world') </code></pre> <p>This does not</p> <pre><code>with open('out.txt', 'w') as f: f.write('hello world') </code></pre> <p>I suppose it would be easy enough to cause the OP problem with <code>from os import open</code>.</p>