当前位置: 首页 > 工具软件 > QARK > 使用案例 >

Python3.10 领英qark解决:ImportError: cannot import name ‘Mapping‘ from ‘collections‘

段干浩荡
2023-12-01

问题

安装qark后执行qark --help出现下面的错误

 % qark --help        
Traceback (most recent call last):
  File "/Users/user/Library/Python/3.10/bin/qark", line 5, in <module>
    from qark.qark import cli
  File "/Users/user/Library/Python/3.10/lib/python/site-packages/qark/qark.py", line 13, in <module>
    from qark.report import Report
  File "/Users/user/Library/Python/3.10/lib/python/site-packages/qark/report.py", line 5, in <module>
    from jinja2 import Environment, PackageLoader, select_autoescape, Template
  File "/opt/homebrew/lib/python3.10/site-packages/jinja2/__init__.py", line 33, in <module>
    from jinja2.environment import Environment, Template
  File "/opt/homebrew/lib/python3.10/site-packages/jinja2/environment.py", line 16, in <module>
    from jinja2.defaults import BLOCK_START_STRING, \
  File "/opt/homebrew/lib/python3.10/site-packages/jinja2/defaults.py", line 32, in <module>
    from jinja2.tests import TESTS as DEFAULT_TESTS
  File "/opt/homebrew/lib/python3.10/site-packages/jinja2/tests.py", line 13, in <module>
    from collections import Mapping
ImportError: cannot import name 'Mapping' from 'collections' (/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py)

解决办法

原因分析:无法从collections 导入 'Mapping,因为Python3.10版本以后对requests 库进行调整,collections中不能直接调用方法MappingMutableMapping

解决办法就是:找到引用collections模块的__init__.py文件或其他py文件,调整从abc导入:

# 将这两句
from collections import Mapping 
from collections import MutableMapping

# 替换为以下两句
from collections.abc import Mapping
from collections.abc import MutableMapping
# 即用collections.abc代替collections调用方法Mapping、MutableMapping

写了个命令批量替换,注意改下自己site-packages的路径,

  • unix系统下
find /opt/homebrew/lib/python3.10/site-packages/ -name "*.py"| xargs grep "from collections import Mapping" | awk -F : '{ print $1 }' | xargs sed -i "" 's/from collections import Mapping/from collections.abc import Mapping/g'
  • linux系统
find /usr/lib/python3.10/site-packages/ -name "*.py"| xargs grep "from collections import Mapping" | awk -F : '{ print $1 }' | xargs sed -i 's/from collections import Mapping/from collections.abc import Mapping/g'

unix与linux在执行sed指令时有些区别,-i指令后面多加一个""空白符即可

替换后就能用qark

% qark --help
Usage: qark [OPTIONS]

Options:
  --sdk-path DIRECTORY            Path to the downloaded SDK directory if
                                  already downloaded. Only necessary if
                                  --exploit-apk is passed. If --exploit-apk is
                                  passed and this flag is not passed,QARK will
                                  attempt to use the ANDROID_SDK_HOME,
                                  ANDROID_HOME, ANDROID_SDK_ROOT environment
                                  variables (in that order) for a path.
  --build-path DIRECTORY          Path to place decompiled files and exploit
                                  APK.  [default: build]
  --debug / --no-debug            Show debugging statements (helpful for
                                  issues).  [default: False]
  --apk PATH                      APK to decompile and run static analysis. If
                                  passed, the --java option is not used.
  --java PATH                     A directory containing Java code, or a Java
                                  file, to run static analysis. If passed,the
                                  --apk option is not used.
  --report-type [html|xml|json|csv]
                                  Type of report to generate along with
                                  terminal output.  [default: html]
  --exploit-apk / --no-exploit-apk
                                  Create an exploit APK targetting a few
                                  vulnerabilities.  [default: False]
  --version                       Show the version and exit.
  --help                          Show this message and exit.

参考

  1. Python错误:ImportError: cannot import name ‘Mapping‘ from ‘collections‘
  2. mac 执行sed -i指令时,总是出现extra characters at the end of command
 类似资料: