問(wèn)題描述
我在 anaconda 環(huán)境中使用 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'
看起來(lái) Python 不包含 _gdbm
模塊,即使實(shí)際安裝了 GDBM.
It seams that Python doesn't include the _gdbm
module, even if GDBM is actually installed.
這是一個(gè)已知問(wèn)題嗎?我該如何解決?
Is this a known problem? How can I fix it?
謝謝!
推薦答案
我也遇到過(guò)這個(gè)問(wèn)題.這可能不是理想的方式,但它確實(shí)有效.我做了以下事情來(lái)解決這個(gè)問(wèn)題 -
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
這會(huì)為 python3 安裝 gdbm 庫(kù),但是由于 apt-get 和 anaconda 是兩個(gè)獨(dú)立的包管理器;這不會(huì)解決你的問(wèn)題.我們這樣做主要是為了獲得 .so 共享庫(kù),我們將把它放在我們安裝的 anaconda 的正確文件夾中.接下來(lái)我們使用 -
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
將此文件復(fù)制到您安裝的 anaconda 的 lib-dynload 文件夾中;對(duì)我來(lái)說(shuō)這是 -
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
被復(fù)制到的目錄位于 python 的 sys.path
中時(shí)才有效.要找到要復(fù)制到的正確目錄,假設(shè)您在激活的 conda 環(huán)境中,請(qǐng)運(yùn)行:
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]'
例如,在我的例子中,該目錄位于環(huán)境路徑中,而不是在 anaconda 主庫(kù)中.~/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
現(xiàn)在嘗試在 python 中導(dǎo)入模塊 -
Now try importing the module in python -
from _gdbm import *
或從命令行測(cè)試它:
python -m dbm.gnu
這應(yīng)該解決了你的問(wèn)題.
This should have fixed your problem.
請(qǐng)注意,我的是 Ubuntu-16.06 操作系統(tǒng),我的 python 版本是 3.5.2..so 文件也可以與 python3.6 一起使用,如果不是,您可以嘗試安裝 python3.6-gdbm,盡管快速搜索 ubuntu 16.04 并沒(méi)有給我任何結(jié)果.
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.
這篇關(guān)于GDBM 不適用于 Python 3.6 和 anaconda的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!