Skip to content

[Bug]: Permission denied when importing matplotlib.pyplot #28538

Closed
@JCisCS

Description

@JCisCS

Bug summary

Once I type " import matplotlib.pyplot as plt" and run it, jupyter always returns:
"Matplotlib is building the font cache; this may take a moment."
#Then the return messages will end up as :
"PermissionError: [Errno 13] Permission denied: 'C:\Users\J\.matplotlib\fontlist-v330.json.matplotlib-lock'

And the functions of matplotlib.pyplot can not be excuted.

Code for reproduction

import matplotlib.pyplot as plt

Actual outcome

Matplotlib is building the font cache; this may take a moment.

PermissionError                           Traceback (most recent call last)
Cell In[3], line 1
----> 1 import matplotlib.pyplot as plt

File ~\anaconda3\Lib\site-packages\matplotlib\pyplot.py:56
     54 from cycler import cycler
     55 import matplotlib
---> 56 import matplotlib.colorbar
     57 import matplotlib.image
     58 from matplotlib import _api

File ~\anaconda3\Lib\site-packages\matplotlib\colorbar.py:19
     16 import numpy as np
     18 import matplotlib as mpl
---> 19 from matplotlib import _api, cbook, collections, cm, colors, contour, ticker
     20 import matplotlib.artist as martist
     21 import matplotlib.patches as mpatches

File ~\anaconda3\Lib\site-packages\matplotlib\contour.py:15
     13 import matplotlib as mpl
     14 from matplotlib import _api, _docstring
---> 15 from matplotlib.backend_bases import MouseButton
     16 from matplotlib.lines import Line2D
     17 from matplotlib.path import Path

File ~\anaconda3\Lib\site-packages\matplotlib\backend_bases.py:46
     43 import numpy as np
     45 import matplotlib as mpl
---> 46 from matplotlib import (
     47     _api, backend_tools as tools, cbook, colors, _docstring, text,
     48     _tight_bbox, transforms, widgets, is_interactive, rcParams)
     49 from matplotlib._pylab_helpers import Gcf
     50 from matplotlib.backend_managers import ToolManager

File ~\anaconda3\Lib\site-packages\matplotlib\text.py:16
     14 from . import _api, artist, cbook, _docstring
     15 from .artist import Artist
---> 16 from .font_manager import FontProperties
     17 from .patches import FancyArrowPatch, FancyBboxPatch, Rectangle
     18 from .textpath import TextPath, TextToPath  # noqa # Logically located here

File ~\anaconda3\Lib\site-packages\matplotlib\font_manager.py:1582
   1578     _log.info("generated new fontManager")
   1579     return fm
-> 1582 fontManager = _load_fontmanager()
   1583 findfont = fontManager.findfont
   1584 get_font_names = fontManager.get_font_names

File ~\anaconda3\Lib\site-packages\matplotlib\font_manager.py:1577, in _load_fontmanager(try_read_cache)
   1575             return fm
   1576 fm = FontManager()
-> 1577 json_dump(fm, fm_path)
   1578 _log.info("generated new fontManager")
   1579 return fm

File ~\anaconda3\Lib\site-packages\matplotlib\font_manager.py:963, in json_dump(data, filename)
    946 def json_dump(data, filename):
    947     """
    948     Dump `FontManager` *data* as JSON to the file named *filename*.
    949 
   (...)
    961     processes from overwriting one another's output.
    962     """
--> 963     with cbook._lock_path(filename), open(filename, 'w') as fh:
    964         try:
    965             json.dump(data, fh, cls=_JSONEncoder, indent=2)

File ~\anaconda3\Lib\contextlib.py:137, in _GeneratorContextManager.__enter__(self)
    135 del self.args, self.kwds, self.func
    136 try:
--> 137     return next(self.gen)
    138 except StopIteration:
    139     raise RuntimeError("generator didn't yield") from None

File ~\anaconda3\Lib\site-packages\matplotlib\cbook.py:1821, in _lock_path(path)
   1819 for _ in range(retries):
   1820     try:
-> 1821         with lock_path.open("xb"):
   1822             break
   1823     except FileExistsError:

File ~\anaconda3\Lib\pathlib.py:1013, in Path.open(self, mode, buffering, encoding, errors, newline)
   1011 if "b" not in mode:
   1012     encoding = io.text_encoding(encoding)
-> 1013 return io.open(self, mode, buffering, encoding, errors, newline)

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\happy\\.matplotlib\\fontlist-v330.json.matplotlib-lock'

### Expected outcome

The expected outcome is to successful import matplotlib.pyplot. 
(Sorry to bother you.
I have struggled with this for more than 2 weeks and searched different methods to resolve it. But, ...
Please help me to out of this, many many thanks!)

### Additional information

*What are the conditions under which this bug happens? input parameters, edge cases, etc?
Ans: When I type " import matplotlib.pyplot as plt" and run it in the Jupyter Notebook.
*Has this worked in earlier versions? 
Ans: No. 
*Do you know why this bug is happening?
Ans:  No. 
*Do you maybe even know a fix? 
Ans:No.
I have tried a lot of times to figure out this import problem, including uninstall/reinstall anaconda, python, upgrade the version of matplotlib......
 Based on the google search results, I also tried to type
 "import matplotlib matplotlib.use('TkAgg')" 
before I import matplotlib.pyplot. 
But it returns me 
"Cell In[2], 
line 1     import matplotlib matplotlib.use('TkAgg')                       
^ SyntaxError: invalid syntax"

Operating system

Windows

Matplotlib Version

matplotlib 3.8.4

Matplotlib Backend

matplotlib-inline 0.1.6

Python version

Python 3.12.3

Jupyter version

Jupyter 7.0.8

Installation

conda

[TAC edited to add markup]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions