脚本系列 —— python问题集锦

0. 从官网下载python速度慢

解决方法: 使用国内镜像网站下载

淘宝镜像网站:https://registry.npmmirror.com/binary.html

在这里插入图片描述

1. ImportError: No module named google.protobuf

使用Android源码自带的工具进行OTA升级,报错如下:

sun@sun-pc:~/nvme0/AOSP$ python system/update_engine/scripts/update_device.py out/dist/ota_package.zip
Traceback (most recent call last):
  File "system/update_engine/scripts/update_device.py", line 34, in <module>
    import update_payload.payload
  File "/home/sun/nvme0/AOSP/system/update_engine/scripts/update_payload/__init__.py", line 20, in <module>
    from update_payload.checker import CHECKS_TO_DISABLE
  File "/home/sun/nvme0/AOSP/system/update_engine/scripts/update_payload/checker.py", line 37, in <module>
    from update_payload import common
  File "/home/sun/nvme0/AOSP/system/update_engine/scripts/update_payload/common.py", line 21, in <module>
    from update_payload import update_metadata_pb2
  File "/home/sun/nvme0/AOSP/system/update_engine/scripts/update_payload/update_metadata_pb2.py", line 7, in <module>
    from google.protobuf import descriptor as _descriptor
ImportError: No module named google.protobuf

解决方法:
当前使用的python版本是2.7,安装对应的版本的protobuf模块即可解决问题。

sun@sun-pc:~/nvme0/AOSP$ python -m pip install protobuf==3.17.3     #如果是python3有问题,就把这里的python改为python3
Collecting protobuf==3.17.3
  Downloading https://files.pythonhosted.org/packages/e9/06/5606088c9fbfb924d0f228350e7ec8707fe8f8a6f3a024ace4f0da81d9ce/protobuf-3.17.3-cp27-cp27mu-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0MB)
    100% |████████████████████████████████| 1.0MB 22kB/s 
Collecting six>=1.9 (from protobuf==3.17.3)
  Downloading https://files.pythonhosted.org/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl
Installing collected packages: six, protobuf
Successfully installed protobuf-3.17.3 six-1.16.0

2. TypeError: a bytes-like object is required, not ‘str’

执行repo upload的时候,python脚本报告了类型错误

sun@sun-pc:~/code/AOSP/QSSI/packages/apps/Launcher3$ repo upload .
Upload project QSSI/packages/apps/Launcher3/ to remote branch AOSP:
  branch master ( 1 commit, Fri Oct 14 09:55:46 2022 +0800):
         b'1126d9bd \xe3\x80\x90\xe5\x88\x86\xe6\x94\xaf\xe5\x90\x8d\xe7\xa7\xb0\xe3\x80\x91\xef\xbc\ \xe3\x80\x90\xe5\xae\xa2\xe6\x88\xb7\xe5\x90\x8d\xe7\xa7\xb0\xe3\x80\x91\xef\xbc\x9a \xe3\x80\x90\xe4\xbf\xae\xe6\x94\xb9\xe6\x8f\x8f\xe8\xbf\xb0\xe3\x80\x91\xef\xbc\x9a\xe4\xbf\xae\xe5\xa4\x8d\xe9\xa6\x96\xe6\xac\xa1\xe5\xbc\x80\xe6\x9c\xba\xe7\x82\xb9\xe5\x87\xbb\xe4\xbb\xbb\xe5\x8a\xa1\xe9\x94\xae\xe9\x97\xaa\xe9\x80\x80 \xe3\x80\x90\xe8\xb4\xa3\xe4\xbb\xbb\xe4\xba\xba\xe5\x91\x98\xe3\x80\x91\xef\xbc\x9asun \xe3\x80\x90\xe9\x97\xae\xe9\xa2\x98\xe5\x8d\x95\xe5\x8f\xb7\xe3\x80\x91\xef\xbc\x9a666\n'
