0

On a specific computer with Python 3.11.2, matplotlib 3.7.1, I am experiencing the following:

If I do following, "all" is good:

C:\Users\zzz>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyqtgraph as pg
>>> import matplotlib
>>> exit()

But if I omit the "import pyqtgraph" I get:

C:\Users\zzz>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\zzz\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\__init__.py", line 246, in <module>
    _check_versions()
  File "C:\Users\zzz\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\__init__.py", line 240, in _check_versions
    module = importlib.import_module(modname)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\zzz\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\zzz\AppData\Local\Programs\Python\Python311\Lib\site-packages\kiwisolver\__init__.py", line 8, in <module>
    from ._cext import (
ImportError: DLL load failed while importing _cext: The specified module could not be found.
>>>

I've tried to remove and reinstall matplotlib, but same result.

(The same happened with a "shiv"-package that I tried to run, but as can be seen, it also just happens in the simplest way...)

Any ideas?

Egholm
  • 33
  • 4
  • Generally these import and environment issues are often solvable by using a virtual environment to separate your python packages to prevent interference. Here's a useful discussion about the python environment tools that ship with python https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe . Basically the community feels your pain, and now the challenge is not whether to use an environment manager, but which to choose... – aorr Apr 14 '23 at 16:40
  • Thanks for the suggestion @aorr, but I've already tried that (with venv) :-| – Egholm Apr 17 '23 at 07:11

2 Answers2

1

After creating an entire new user on the PC, and it still didn't work, I found this answer: matplotlib ImportError: DLL load failed while importing _cext So it all boils down to whether the PC has Microsoft's runtime libraries installed (either as described in the link above, or by installing the https://learn.microsoft.com/en-US/cpp/windows/latest-supported-vc-redist?view=msvc-170).

So if even though matplotlib comes with it's own "msvcp140.dll" (msvcp140-[a-f0-9]{32}.dll), which is being installed in the "site-packages/matplotlib.libs"-folder, this DLL has to be in the location of the python.exe, well.

Egholm
  • 33
  • 4
0

This error is because your python is resolving the packages on the appdata path before your venv path, likely because at some point you installed a package via pip with the --user flag.

My suggestion is to start over and delete your entire C:\Users\zzz\AppData\Local\Programs\Python directory.

When pip install encounters certain errors during installation the program suggests using the '--user' flag to try to get around permissions issues. This is sometimes an ok suggestion on linux, but it's more likely that anyone using pip on windows and getting a warning suggesting to install as '--user' should use a package manager instead.

aorr
  • 937
  • 7
  • 9
  • no that wasn't it. I tried what you suggested, and removed the Python folder. I even tried creating an entire new user on the computer - and the problem persists. This with fresh user-based installations of both Python 3.11.2 and Python 3.10.8. With and without venv, I get the same error when trying to import matplotlib. – Egholm Apr 20 '23 at 07:41