由于种种原因,需要在venv 环境下安装 python 3.6版本的 lmdb 库,本来系统自带的 python 是2.7版本的,所以我电脑里有两个版本的 python (2.7 和 3.6),这是背景。
一开始进入venv 虚拟环境:
source venv/bin/activate
然后命令行状态变为:
(venv) shopping1@ubuntu:/usr/local/lib/python3.6/dist-packages$
一开始,输入 pip 命令安装 lmdb:
(venv) shopping1@ubuntu:/usr/local/lib/python3.6/dist-packages$ pip install lmdb
Collecting lmdb
Using cached https://files.pythonhosted.org/packages/86/5c/6ad690daeaa00b328606bf2fa435244a11cc2dd9aedac6ef4d2ef2a21f6e/lmdb-0.96.tar.gz
Building wheels for collected packages: lmdb
Building wheel for lmdb (setup.py) ... error
ERROR: Complete output from command /home/shopping1/venv/bin/python3 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-gpueg0ga/lmdb/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-_1wwwpml --python-tag cp36:
ERROR: py-lmdb: Using bundled liblmdb; override with LMDB_FORCE_SYSTEM=1.
py-lmdb: Using CPython extension; override with LMDB_FORCE_CFFI=1.
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/lmdb
copying lmdb/cffi.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/__main__.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/tool.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/_config.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/__init__.py -> build/lib.linux-x86_64-3.6/lmdb
running build_ext
building 'cpython' extension
creating build/temp.linux-x86_64-3.6
creating build/temp.linux-x86_64-3.6/lmdb
creating build/temp.linux-x86_64-3.6/lib
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Ilib/py-lmdb -Ilib -I/usr/include/python3.6m -I/home/shopping1/venv/include/python3.6m -c lmdb/cpython.c -o build/temp.linux-x86_64-3.6/lmdb/cpython.o -UNDEBUG -w
lmdb/cpython.c:42:10: fatal error: Python.h: No such file or directory
#include "Python.h"
^~~~~~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
----------------------------------------
ERROR: Failed building wheel for lmdb
Running setup.py clean for lmdb
Failed to build lmdb
Installing collected packages: lmdb
Running setup.py install for lmdb ... error
ERROR: Complete output from command /home/shopping1/venv/bin/python3 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-gpueg0ga/lmdb/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-2e9ozbb1/install-record.txt --single-version-externally-managed --compile --install-headers /home/shopping1/venv/include/site/python3.6/lmdb:
ERROR: py-lmdb: Using bundled liblmdb; override with LMDB_FORCE_SYSTEM=1.
py-lmdb: Using CPython extension; override with LMDB_FORCE_CFFI=1.
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/lmdb
copying lmdb/cffi.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/__main__.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/tool.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/_config.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/__init__.py -> build/lib.linux-x86_64-3.6/lmdb
running build_ext
building 'cpython' extension
creating build/temp.linux-x86_64-3.6
creating build/temp.linux-x86_64-3.6/lmdb
creating build/temp.linux-x86_64-3.6/lib
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Ilib/py-lmdb -Ilib -I/usr/include/python3.6m -I/home/shopping1/venv/include/python3.6m -c lmdb/cpython.c -o build/temp.linux-x86_64-3.6/lmdb/cpython.o -UNDEBUG -w
lmdb/cpython.c:42:10: fatal error: Python.h: No such file or directory
#include "Python.h"
^~~~~~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
----------------------------------------
ERROR: Command "/home/shopping1/venv/bin/python3 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-gpueg0ga/lmdb/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-2e9ozbb1/install-record.txt --single-version-externally-managed --compile --install-headers /home/shopping1/venv/include/site/python3.6/lmdb" failed with error code 1 in /tmp/pip-install-gpueg0ga/lmdb/
出现这一堆鬼东西,后来就到了我这个菜鸟的犯错地方了,我输入:
(venv) shopping1@ubuntu:~/rk3399pro_linux/external/rknn-toolkit/packages$ sudo pip install lmdb
The directory '/home/shopping1/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/shopping1/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting lmdb
Downloading https://files.pythonhosted.org/packages/86/5c/6ad690daeaa00b328606bf2fa435244a11cc2dd9aedac6ef4d2ef2a21f6e/lmdb-0.96.tar.gz (869kB)
100% |████████████████████████████████| 870kB 19kB/s
Installing collected packages: lmdb
Running setup.py install for lmdb ... done
Successfully installed lmdb-0.96
发现成功了,瞎JB开心了一下,然后输入 python 进入 python 交互模式,此时还是在 venv 环境下。
(venv) shopping1@ubuntu:~/rk3399pro_linux/external/rknn-toolkit/packages$ python
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import libllvm7
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'libllvm7'
>>> import lmdb
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'lmdb'
>>> quit()
发现不行,这对我打击很大,想了一下,输入 locate lmdb ,打印出来它的安装位置,发现它没有在python 3.6 下面,于是试了一下,此时已经退出了 venv 环境:
shopping1@ubuntu:~/venv/bin$ python
Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lmdb
>>> lmdb.__version__
'0.96'
>>> quit()
当时就日了狗了,无心插柳柳成荫。找了下原因,发现是sudo 命令导致的。可我不加 sudo 也报错,于是就想手动下载 lmdb 安装包,人肉操作。
(venv) shopping1@ubuntu:/usr/local/lib/python3.6/dist-packages$ sudo wget https://files.pythonhosted.org/packages/86/5c/6ad690daeaa00b328606bf2fa435244a11cc2dd9aedac6ef4d2ef2a21f6e/lmdb-0.96.tar.gz
--2019-07-21 19:31:45-- https://files.pythonhosted.org/packages/86/5c/6ad690daeaa00b328606bf2fa435244a11cc2dd9aedac6ef4d2ef2a21f6e/lmdb-0.96.tar.gz
Resolving files.pythonhosted.org (files.pythonhosted.org)... 151.101.77.63, 2a04:4e42:12::319
Connecting to files.pythonhosted.org (files.pythonhosted.org)|151.101.77.63|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 869450 (849K) [binary/octet-stream]
Saving to: ‘lmdb-0.96.tar.gz’
lmdb-0.96.tar.gz 100%[==================>] 849.07K 50.6KB/s in 30s
2019-07-21 19:32:17 (28.5 KB/s) - ‘lmdb-0.96.tar.gz’ saved [869450/869450]
网址是从 pip install lmdb 里面复制的,一开始不加 sudo ,报错权限不够,于是加了这才跑通。然后人肉安装,还是报错了,不过没关系,失败是成功之母,成功已经很近了。
(venv) shopping1@ubuntu:/usr/local/lib/python3.6/dist-packages/lmdb-0.96$ python3 setup.py install
py-lmdb: Using bundled liblmdb; override with LMDB_FORCE_SYSTEM=1.
py-lmdb: Using CPython extension; override with LMDB_FORCE_CFFI=1.
running install
running bdist_egg
running egg_info
writing lmdb.egg-info/PKG-INFO
writing dependency_links to lmdb.egg-info/dependency_links.txt
writing top-level names to lmdb.egg-info/top_level.txt
reading manifest file 'lmdb.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'lmdb/__pycache__'
writing manifest file 'lmdb.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/lmdb
copying lmdb/cffi.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/__main__.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/tool.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/_config.py -> build/lib.linux-x86_64-3.6/lmdb
copying lmdb/__init__.py -> build/lib.linux-x86_64-3.6/lmdb
running build_ext
building 'cpython' extension
creating build/temp.linux-x86_64-3.6
creating build/temp.linux-x86_64-3.6/lmdb
creating build/temp.linux-x86_64-3.6/lib
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Ilib/py-lmdb -Ilib -I/usr/include/python3.6m -I/home/shopping1/venv/include/python3.6m -c lmdb/cpython.c -o build/temp.linux-x86_64-3.6/lmdb/cpython.o -UNDEBUG -w
lmdb/cpython.c:42:10: fatal error: Python.h: No such file or directory
#include "Python.h"
^~~~~~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
虽然还报错,但现在错误已经很明显了, python.h 的锅,上网搜了一下,解法很多,我参考这位前辈的,很清晰
https://www.jianshu.com/p/e6945ac9c4c8 。按照他给的法子,输入:
(venv) shopping1@ubuntu:/usr/local/lib/python3.6/dist-packages$ sudo apt install python3-dev
成功安装后,继续安装 lmdb ,此时不能加 sudo ! ! !
(venv) shopping1@ubuntu:/usr/local/lib/python3.6/dist-packages$ pip install lmdb
Collecting lmdb
Using cached https://files.pythonhosted.org/packages/86/5c/6ad690daeaa00b328606bf2fa435244a11cc2dd9aedac6ef4d2ef2a21f6e/lmdb-0.96.tar.gz
Building wheels for collected packages: lmdb
Building wheel for lmdb (setup.py) ... done
Stored in directory: /home/shopping1/.cache/pip/wheels/f0/78/2f/3446719fa7623e9aa8331af56a071a43ebc0951b27a03d6e11
Successfully built lmdb
Installing collected packages: lmdb
Successfully installed lmdb-0.96
终于好了,但是这口气还不能松,验证:
(venv) shopping1@ubuntu:/usr/local/lib/python3.6/dist-packages$ python
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lmdb
>>> lmdb.__version__
'0.96'
>>> quit()
终于好了,折磨我的BUG终于搞死了。
总结:碰见BUG还是要多想多试多动手,慌是没用的。一切BUG都是纸老虎!此外,对于一些模糊的知识点要吃透,不然害的自己。