問題描述
我在 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.
這篇關于GDBM 不適用于 Python 3.6 和 anaconda的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!