python-3.x PyCharm ModuleNotFoundError:没有名为“_fontdata_enc_winansi”的模块

1l5u6lss  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(74)

我有一个PyCharm项目,用于其他项目,这些项目是作为单独的PyCharm项目管理的github checkout 。我使用了一个外部项目(z3c.rml)中的Python脚本,这个脚本依赖于另一个外部项目(reportlab)。我已经将外部项目的src文件夹配置为项目中的内容根src文件夹。当我在我的项目中运行外部脚本时,脚本不会找到其他外部项目源。PyCharm不会在我的项目中显示任何未解决的符号错误。
运行脚本将生成以下stacktrace:

E:\Users\Work\Documents\ReportConverter\python\win-venv\Scripts\python.exe E:\Users\Work\PycharmProjects\z3c.rml\src\z3c\rml\rml2pdf.py rml/test/sample.rml ../data/sample.pdf 
Traceback (most recent call last):
  File "E:\Users\Work\PycharmProjects\z3c.rml\src\z3c\rml\rml2pdf.py", line 24, in <module>
    from z3c.rml import document
  File "E:\Users\Work\PycharmProjects\z3c.rml\src\z3c\rml\__init__.py", line 4, in <module>
    import z3c.rml.paraparser
  File "E:\Users\Work\PycharmProjects\z3c.rml\src\z3c\rml\paraparser.py", line 21, in <module>
    import reportlab.platypus.paragraph
  File "E:\Users\Work\PycharmProjects\reportlab\src\reportlab\platypus\__init__.py", line 7, in <module>
    from reportlab.platypus.flowables import Flowable, Image, Macro, PageBreak, Preformatted, Spacer, XBox, \
  File "E:\Users\Work\PycharmProjects\reportlab\src\reportlab\platypus\flowables.py", line 34, in <module>
    from reportlab.pdfbase.pdfmetrics import stringWidth
  File "E:\Users\Work\PycharmProjects\reportlab\src\reportlab\pdfbase\pdfmetrics.py", line 22, in <module>
    from reportlab.pdfbase import _fontdata
  File "E:\Users\Work\PycharmProjects\reportlab\src\reportlab\pdfbase\_fontdata.py", line 159, in <module>
    module = __import__(modname, globals(), locals())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_fontdata_enc_winansi'

字符串
我检查了Python控制台中的Python anssys路径

>>> print (os.environ['PYTHONPATH'])
C:/Program Files/JetBrains/PyCharm Community Edition 2023.1.2/plugins/python-ce/helpers/third_party/thriftpy;C:/Program Files/JetBrains/PyCharm Community Edition 2023.1.2/plugins/python-ce/helpers/pydev
import sys
print (sys.path)
['C:\\Program Files\\JetBrains\\PyCharm Community Edition 2023.1.2\\plugins\\python-ce\\helpers\\pydev', 'C:\\Program Files\\JetBrains\\PyCharm Community Edition 2023.1.2\\plugins\\python-ce\\helpers\\third_party\\thriftpy', 'C:\\Program Files\\JetBrains\\PyCharm Community Edition 2023.1.2\\plugins\\python-ce\\helpers\\pydev', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\\python311.zip', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\\DLLs', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\\Lib', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0', 'E:\\Users\\Work\\Documents\\ReportConverter\\python\\win-venv', 'E:\\Users\\Work\\Documents\\ReportConverter\\python\\win-venv\\Lib\\site-packages', 'E:\\Users\\Work\\Documents\\ReportConverter\\docker', 'E:\\Users\\Work\\Documents\\ReportConverter\\python', 'E:\\Users\\Work\\PycharmProjects\\reportlab\\src', 'E:\\Users\\Work\\PycharmProjects\\z3c.rml\\src', 'E:\\Users\\Work\\Documents\\ReportConverter\\python\\py']


我觉得一切都很好。
我在python控制台中尝试了一下:

>>>import _fontdata_enc_winansi
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.1.2\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
           ^^^^^^
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.1.2\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_fontdata_enc_winansi'

>>>import reportlab.pdfbase._fontdata_enc_winansi


那很好看起来Python不会从像“reportlab.pdfbase”这样的子文件夹中的模块中进行搜索
问题是:生成modulename,但不包含包文件夹:

modname = '_fontdata_enc_%s' % keyname.lower()[:-8]  #chop off 'Encoding'
    module = __import__(modname, globals(), locals())


搜索到的模块与上面的代码位于同一个包中

qnzebej0

qnzebej01#

解决方案是:

module = __import__(modname, globals(), locals(), level=1)

字符串
与文档相反,您需要在__import_调用中添加“level=1”,以便在本地搜索模块。

相关问题