模块六没有属性 ensure_text

Module Six has no attribute ensure_text

突然之间,我的 python 安装好像坏了。当尝试 运行 一个 python 脚本时,我得到以下...

[michael@arch ~/Code/CTF/aero/house]$ ./solve.py 
Traceback (most recent call last):
  File "/home/michael/Code/CTF/aero/house/./solve.py", line 5, in <module>
    from pwn import *
  File "/home/michael/.local/lib/python3.9/site-packages/pwn/__init__.py", line 20, in <module>
    pwnlib.update.check_automatically()
  File "/home/michael/.local/lib/python3.9/site-packages/pwnlib/update.py", line 199, in check_automatically
    log.info("\n".join(message))
  File "/home/michael/.local/lib/python3.9/site-packages/pwnlib/log.py", line 395, in info
    self._log(logging.INFO, message, args, kwargs, 'info')
  File "/home/michael/.local/lib/python3.9/site-packages/pwnlib/log.py", line 292, in _log
    msg = six.ensure_text(msg)
AttributeError: module 'six' has no attribute 'ensure_text'

尝试使用 pip 重新安装模块也会中断...

[michael@arch ~/Code/CTF/aero/house]$ python3 -m pip install pwntools
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.9/site-packages/pip/__main__.py", line 26, in <module>
    sys.exit(_main())
  File "/usr/lib/python3.9/site-packages/pip/_internal/cli/main.py", line 73, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "/usr/lib/python3.9/site-packages/pip/_internal/commands/__init__.py", line 105, in create_command
    module = importlib.import_module(module_path)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/usr/lib/python3.9/site-packages/pip/_internal/commands/install.py", line 14, in <module>
    from pip._internal.cache import WheelCache
  File "/usr/lib/python3.9/site-packages/pip/_internal/cache.py", line 15, in <module>
    from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
  File "/usr/lib/python3.9/site-packages/pip/_internal/utils/temp_dir.py", line 11, in <module>
    from pip._vendor.six import ensure_text
ImportError: cannot import name 'ensure_text' from 'six' (/home/michael/.local/lib/python3.9/site-packages/six.py)

我读到这是由运行以root身份使用pip引起的,但我没有这样做。我也没有进行任何最近的升级或安装任何新软件包。到目前为止,我已经尝试设置 PYTHONPATH、重新安装 python 和重新安装 pip,并且 none 这些选项都有效。有人有什么想法吗?提前致谢!

编辑:我也尝试更新 python 和 pip,但没有用。

请求的信息:

>>> import six; print(six.__file__)
/home/michael/.local/lib/python3.9/site-packages/six.py
>>> print(six.__version__)
1.10.0

通过在 ~/.local/lib/python3.9/site-packages 中执行 wget https://raw.githubusercontent.com/benjaminp/six/master/six.py,我能够更新六个。这解决了问题。

python3 -m pip install --upgrade six

您最好在虚拟环境中执行此操作。