Search code examples
pythonwindowserror-handlingpathpermissions

Importing some libraries in Python fails


I do not know what happened, but suddenly I can no longer run Python code like before. I am on windows and I have a feeling the problem is the result of PATH, because I have seen this error before.

Python gives me the following error when trying to import matplotlib:

ImportError: cannot import name 'axes' from 'matplotlib' 
(C:\Users\JohnDoe\AppData\Local\Packages
\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\
Python310\site-packages\matplotlib\__init__.py)

When trying to install matplotlib, I get the following error:

ERROR: Could not install packages due to an OSError: [WinError 5]
Toegang geweigerd: 'C:\\Users\\JohnDoe\\AppData\\Local\\Packages
\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages
\\Python310\\site-packages\\matplotlib\\ft2font.cp310-win_amd64.pyd'
Check the permissions.

My setup:

  • Windows 11
  • VS Code installed through the MS Store
  • Python installed through VS Code, which also installs python through the MS Store

Solution

  • I managed to solve this issue. For future reference, the solution was:

    • Run pip list, take note of every package starting with ~ and where the first letter is missing (example ~atplotlib instead of matplotlib). The location of the directory is also shown.
    • Open the location as indicated by pip list in a file explorer, find the package starting with a ~ and delete the folder.
    • The command pip install matplotlib and pip install --user matplotlib still create a OSerror stating the permission is denied. This error occurs both in the terminal of VS Code, command prompt and command prompt opened as administrator.
    • The solution to the OSerror is to run VS Code as administrator and then run pip install matplotlib from the terminal in VS Code.

    To summarize: delete the conflicting package and reinstall through the VS Code terminal while running VS Code as administrator.

    All other suggestions I found online to solve this issue failed for me. These suggestions included changing the permission in windows to full access.