GDBM 不适用于 Python 3.6 和 anaconda

2022-01-10 00:00:00 python python-3.x anaconda dbm gdbm

问题描述

我在 anaconda 环境中使用 Python 3.6.我用

I use Python 3.6 in an anaconda environment. I installed GDBM with

conda install gdbm

安装很顺利,但是我不能使用 Python 中的 dbm.gnu:

The installation went well, however I can't use dbm.gnu from Python:

ModuleNotFoundError: No module named '_gdbm'

看起来 Python 不包含 _gdbm 模块,即使实际安装了 GDBM.

It seams that Python doesn't include the _gdbm module, even if GDBM is actually installed.

这是一个已知问题吗?我该如何解决?

Is this a known problem? How can I fix it?

谢谢!


解决方案

我也遇到过这个问题.这可能不是理想的方式,但它确实有效.我做了以下事情来解决这个问题 -

I faced this issue as well. This is probably not the ideal way, but it works. I did the following to resolve this -

sudo apt-get install python3-gdbm

这会为 python3 安装 gdbm 库,但是由于 apt-get 和 anaconda 是两个独立的包管理器;这不会解决你的问题.我们这样做主要是为了获得 .so 共享库,我们将把它放在我们安装的 anaconda 的正确文件夹中.接下来我们使用 -

This installs the gdbm library for python3, however since apt-get and anaconda are two independent package managers; this isn't going to solve your problem. We primarily do this to get a hold of the .so shared library which we will place in the right folder in our anaconda installation. Next we find the location of the .so file using -

dpkg -L python3-gdbm

这给了我们以下输出 -

This gives us the following output -

/.
/usr
/usr/lib
/usr/lib/python3.5
/usr/lib/python3.5/lib-dynload
/usr/lib/python3.5/lib-dynload/_gdbm.cpython-35m-x86_64-linux-gnu.so
/usr/share
/usr/share/doc
/usr/share/doc/python3-gdbm
/usr/share/doc/python3-gdbm/copyright
/usr/share/doc/python3-gdbm/changelog.Debian.gz
/usr/share/doc/python3-gdbm/README.Debian

我们需要的文件在这里 -

The file we require is here -

/usr/lib/python3.5/lib-dynload/_gdbm.cpython-35m-x86_64-linux-gnu.so

将此文件复制到您安装的 anaconda 的 lib-dynload 文件夹中;对我来说这是 -

Copy this file to the lib-dynload folder of your anaconda installation; for me this was -

cp /usr/lib/python3.5/lib-dynload/_gdbm.cpython-35m-x86_64-linux-gnu.so /home/username/anaconda3/lib/python3.5/lib-dynload

注意,这只有在 .so 被复制到的目录位于 python 的 sys.path 中时才有效.要找到要复制到的正确目录,假设您在激活的 conda 环境中,请运行:

Note, that this will only work if the directory the .so was copied to is in python's sys.path. To find the correct directory to copy to, assuming you're inside the activated conda environment, run:

python -c 'import sys; [print(x) for x in sys.path if "lib-dynload" in x]'

例如,在我的例子中,该目录位于环境路径中,而不是在 anaconda 主库中.~/anaconda3/envs/myenvname/lib/python3.7/lib-dynload

For example, in my case, the directory was inside the environment path and not in the anaconda main library. ~/anaconda3/envs/myenvname/lib/python3.7/lib-dynload

现在尝试在 python 中导入模块 -

Now try importing the module in python -

from _gdbm import *

或从命令行测试它:

python -m dbm.gnu

这应该解决了你的问题.

This should have fixed your problem.

请注意,我的是 Ubuntu-16.06 操作系统,我的 python 版本是 3.5.2..so 文件也可以与 python3.6 一起使用,如果不是,您可以尝试安装 python3.6-gdbm,尽管快速搜索 ubuntu 16.04 并没有给我任何结果.

Please note, mine is an Ubuntu-16.06 OS and my python version is 3.5.2. The .so file may work with python3.6 as well, if not you can try installing python3.6-gdbm, although a quick search for ubuntu 16.04 didn't give me any results.

相关文章