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__
Caio Alexandre
  • 143
  • 1
  • 1
  • 4
  • 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 '18 at 20:53
  • It's not quite a duplicate, but is relevant: https://stackoverflow.com/questions/1984325/explaining-pythons-enter-and-exit – Green Cloak Guy Nov 30 '18 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 '18 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`). – ShadowRanger Nov 30 '18 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. – ShadowRanger Nov 30 '18 at 20:58
  • @ShadowRanger you're right. did not realize, removed comment – Ryan Haining Nov 30 '18 at 21:08

4 Answers4

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.

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
  • 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! – Caio Alexandre Nov 30 '18 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 '18 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. – Under-qualified NASA Intern Aug 02 '21 at 16:16
  • Im also getting the same error on this basic code and have no clue why https://snippet.host/pbwp – Grimeire Apr 20 '22 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 '22 at 20:06
  • I found out after commenting that what ever the issue is when i run the exact same code i provided on another machine it works as expected. Ill look into your code and test to see if it runs on both machines. Thanks. – Grimeire Apr 21 '22 at 08:25
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:
a20
  • 5,495
  • 2
  • 30
  • 27
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.

pbatey
  • 1,234
  • 13
  • 13