2019年7月11日 / 1,437次阅读 / Last Modified 2019年7月11日
安装配置
在Ubuntu桌面版中编译安装Python3后,在使用pip3时,出现了一个No module named 'lsb_release'的问题。确认在CentOS7中没有这个问题,看来此问题跟Linux发行版本有关系。本文介绍如何解决这个问题。
在使用pip3时,出现下列错误提示:
$ pip3 list
Package Version
---------- -------
pip 19.0.3
setuptools 40.8.0
Traceback (most recent call last):
File "/usr/bin/pip3", line 10, in
sys.exit(main())
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_internal/__init__.py", line 78, in main
return command.main(cmd_args)
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 228, in main
timeout=min(5, options.timeout)
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 93, in _build_session
insecure_hosts=options.trusted_hosts,
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_internal/download.py", line 344, in __init__
self.headers["User-Agent"] = user_agent()
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_internal/download.py", line 108, in user_agent
zip(["name", "version", "id"], distro.linux_distribution()),
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 120, in linux_distribution
return _distro.linux_distribution(full_distribution_name)
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 675, in linux_distribution
self.version(),
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 735, in version
self.lsb_release_attr('release'),
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 892, in lsb_release_attr
return self._lsb_release_info.get(attribute, '')
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 550, in __get__
ret = obj.__dict__[self._fname] = self._f(obj)
File "/usr/local/python-3.7/lib/python3.7/site-packages/pip/_vendor/distro.py", line 998, in _lsb_release_info
stdout = subprocess.check_output(cmd, stderr=devnull)
File "/usr/local/python-3.7/lib/python3.7/subprocess.py", line 395, in check_output
**kwargs).stdout
File "/usr/local/python-3.7/lib/python3.7/subprocess.py", line 487, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.
注意看最后一行的显示,subprocess模块在执行 lsb_release -a 命令的时候,返回了一个非0的值。非0返回值表示此命令执行失败了。
为了搞清楚为啥 lsb_release -a 命令会执行失败,我单独做了一次执行:
$ lsb_release -a
Traceback (most recent call last):
File "/usr/bin/lsb_release", line 25, in
import lsb_release
ModuleNotFoundError: No module named 'lsb_release'
还是看最后一行,No module named ‘lsb_release’ 出现了。而且,我还看到了 import lsb_release
这一行,这说明 lsb_release 是一个Python程序!
lsb_release 命令是一个python程序,代码不长,我们可以看看这个程序的源码:
$ cat /usr/bin/lsb_release
#!/usr/bin/python3 -Es
# lsb_release command for Debian
# (C) 2005-10 Chris Lawrence
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
from optparse import OptionParser
import sys
import os
import re
import lsb_release
def main():
parser = OptionParser()
parser.add_option('-v', '--version', dest='version', action='store_true',
default=False,
help="show LSB modules this system supports")
parser.add_option('-i', '--id', dest='id', action='store_true',
default=False,
help="show distributor ID")
parser.add_option('-d', '--description', dest='description',
default=False, action='store_true',
help="show description of this distribution")
parser.add_option('-r', '--release', dest='release',
default=False, action='store_true',
help="show release number of this distribution")
parser.add_option('-c', '--codename', dest='codename',
default=False, action='store_true',
help="show code name of this distribution")
parser.add_option('-a', '--all', dest='all',
default=False, action='store_true',
help="show all of the above information")
parser.add_option('-s', '--short', dest='short',
action='store_true', default=False,
help="show requested information in short format")
(options, args) = parser.parse_args()
if args:
parser.error("No arguments are permitted")
short = (options.short)
none = not (options.all or options.version or options.id or
options.description or options.codename or options.release)
distinfo = lsb_release.get_distro_information()
if none or options.all or options.version:
verinfo = lsb_release.check_modules_installed()
if not verinfo:
print("No LSB modules are available.", file=sys.stderr)
elif short:
print(':'.join(verinfo))
else:
print('LSB Version:\t' + ':'.join(verinfo))
if options.id or options.all:
if short:
print(distinfo.get('ID', 'n/a'))
else:
print('Distributor ID:\t%s' % distinfo.get('ID', 'n/a'))
if options.description or options.all:
if short:
print(distinfo.get('DESCRIPTION', 'n/a'))
else:
print('Description:\t%s' % distinfo.get('DESCRIPTION', 'n/a'))
if options.release or options.all:
if short:
print(distinfo.get('RELEASE', 'n/a'))
else:
print('Release:\t%s' % distinfo.get('RELEASE', 'n/a'))
if options.codename or options.all:
if short:
print(distinfo.get('CODENAME', 'n/a'))
else:
print('Codename:\t%s' % distinfo.get('CODENAME', 'n/a'))
if __name__ == '__main__':
main()
看到 import lsb_release 这一行了吧!出现 No module named ‘lsb_release’ 的问题,显然是这个程序找不到 lsb_release 模块了。
分析:Ubuntu系统自带Python3,而出问题的是我们自己编译安装的最新版的Python3,解决问题的方法,就是寻找Ubunbu自带的Python3使用的 lsb_release 模块,然后将这个模块copy到我们自己编译安装的Python3的lib目录下。同学们可以回头看看上面subprocess模块的错误提示,可以看到我们自己编译安装的Python3的lib路径是:/usr/local/python-3.7/lib/python3.7/,而这个路径下,确实没有 lsb_release 模块。
寻找Ubuntu系统自带的 lsb_release 模块
$ sudo find / -name lsb_release.py
[sudo] password for xinlin:
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/lib/python2.7/dist-packages/lsb_release.py
/usr/lib/python3/dist-packages/lsb_release.py
/usr/share/pyshared/lsb_release.py
就是这个文件:/usr/lib/python3/dist-packages/lsb_release.py
copy到正确路径
将找到的这个文件,copy到正确路径:
$ sudo cp /usr/lib/python3/dist-packages/lsb_release.py /usr/local/python-3.7/lib/python3.7/
然后,再试试pip3:
$ pip3 list
Package Version
---------- -------
pip 19.0.3
setuptools 40.8.0
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
成功!圆满解决 No module named 'lsb_release' 的问题。CentOS7中不存在此问题,CentOS7中甚至连 lsb_release 命令都没有。Ubuntu服务器版应该也存在这个问题,因为它也有 lsb_release 命令。
-- EOF --
本文链接:https://www.pynote.net/archives/592
《解决使用pip3时No module named 'lsb_release'的问题》有2条留言
Ctrl+D 收藏本页
©Copyright 麦新杰 Since 2019 Python笔记
安装python后,创建symbolic link to python(保留原Ubuntu系统的python3不动),用 python -m的方式运行pip,可规避此问题。并且不用sudo执行,defaulting to ~/.local 路径下。 [ ]
每次在Ubuntu中安装python,都要搞一遍这个问题... [ ]