9

我在windows机器上安装了python3.6。打开 my.db 文件时出现以下错误。

我的程序在 python3.6 的 ubuntu16.04 中使用搁置模块创建的 my.db 文件。

In [1]: import shelve

In [2]: db = shelve.open("etc/my.db")
---------------------------------------------------------------------------
error                                    Traceback (most recent call last)
<ipython-input-2-b4828c8ee6e1> in <module>()
----> 1 db = shelve.open("etc/my.db")

c:\Python36\Lib\shelve.py in open(filename, flag, protocol, writeback)
    241     """
    242
--> 243     return DbfilenameShelf(filename, flag, protocol, writeback)

c:\Python36\Lib\shelve.py in __init__(self, filename, flag, protocol, writeback)
    225     def __init__(self, filename, flag='c', protocol=None, writeback=False):
    226         import dbm
--> 227         Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
    228
    229

c:\Python36\Lib\dbm\__init__.py in open(file, flag, mode)
     89     elif result not in _modules:
     90         raise error[0]("db type is {0}, but the module is not "
---> 91                        "available".format(result))
     92     else:
     93         mod = _modules[result]

error: db type is dbm.gnu, but the module is not available

请帮助,如何在 Windows 中安装缺少的模块。

4

1 回答 1

0

正如@alexander-p 所提到的,问题可能出__pycache__在源代码中的文件夹上。

在我的情况下,我确实用一个venv具有相同名称但更新版本的 Python(~3.8~ → 3.9)的新文件夹替换了一个带有虚拟环境的文件夹,并同时使用了 PyCharm(使用了 venv 设置) .

删除所有 Python 缓存(并重新启动 PyCharm 以防万一)解决了这个问题。

你可以这样做:

$ find . -name __pycache__ | xargs rm -Rv

如果该venv文件夹位于放置源代码的同一文件夹内,则最好执行:

$ find . -name __pycache__ | grep -v venv | xargs rm -Rv

所以venv/文件夹内的缓存不会被删除。

于 2022-01-24T18:53:01.400 回答