简体   繁体   English

GDBM 不适用于 Python 3.6 和 anaconda

[英]GDBM doesn't work with Python 3.6 and anaconda

I use Python 3.6 in an anaconda environment.我在 anaconda 环境中使用 Python 3.6。 I installed GDBM with我安装了 GDBM

conda install gdbm

The installation went well, however I can't use dbm.gnu from Python:安装进行得很顺利,但是我无法使用 Python 中的dbm.gnu

ModuleNotFoundError: No module named '_gdbm'

It seams that Python doesn't include the _gdbm module, even if GDBM is actually installed.看起来 Python 不包含_gdbm模块,即使实际安装了 GDBM。

Is this a known problem?这是一个已知问题吗? How can I fix it?我该如何解决?

Thanks!谢谢!

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

This installs the gdbm library for python3, however since apt-get and anaconda are two independent package managers;这将为 python3 安装 gdbm 库,但是因为 apt-get 和 anaconda 是两个独立的包管理器; 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.我们这样做主要是为了获取 .so 共享库,我们将把它放在 anaconda 安装的正确文件夹中。 Next we find the location of the.so file using -接下来我们使用 - 找到 .so 文件的位置

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

Copy this file to the lib-dynload folder of your anaconda installation;将此文件复制到 anaconda 安装的 lib-dynload 文件夹中; 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

Note, that this will only work if the directory the .so was copied to is in python's sys.path .请注意,这仅在.so复制到的目录位于 python 的sys.path中时才有效。 To find the correct directory to copy to, assuming you're inside the activated conda environment, run:要找到要复制到的正确目录,假设您在激活的 conda 环境中,请运行:

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

For example, in my case, the directory was inside the environment path and not in the anaconda main library.例如,在我的例子中,该目录位于环境路径中,而不是在 anaconda 主库中。 ~/anaconda3/envs/myenvname/lib/python3.7/lib-dynload

Now try importing the module in python -现在尝试在 python 中导入模块 -

from _gdbm import *

or testing it from the command line:或者从命令行测试它:

python -m dbm.gnu

This should have fixed your problem.这应该已经解决了您的问题。

Please note, mine is an Ubuntu-16.06 OS and my python version is 3.5.2.请注意,我的是 Ubuntu-16.06 操作系统,我的 python 版本是 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. .so 文件也可以与 python3.6 一起使用,如果不能,您可以尝试安装 python3.6-gdbm,尽管快速搜索 ubuntu 16.04 没有给我任何结果。

Although the question is for Python3, I got here while trying to install gdbm on Python2 so I post my answer as it may be useful for others.虽然问题是针对 Python3 的,但我是在尝试在 Python2 上安装gdbm时来到这里的,所以我发布了我的答案,因为它可能对其他人有用。 The correct command was conda install python-gdbm .正确的命令是conda install python-gdbm Although conda install gdbm went through, the module couldn't be imported. conda install gdbm虽然通过了,但是模块导入不了。 As per here , however, this may not work for Python3.但是,根据此处,这可能不适用于 Python3。

The answer from @stason worked for me with a little modification regarding the destination route for the.so file. @stason 的回答对我有用,只是对 .so 文件的目标路由进行了一些修改。 I copied the files to the lib-dynload folder in the environment to make this work.我将文件复制到环境中的 lib-dynload 文件夹以使其工作。

Instead of: /home/username/anaconda3/lib/python3.X/lib-dynload而不是: /home/username/anaconda3/lib/python3.X/lib-dynload

I used: /home/username/anaconda3/envs/**your_env**/lib/python3.X/lib-dynload我用过:/home/username/anaconda3/envs/**your_env**/lib/python3.X/ /home/username/anaconda3/envs/**your_env**/lib/python3.X/lib-dynload

Doing the same but pointing to this folder worked for me.做同样的事情但指向这个文件夹对我有用。 I hope this helps anyone with the issue.我希望这可以帮助任何人解决这个问题。

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM