12

So, I can't load my json file and I don't know why, can anyone explain what I'm doing wrong?

async def give(msg, arg):
    if arg[0] == prefix + "dailycase":
                with open("commands/databases/cases.json", "r") as d:
                     data = json.load(d)

For some reason I'm getting this error:

    with open("commands/databases/cases.json", "r") as d:
AttributeError: __enter__
6
  • 6
    Do you have the builtin open defined to something else in your code? Either by assignment (open =) or by importing it from somewhere (from place import open)?
    – Lukas Graf
    Nov 30, 2018 at 20:53
  • It's not quite a duplicate, but is relevant: stackoverflow.com/questions/1984325/… Nov 30, 2018 at 20:54
  • To test @LukasGraf 's theory: print(open.__doc__) ... if you don't see a massive blob of internal python text, starting with "Open file and return a stream", then his theory is correct.
    – JacobIRR
    Nov 30, 2018 at 20:55
  • 2
    @JacobIRR: You can confirm if it's the built-in open by just doing import io, and testing open is io.open (on Python 3, the built-in open aliases io.open). Nov 30, 2018 at 20:57
  • @RyanHaining: Not necessarily; __enter__ isn't looked up until the item in question is fully constructed; odds are the open call succeeded, it just referenced the wrong open, but once it succeeds, that wrong open isn't on the call stack anymore. Nov 30, 2018 at 20:58

4 Answers 4

18

Most likely, you have reassigned the Python builtin open function to something else in your code (there's almost no other plausible way this exception could be explained).

The with statement will then attempt to use it as a context manager, and will try to call its __enter__ method when first entering the with block. This then leads to the error message you're seeing because your object called open, whatever it is, doesn't have an __enter__ method.


Look for places in your Python module where you are re-assigning open. The most obvious ones are:

  • A function in the global scope, like def open(..)
  • Direct reassignment using open =
  • Imports like from foo import open or import something as open

The function is the most likely suspect, because it seems your open is actually a callable.

To aid you finding what object open was accidentally bound to, you can also try to

print('open is assigned to %r' % open)

immediately before your with statement. If it doesn't say <built-in function open>, you've found your culprit.

6
  • 1
    I double checked all my file's script to see what I did wrong, and nope, I haven't ant variable or import named "open", but I did a mistake and I saw I created a function named "open" and I did not realize. Thank you for the support and sorry about that! Nov 30, 2018 at 21:25
  • You're welcome, glad I could help - shadowing a builtin name is a gotcha that tripped up most of us at one time or another ;-)
    – Lukas Graf
    Nov 30, 2018 at 21:27
  • Also check python version. OP's syntax is only supported in 3.8+ My solution below has the python 3.6 syntax. Aug 2, 2021 at 16:16
  • Im also getting the same error on this basic code and have no clue why snippet.host/pbwp
    – Grimeire
    Apr 20, 2022 at 16:17
  • 1
    @Grimeire requests.get is not a context manager, so you can't use it in a with statement. But requests.Session() is - that's probably what you meant to use. So you could do with requests.Session() as session: r = session.get(dl_link, stream=True, headers=headers)
    – Lukas Graf
    Apr 20, 2022 at 20:06
18

I got this error at this line:

with concurrent.futures.ProcessPoolExecutor as executor:

missing brackets was the issue

with concurrent.futures.ProcessPoolExecutor() as executor:
0

In my case, I was intentionally defining a custom with function called stopwatch

with stopwatch('upload %d bytes' % len(data)):
    ...code...

And so had to add:

import contextlib

and prefix the custom function definition as follows:

@contextlib.contextmanager
def stopwatch(message):
    ...code...
-1

My problem was that I was expecting os.open to work like the built-in open...

This results in AttributeError: __enter__

import os 
with os.open('out.txt', os.CREAT) as f:
  f.write('hello world')

This does not

with open('out.txt', 'w') as f:
  f.write('hello world')

I suppose it would be easy enough to cause the OP problem with from os import open.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.