【问题标题】:Matplotlib Import Error: TypeError: expected str, bytes or os.PathLike object, not WindowsPathMatplotlib 导入错误:TypeError:预期的 str、字节或 os.PathLike 对象,而不是 WindowsPath
【发布时间】:2021-10-27 17:11:26
【问题描述】:

虽然我无法导入 matplotlib.pyplot,但我发现我什至无法导入 matplotlib。

我正在使用 pip、ma​​tplotlib 3.4.3 和 python 3.9.5。

这是import matplotlib的回溯:

Traceback (most recent call last):
  File "c:\Users\xtzha\Desktop\UN Online Volunteer\GIS\Data\TZ_2015-16_DHS_08122021_1325_155740\TZBR7BDT\check.py", line 6, in <module>
    import matplotlib
  File "C:\Users\xtzha\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\__init__.py", line 822, in <module>
    rcParamsDefault = _rc_params_in_file(
  File "C:\Users\xtzha\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\__init__.py", line 727, in _rc_params_in_file
    with _open_file_or_url(fname) as fd:
  File "C:\Users\xtzha\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 117, in __enter__
    return next(self.gen)
  File "C:\Users\xtzha\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\__init__.py", line 701, in _open_file_or_url
    fname = os.path.expanduser(fname)
  File "C:\Users\xtzha\AppData\Local\Programs\Python\Python39\lib\ntpath.py", line 293, in expanduser
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not WindowsPath

我只是想知道是否可以在不降级的情况下导入 matplotlib。

附: 我尝试安装较低版本的matplotlib,但由于以下错误而无法安装:

 ERROR: Command errored out with exit status 1:
     command: 'C:\Users\xtzha\AppData\Local\Programs\Python\Python39\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\xtzha\\AppData\\Local\\Temp\\pip-install-3wyvt9m9\\matplotlib_f7637efda90a425e98d8a380e709b5c3\\setup.py'"'"'; __file__='"'"'C:\\Users\\xtzha\\AppData\\Local\\Temp\\pip-install-3wyvt9m9\\matplotlib_f7637efda90a425e98d8a380e709b5c3\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\xtzha\AppData\Local\Temp\pip-pip-egg-info-gxef4izq'
         cwd: C:\Users\xtzha\AppData\Local\Temp\pip-install-3wyvt9m9\matplotlib_f7637efda90a425e98d8a380e709b5c3\
    Complete output (61 lines):
    ============================================================================
    Edit setup.cfg to change the build options

    BUILDING MATPLOTLIB
                matplotlib: yes [2.2.3]
                    python: yes [3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021,
                            17:27:52) [MSC v.1928 64 bit (AMD64)]]
                  platform: yes [win32]

    REQUIRED DEPENDENCIES AND EXTENSIONS
                     numpy: yes [version 1.19.3]
          install_requires: yes [handled by setuptools]
                    libagg: yes [pkg-config information for 'libagg' could not
                            be found. Using local copy.]
                  freetype: no  [The C/C++ header for freetype
                            (freetype2\ft2build.h) could not be found.  You may
                            need to install the development package.]
                       png: no  [The C/C++ header for png (png.h) could not be
                            found.  You may need to install the development
                            package.]
                     qhull: yes [pkg-config information for 'libqhull' could not
                            be found. Using local copy.]

    OPTIONAL SUBPACKAGES
               sample_data: yes [installing]
                  toolkits: yes [installing]
                     tests: no  [skipping due to configuration]
            toolkits_tests: no  [skipping due to configuration]

    OPTIONAL BACKEND EXTENSIONS
                    macosx: no  [Mac OS-X only]
                    qt5agg: no  [PySide2 not found; PyQt5 not found]
                    qt4agg: no  [PySide not found; PyQt4 not found]
                   gtk3agg: no  [Requires pygobject to be installed.]
                 gtk3cairo: no  [Requires cairocffi or pycairo to be installed.]
                    gtkagg: no  [Requires pygtk]
                     tkagg: yes [installing; run-time loading from Python Tcl /
                            Tk]
                     wxagg: no  [requires wxPython]
                       gtk: no  [Requires pygtk]
                       agg: yes [installing]
                     cairo: no  [cairocffi or pycairo not found]
                 windowing: yes [installing]

    OPTIONAL LATEX DEPENDENCIES
                    dvipng: no
               ghostscript: no
                     latex: no
                   pdftops: no

    OPTIONAL PACKAGE DATA
                      dlls: no  [skipping due to configuration]

    ============================================================================
                            * The following required packages can not be built:
                            * freetype, png
                            * Please check http://gnuwin32.sourceforge.net/packa
                            * ges/freetype.htm for instructions to install
                            * freetype
                            * Please check http://gnuwin32.sourceforge.net/packa
                            * ges/libpng.htm for instructions to install png
    ----------------------------------------

即使我安装了 freetype 和 png 包,仍然会出现相同的消息,这进一步阻止了我降级 maplotlib。有没有办法在不降级 matplotlib 的情况下解决这个导入问题?

【问题讨论】:

    标签: python python-3.x matplotlib pip


    【解决方案1】:

    看来您需要自己进行一些下载。

    从错误消息中,您至少需要下载freetypepng 的标头(这些链接仅供参考。请在确定它们是否符合您的需求以及您是否信任它们之前进一步阅读它们)。

    【讨论】:

    • 那你建议我降级matplotlib来解决导入错误?
    • 是的,我认为现在你应该降级。如果它不起作用,那么我没有解决方案......
    猜你喜欢
    • 2019-09-27
    • 2021-03-05
    • 2017-10-23
    • 2020-07-12
    • 2019-04-11
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多