当前位置:   article > 正文

ModuleNotFoundError: No module named ‘formatter‘

ModuleNotFoundError: No module named ‘formatter‘

当你在使用Python时,如果遇到 ModuleNotFoundError: No module named 'formatter' 的错误,这通常表示你的代码中引用了一个名为 'formatter' 的模块,但是Python解释器无法找到这个模块。

这个问题发生在Ubuntu22.04上安装python2.7后,通过repo同步源代码的时候出现的。


分析:formatter已在python3.4+标记成废弃接口,就算你按照网上教程添加这个模块也无法解决。

经过查询资料,https://gerrit-review.googlesource.com/c/git-repo/+/303282
有提供的解决方法:


实际操作如下:

查看repo报错信息,找到help.py的具体路径,例如: .repo/repo/subcmds/help.py

然后给help.py打上图的补丁即可解决问题。

打补丁后的文件如下:
 

  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2008 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. import re
  18. import sys
  19. import textwrap
  20. from subcmds import all_commands
  21. from color import Coloring
  22. from command import PagedCommand, MirrorSafeCommand, GitcAvailableCommand, GitcClientCommand
  23. import gitc_utils
  24. class Help(PagedCommand, MirrorSafeCommand):
  25. common = False
  26. helpSummary = "Display detailed help on a command"
  27. helpUsage = """
  28. %prog [--all|command]
  29. """
  30. helpDescription = """
  31. Displays detailed usage information about a command.
  32. """
  33. def _PrintCommands(self, commandNames):
  34. """Helper to display |commandNames| summaries."""
  35. maxlen = 0
  36. for name in commandNames:
  37. maxlen = max(maxlen, len(name))
  38. fmt = ' %%-%ds %%s' % maxlen
  39. for name in commandNames:
  40. command = all_commands[name]()
  41. try:
  42. summary = command.helpSummary.strip()
  43. except AttributeError:
  44. summary = ''
  45. print(fmt % (name, summary))
  46. def _PrintAllCommands(self):
  47. print('usage: repo COMMAND [ARGS]')
  48. print('The complete list of recognized repo commands are:')
  49. commandNames = list(sorted(all_commands))
  50. self._PrintCommands(commandNames)
  51. print("See 'repo help <command>' for more information on a "
  52. 'specific command.')
  53. def _PrintCommonCommands(self):
  54. print('usage: repo COMMAND [ARGS]')
  55. print('The most commonly used repo commands are:')
  56. def gitc_supported(cmd):
  57. if not isinstance(cmd, GitcAvailableCommand) and not isinstance(cmd, GitcClientCommand):
  58. return True
  59. if self.manifest.isGitcClient:
  60. return True
  61. if isinstance(cmd, GitcClientCommand):
  62. return False
  63. if gitc_utils.get_gitc_manifest_dir():
  64. return True
  65. return False
  66. commandNames = list(sorted([name
  67. for name, command in all_commands.items()
  68. if command.common and gitc_supported(command)]))
  69. self._PrintCommands(commandNames)
  70. print(
  71. "See 'repo help <command>' for more information on a specific command.\n"
  72. "See 'repo help --all' for a complete list of recognized commands.")
  73. def _PrintCommandHelp(self, cmd, header_prefix=''):
  74. class _Out(Coloring):
  75. def __init__(self, gc):
  76. Coloring.__init__(self, gc, 'help')
  77. self.heading = self.printer('heading', attr='bold')
  78. self.wrap = True
  79. def _PrintSection(self, heading, bodyAttr):
  80. try:
  81. body = getattr(cmd, bodyAttr)
  82. except AttributeError:
  83. return
  84. if body == '' or body is None:
  85. return
  86. if not self._first:
  87. self.nl()
  88. self._first = False
  89. self.heading('%s%s', header_prefix, heading)
  90. self.nl()
  91. self.nl()
  92. me = 'repo %s' % cmd.NAME
  93. body = body.strip()
  94. body = body.replace('%prog', me)
  95. # Extract the title, but skip any trailing {#anchors}.
  96. asciidoc_hdr = re.compile(r'^\n?#+ ([^{]+)(\{#.+\})?$')
  97. for para in body.split("\n\n"):
  98. if para.startswith(' '):
  99. self.write('%s', para)
  100. self.nl()
  101. self.nl()
  102. continue
  103. m = asciidoc_hdr.match(para)
  104. if m:
  105. self.heading('%s%s', header_prefix, m.group(1))
  106. self.nl()
  107. self.nl()
  108. continue
  109. lines = textwrap.wrap(para.replace(' ', ' '), width=80,
  110. break_long_words=False, break_on_hyphens=False)
  111. for line in lines:
  112. self.write('%s', line)
  113. self.nl()
  114. self.nl()
  115. out = _Out(self.manifest.globalConfig)
  116. out._PrintSection('Summary', 'helpSummary')
  117. cmd.OptionParser.print_help()
  118. out._PrintSection('Description', 'helpDescription')
  119. def _PrintAllCommandHelp(self):
  120. for name in sorted(all_commands):
  121. cmd = all_commands[name]()
  122. cmd.manifest = self.manifest
  123. self._PrintCommandHelp(cmd, header_prefix='[%s] ' % (name,))
  124. def _Options(self, p):
  125. p.add_option('-a', '--all',
  126. dest='show_all', action='store_true',
  127. help='show the complete list of commands')
  128. p.add_option('--help-all',
  129. dest='show_all_help', action='store_true',
  130. help='show the --help of all commands')
  131. def Execute(self, opt, args):
  132. if len(args) == 0:
  133. if opt.show_all_help:
  134. self._PrintAllCommandHelp()
  135. elif opt.show_all:
  136. self._PrintAllCommands()
  137. else:
  138. self._PrintCommonCommands()
  139. elif len(args) == 1:
  140. name = args[0]
  141. try:
  142. cmd = all_commands[name]()
  143. except KeyError:
  144. print("repo: '%s' is not a repo command." % name, file=sys.stderr)
  145. sys.exit(1)
  146. cmd.manifest = self.manifest
  147. self._PrintCommandHelp(cmd)
  148. else:
  149. self._PrintCommandHelp(self)

通过以上方法,你有望解决 ModuleNotFoundError: No module named 'formatter' 的问题。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号