我在Windows机器中安装了python3.6。并在打开我的.db文件时出现以下错误。
my.db 我的程序在 Ubuntu16.04 中使用 python3.6 中创建的文件,使用 shelve 模块。
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:Python36Libshelve.py in open(filename, flag, protocol, writeback)
241 """
242
--> 243 return DbfilenameShelf(filename, flag, protocol, writeback)
c:Python36Libshelve.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:Python36Libdbm__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
请帮忙,如何在窗口中安装缺少的模块。
正如 @alexander-p 所述,问题可能是源代码中__pycache__
文件夹。
就我而言,我确实用一个同名但较新版本的 Python (~3.8~ → 3.9( 的新文件夹替换了一个带有虚拟环境的venv
文件夹,并同时使用 PyCharm(使用 venv 设置(。
删除所有Python缓存(并重新启动PyCharm以防万一(解决了这个问题。
您可以使用以下方法执行此操作:
$ find . -name __pycache__ | xargs rm -Rv
如果venv
文件夹位于放置源代码的同一文件夹中,则最好执行:
$ find . -name __pycache__ | grep -v venv | xargs rm -Rv
因此,不会删除venv/
文件夹中的缓存。
我发现修复此错误的唯一方法是在升级到python3.10后在我的Ubuntu上安装一个缺少的组件
sudo apt-get install python3.10-gdbm