Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JCisCS opened this issue Jul 11, 2024 · 8 comments · Fixed by #28541
Closed

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

JCisCS opened this issue Jul 11, 2024 · 8 comments · Fixed by #28541
Milestone

Comments

@JCisCS
Copy link

JCisCS commented Jul 11, 2024

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]

@WeatherGod
Copy link
Member

The error is that you don't have permission to create C:\Users\J\.matplotlib\fontlist-v330.json.matplotlib-lock. This is unusual, but I doubt there is a bug in matplotlib. You need to see what the permissions are for the C:\Users\J\.matplotlib\ folder. I am assuming that you are logged in as the user J?

@tacaswell
Copy link
Member

Agree that this is a permissions issue and you should sort out why a process running as your user can not create the lock file. I would try removing C:\Users\J\.matplotlib and letting us re-generate it on the next import. On startup we check that the path exists, is a directory, and is writable. It would be good to sort out how you got to this state so we can make our test more correct.

if configdir:
configdir = Path(configdir).resolve()
elif sys.platform.startswith(('linux', 'freebsd')):
# Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first,
# as _xdg_base_getter can throw.
configdir = Path(xdg_base_getter(), "matplotlib")
else:
configdir = Path.home() / ".matplotlib"
try:
configdir.mkdir(parents=True, exist_ok=True)
except OSError:
pass
else:
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
return str(configdir)

is how we are currently checking that we can write to the cache directory.

That said, we do have the behavior that if the config directory is not writable we skip writing and continue on.

with cbook._lock_path(filename), open(filename, 'w') as fh:
try:
json.dump(data, fh, cls=_JSONEncoder, indent=2)
except OSError as e:
_log.warning('Could not save font_manager cache %s', e)

We are being careful about the json writing failing, but not careful about the lock failing!

@JCisCS
Copy link
Author

JCisCS commented Jul 11, 2024

The error is that you don't have permission to create C:\Users\J\.matplotlib\fontlist-v330.json.matplotlib-lock. This is unusual, but I doubt there is a bug in matplotlib. You need to see what the permissions are for the C:\Users\J\.matplotlib\ folder. I am assuming that you are logged in as the user J?

Dear WeatherGod and tacaswell,
Thank you so much for your suggestion.
Yes, I am login as "J".
I will try what your suggestions to see whether it can be resolved or not.

Actually, when I bumped into that import problem, I had tried to figure it out by reinstall my Anaconda and Python, therefor I remove both of them through the "Uninstall or change a program" of "Control panel".
After the removing, I did the re-install of Anaconda. But one message is shown up as:
"A version of Python 3.12(64-bit) is alreadt at C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.O_x64_qbz5n2kfra8pO
We recommend that if you want Anaconda3 registered as your system Python, you unregister this Python first. If you really know this is what you want, click OK, otherwise click cancel to continue."
Therefor, I went back to check the existence of Python form the "Uninstall or change a program" , but there is nothing left.
Then, I tried to reach the Python 3.12 mentioned in the above message to remove it. But I was prohibited from accessing the C:\Program Files.
So I went back to the Anaconda installation and finish it.
After that, I checked the versions of Anaconda, Python. Open the jupyter notebook and type the "import matplotlib.pyplot as plt" and run it, and, the Denied Message shown it self again to me.
I am wondering whether this event being related with this import denied problem since before that, the use of jupyter and importing of matplotlib.pyplot were smooth.

A lot of thanks for your great suggestions. I will try it! Thank you!

@JCisCS JCisCS closed this as completed Jul 11, 2024
@WeatherGod
Copy link
Member

WeatherGod commented Jul 11, 2024 via email

@tacaswell tacaswell reopened this Jul 11, 2024
@tacaswell
Copy link
Member

I'm going to re-open this as we should not be failing to import due to failure to write the cache.

@tacaswell tacaswell added this to the v3.9.2 milestone Jul 11, 2024
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Jul 11, 2024
The locker works by writing a file to disk.  This can also fail so make sure we
can still import in that case.

Closes matplotlib#28538
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Jul 11, 2024
The locker works by writing a file to disk.  This can also fail so make sure we
can still import in that case.

Closes matplotlib#28538
@JCisCS
Copy link
Author

JCisCS commented Jul 12, 2024

Dear tacaswell,
Thank you for your great suggestion!
I remove the "C:\Users\J.matplotlib", then open Jupyter Notebook, type and run "import matplotlib.pyplot as plt".
And, yeah! It works! It works good now, and I can plot the figures! :)
Although I have no idea why I am not being able to access the previous "C:\Users\J.matplotlib". Might I ask any idea from you?
It is workable now! :)
A million thanks for your great help! Thanks, and thanks! :)

And thank you too, WeatherGod!

@QuLogic QuLogic changed the title Permission denied when importing matplotlib.pyplot[Bug]: [Bug]: Permission denied when importing matplotlib.pyplot Aug 12, 2024
@mrmathematica
Copy link

I see the same bug, and don't think #28541 is enough. The try is moved up to include cbook._lock_path call, but it also need to catch TimeoutError (raised in

raise TimeoutError("""\
) along with OSError.

@tacaswell
Copy link
Member

@mrmathematica Under what conditions do you get the timeout error when it is correct to ignore the error?

In the case of OSError users may be running on systems with limited permissions where getting the write permissions sorted is in fact beyond their control. However, if the lock file exists and does not get cleaned up there is likely something within the users control going on and we should let them know that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants