1. 程式人生 > 其它 >如何在 python 中解決報錯 ImportError: The 'pyparsing' package is required

如何在 python 中解決報錯 ImportError: The 'pyparsing' package is required

問題描述

今天在敲程式碼的時候遇到了一個問題,報錯資訊如下所示:

  File "D:\Python_Study\Github_Repositories\Hotspot-Detector\algorithm\net\ssd.py", line 7, in <module>
    import torch
  File "D:\Python\lib\site-packages\torch\__init__.py", line 29, in <module>
    from .torch_version import __version__ as __version__
  File "D:\Python\lib\site-packages\torch\torch_version.py", line 3, in <module>
    from pkg_resources import packaging  # type: ignore[attr-defined]
  File "D:\Python\lib\site-packages\pkg_resources\__init__.py", line 83, in <module>
    __import__('pkg_resources.extern.packaging.requirements')
  File "D:\Python\lib\site-packages\pkg_resources\_vendor\packaging\requirements.py", line 9, in <module>
    from pkg_resources.extern.pyparsing import stringStart, stringEnd, originalTextFor, ParseException
  File "D:\Python\lib\site-packages\pkg_resources\extern\__init__.py", line 50, in load_module
    raise ImportError(
ImportError: The 'pyparsing' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution

這就很讓人納悶,上一秒同一個檔案還跑的好好的,下一秒怎麼就跑不起來了?更奇怪的是,連 VS Code 的格式化檔案功能都不起作用了。

問題解決

又仔細看了一下自己的資料夾,發現剛才偷懶新建了一個 copy.py 檔案,而恰好 python 自帶了一個 copy 包,這樣就命名衝突了。只要把 copy.py 改成其他名字就大功告成了~~