0

Following is my code,

import shelve

sd = shelve.open("session.data")

When I try the same code in IDLE , I am not getting any error. But when running the script with this code, I am getting the following error,

Traceback (most recent call last):
  File "try.py", line 3, in <module>
    sd = shelve.open("session.data")
AttributeError: 'module' object has no attribute 'open'

1 Answers1

2

You imported a different module shelve, one that masks the standard library version.

Do:

import shelve
print(shelve.__file__)

and move that file aside, rename it, or delete it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I tried this but for me it gave the error "import shelve my_shelf = shelve.open(filename,'n') # 'n' for new AttributeError: module 'shelve' has no attribute 'open'" – R. Cox Sep 27 '19 at 09:29
  • @R.Cox: did you try to print the `__fie__` attribute **instead** of using `shelve.open()`? – Martijn Pieters Sep 27 '19 at 10:32
  • I ran "print(shelve.__file__)". It replied "NameError: name 'shelve' is not defined" – R. Cox Sep 27 '19 at 10:34
  • @R.Cox: I didn't say you needed to remove the `import shelve` line, I assumed that you'd still leave that in, just like I show in my answer. Also see the canonical post I closed this question as a duplicate of for more information. – Martijn Pieters Sep 27 '19 at 10:36
  • When I run "import shelve", press enter, and then run "print(shelve.__file__)" it replies with a location ending "Continuum\Anaconda3\lib\shelve.py" – R. Cox Sep 27 '19 at 10:36
  • @R.Cox: and that's with the same file that would have produced the error, or did you do that in your interactive interpreter? Take into account that when you run your script that the `sys.path` directories can be different and so `import shelve` can find something else. That's the issue we are trying to solve here. – Martijn Pieters Sep 27 '19 at 10:57
  • Thanks. I've justgot it working. I went to the location in file explorer and changed the name of "shelve.py". – R. Cox Sep 27 '19 at 11:04
  • But now, when I run "import shelve", I get "AttributeError: module 'shelve' has no attribute 'open'". This seems like an intermittent fault. – R. Cox Oct 03 '19 at 10:34
  • @R.Cox you have another file named `shelve.py` or a directory named `shelve` in one of your `sys.path` directories. Usually this is the current working directory. When you have that error, insert a `print(shelve)` right after importing to see what was imported. – Martijn Pieters Oct 03 '19 at 10:56
  • Yeah, when it was working before I did find where my directory was. Unfortunately I didn't make a note of that and I can't find it now. When I run "print(shelve)" I again get "NameError: name 'shelve' is not defined". When I run "import shelve", I get "AttributeError: module 'shelve' has no attribute 'open'". – R. Cox Oct 03 '19 at 11:09
  • I used "print ('\n'.join(sys.path))" to find my directory. Then I changed the file name. It's still giving the same error. – R. Cox Oct 03 '19 at 12:04
  • @R.Cox: you need to print **before** you use `shelve.open`. Put the statement on the line before you use `shelve.open()` and the information will shop up just before the `Traceback ...` line. You are importing a module that doesn't have that attribute instead of the standard library version, or doing something else to the name `shelve` (like assigning something to the name). – Martijn Pieters Oct 03 '19 at 12:28
  • I don't use shelve.open. I found 2 files called "shelve.pyc" and "shelve.py" and changed both their names. Then I turned Spyder off and on again. Now when I run "import shelve" I now get "ModuleNotFoundError: No module named 'shelve'" – R. Cox Oct 03 '19 at 13:33
  • @R.Cox: you didn't tell me that; something you import can also be using `shelve.open`. Did you change `shelve.py` in the `lib/pythonX.Y/` directory perhaps? *That's the actual `shelve` module in the standard library*. Put those files back again, and then when the error happens, look at the *full traceback*. Look at the post this is closed as a duplicate of, and then look carefully at all the files named in the traceback to see if there is something in there that is *also* named `shelve`. Otherwise, use a debugger to see what the `shelve` object actually is pointing to. – Martijn Pieters Oct 03 '19 at 13:53
  • Thanks, I've put them back. What I've done is I've called my script shelve. – R. Cox Oct 03 '19 at 13:59
  • I changed the name ofmy script and now I get "ModuleNotFoundError: No module named 'shelve'" – R. Cox Oct 03 '19 at 14:10