Python Error: "AttributeError: __enter__" - Stack Overflowmost recent 30 from stackoverflow.com2026-04-14T08:52:37Zhttps://stackoverflow.com/feeds/question/53564755https://creativecommons.org/licenses/by-sa/4.0/rdfhttps://stackoverflow.com/q/5356475513Python Error: "AttributeError: __enter__"Caio Alexandrehttps://stackoverflow.com/users/107293272018-11-30T20:52:06Z2021-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#5356491822Answer by Lukas Graf for Python Error: "AttributeError: __enter__"Lukas Grafhttps://stackoverflow.com/users/15991112018-11-30T21:06:58Z2018-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><built-in function open></code>, you've found your culprit. </p>
https://stackoverflow.com/questions/53564755/-/64061182#6406118221Answer by a20 for Python Error: "AttributeError: __enter__"a20https://stackoverflow.com/users/1633822020-09-25T09:11:36Z2020-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#650582502Answer by Chris Pollitt for Python Error: "AttributeError: __enter__"Chris Pollitthttps://stackoverflow.com/users/47191812020-11-29T08:23:55Z2020-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-1Answer by pbatey for Python Error: "AttributeError: __enter__"pbateyhttps://stackoverflow.com/users/26832942021-10-13T17:05:24Z2021-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>