y
to http://192.32.1.23:8080 (y/N)? Traceback (most recent call last):
  File "/home/sun/code/AOSP/.repo/repo/main.py", line 547, in <module>
    _Main(sys.argv[1:])
  File "/home/sun/code/AOSP/.repo/repo/main.py", line 522, in _Main
    result = repo._Run(argv) or 0
  File "/home/sun/code/AOSP/.repo/repo/main.py", line 184, in _Run
    result = cmd.Execute(copts, cargs)
  File "/home/sun/sata/home/sun/project/AOSP/.repo/repo/subcmds/upload.py", line 504, in Execute
    self._SingleBranch(opt, pending[0][1][0], people)
  File "/home/sun/sata/home/sun/project/AOSP/.repo/repo/subcmds/upload.py", line 232, in _SingleBranch
    self._UploadAndReport(opt, [branch], people)
  File "/home/sun/sata/home/sun/project/AOSP/.repo/repo/subcmds/upload.py", line 401, in _UploadAndReport
    push_options=opt.push_options)
  File "/home/sun/sata/home/sun/project/AOSP/.repo/repo/project.py", line 193, in UploadForReview
    push_options=push_options)
  File "/home/sun/sata/home/sun/project/AOSP/.repo/repo/project.py", line 1152, in UploadForReview
    url = branch.remote.ReviewUrl(self.UserEmail, validate_certs)
  File "/home/sun/sata/home/sun/project/AOSP/.repo/repo/git_config.py", line 659, in ReviewUrl
    if info == 'NOT_AVAILABLE' or '<' in info:
TypeError: a bytes-like object is required, not 'str'

解决方法:
在这里插入图片描述
对658行返回的info进行类型转换,转换为str类型。

info = info.decode()   #将bytes类型转化为str类型

3. ImportError: No module named ‘_ssl’

情况一:
pyenv安装pythonc的时候报了这个错误。

sun@sun-pc:~$ pyenv install 3.5.0
Installing Python-3.5.0...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/src/pyenv/versions/3.5.0/lib/python3.5/ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ImportError: No module named '_ssl'
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

解决办法:
安装libssl1.0-dev可以解决。

sudo apt-get install libssl1.0-dev

情况二:
pyenv安装python3.10.6的时候报了这个错误。

解决方法:

sudo apt install libssl-dev

4. 因国外网站的原因导致pip安装模块失败

可以将pip下载模块的地址改为国内地址,方法如下(以阿里镜像地址为例)。

方法一:
终端执行下面命令。

pip config set global.trusted-host mirrors.aliyun.com
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip config set global.timeout 120

方法二:
修改~/.config/pip/pip.conf,内容如下。

[global]
trusted-host = mirrors.aliyun.com
index-url = https://mirrors.aliyun.com/pypi/simple
timeout = 120

方法三(临时方法):
在执行pip install命令的时候使用 -i 选项指定下载地址。

pip install xxx -i https://mirrors.aliyun.com/pypi/simple

5. Command ‘lsb_release -a’ returned non-zero exit status 1

将python版本从3.8切换回2.7后,pip 安装包的时候出现了下面关于 lsb_release 的问题。

sun@sun-pc:~$ python -m pip install psutil
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 163, in _run_module_as_main
    mod_name, _Error)
  File "/usr/lib/python2.7/runpy.py", line 111, in _get_module_details
    __import__(mod_name)  # Do not catch exceptions initializing package
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 22, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
  File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 64, in <module>
    vendored("distro")
  File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 33, in vendored
    __import__(modulename, globals(), locals(), level=0)
  File "/usr/share/python-wheels/distro-1.0.1-py2.py3-none-any.whl/distro.py", line 1051, in <module>
  File "/usr/share/python-wheels/distro-1.0.1-py2.py3-none-any.whl/distro.py", line 595, in __init__
  File "/usr/share/python-wheels/distro-1.0.1-py2.py3-none-any.whl/distro.py", line 934, in _get_lsb_release_info
subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1

解决方法:
删除 /usr/bin/lsb_release 文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星际工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值