DLL load failed while importing ngslib: A dynamic link library (DLL) initialization routine failed

Hi,

a few of my students get the error during the import of ngsolve after an error-free installation of ngsolve:

ImportError: DLL load failed while importing ngslib: A dynamic link library (DLL) initialization routine failed.

They are using Windows 11 but not everyone has the problem. A student has successfully installed ngsolve with python3.12 under Windows 11 on a notebook, but on the PC also with Windows 11 and python3.11 /3.12 with the above error.

Does anyone know the problem and a solution :upside_down_face:? I suspect that some environment variable is not set correctly. Unfortunately, I cannot reconstruct the problem as I am using MacOS or Ubuntu.

Best regards

Simon Stingelin.

installation with pip, installer or self built? most often this error is coming from a second installation of ngsolve and python loading incompatible dlls between py bindings and ngslib.
best Christopher

They used pip install with .org python distribution (cf. upload).

NGSolveInstalation.pdf (183.9 KB)

I will ask the students for second installations.

Best regards, Simon.

Hi Simon,

I encountered the same issue as your students with a pip install and python 3.12, which led me to your post.

I tried a few things, but the only thing that actually worked is wiping ngsolve/python installations clean and downloading the windows .msi installer and python 3.7. After that, everything works fine!

There is the possibility that pip installation and python 3.7 might also work, but I did not try that specific combination.

I also have the same issue.
I tried just pip install and not going through windows .msi installer.
The issue occurs with windows machines. The usual pip install works fine in Ubuntu/linux.

Any solution so far such that it works for windows as well .

ImportError: DLL load failed while importing ngslib: A dynamic link library (DLL) initialization routine failed.

Thanks

can you try the nightly build?

pip install --pre ngsovle

I think this might be fixed with a recent commit.

Thanks a lot. Now it works.

Hello,
I have the same problem.

I have a new HP Windows PC, python 13.3.7 from python.org. I created a venv, then run pip install ngsolve, then open python and run import ngsolve.

Also (not sure if this is relevant) there are no .dll files in the directory
\Venv\Lib\site-packages\ngsolve, while there are .dll files in other directories like \Venv\Lib\site-packages\netgen

The error message reads:

 File "C:\Users\cstolk1\Venv\Lib\site-packages\ngsolve\__init__.py", line 38, in <module>
    from .ngslib import __version__, ngstd, bla, la, fem, comp, solve
ImportError: DLL load failed while importing ngslib: A dynamic link library (DLL) initialization routine failed.

Best regards,
Chris Stolk

If you add the folder with the dlls to PATH environment variable does it work then?
Then probably the pip installer installs on windows some dlls in the wrong folder if in virtual environment, that could be

Hello Christopher,
Thanks for the reply. Did you mean the folder that contains dll’s like libngsolve.dll and nglib.dll?
It is called
C:\Users\cstolk1\Venv\Lib\site-packages\netgen in my case. I have added it to the path, but this made no difference. I have also added and removed some other folders with dll’s, which also made no difference.

I compared the path settings with those on my old laptop where NGSolve worked using the same setup. It was basically the same just some other apps I have added to the path like miktex and a pdf viewer.

BTW, I can still use ngsolve using the WSL.
Chris

Another guess that often leads to problems is mkl. Can you compare mkl-devel pip versions on the system that works vs the non working one?

Both systems have a package called mkl, version 2025.2.0

There should be a dynamic library ngslib.pyd within C:\Users\cstolk1\Venv\Lib\site-packages\ngsolve, which fails to load.

Can you try Dependency Walker (https://www.dependencywalker.com) on the library to check its dependencies?

An other tool is here: GitHub - lucasg/Dependencies: A rewrite of the old legacy software "depends.exe" in C# for Windows devs to troubleshoot dll load dependencies issues.

Joachim

Hi Joachim,
I located ngslib.pyd.

Dependencies worked better than Dependency Walker, but it doesn’t know about the paths that are set in the process of importing, so some non-problematic files were marked in red (not found). I made some screenshots (not attached).

I also found a python tool called dlltracer ( GitHub - microsoft/dlltracer-python: A DLL load tracing tool for CPython ). This I tried with a small script on both machines, the one where it works and the one where it fails. I will include below.

Basically you run “import ngsolve” in some kind of dlltrace context. The docs say that the import that causes the problem is not shown. The first import that is missing from the output of the machine where it fails is mkl_intel_thread.2.dll . So it appears that mkl files are the problem. On the other hand AFAICT the system finds at least one mkl file (mkl_rt.2.dll), but others are somehow not imported. Perhaps the one that is found is imported by some other component of netgen/ngsolve than the ones where it fails.

Chris

The script:

import os
import sys
import dlltracer

# # following doesn't make a difference
# mkl_bin_dir = "C:\\Users\\cstolk1\\Venv\\Library\\bin\\"
# os.add_dll_directory(mkl_bin_dir)

with dlltracer.Trace(out=sys.stdout):
    import ngsolve

# with open("log_dlltrace_test5.txt", "w") as log:
#     with dlltracer.Trace(out=log):
#         import ngsolve

str = input("press enter")

The output when it works:

LoadLibrary C:\Windows\System32\kernel.appcore.dll
LoadLibrary C:\Users\ccsto\AppData\Local\Programs\Python\Python313\DLLs\_bz2.pyd
LoadLibrary C:\Users\ccsto\AppData\Local\Programs\Python\Python313\DLLs\_lzma.pyd
LoadLibrary C:\Users\ccsto\AppData\Local\Programs\Python\Python313\DLLs\_ctypes.pyd
LoadLibrary C:\Windows\System32\ole32.dll

...

LoadLibrary C:\Windows\System32\wintrust.dll
LoadLibrary C:\Windows\System32\imagehlp.dll
LoadLibrary C:\Windows\System32\crypt32.dll
LoadLibrary C:\Windows\System32\msasn1.dll
LoadLibrary C:\Users\ccsto\Work\Software\VenvNgsJax\Library\bin\mkl_rt.2.dll
LoadLibrary C:\Users\ccsto\Work\Software\VenvNgsJax\Lib\site-packages\ngsolve\ngslib.pyd
LoadLibrary C:\Users\ccsto\Work\Software\VenvNgsJax\Lib\site-packages\netgen\libngsolve.dll
LoadLibrary C:\Users\ccsto\Work\Software\VenvNgsJax\Library\bin\mkl_intel_thread.2.dll
LoadLibrary C:\Users\ccsto\Work\Software\VenvNgsJax\Library\bin\mkl_core.2.dll
LoadLibrary C:\Users\ccsto\Work\Software\VenvNgsJax\Library\bin\tbbmalloc.dll
press enter

The output when it fails:

LoadLibrary C:\Windows\System32\kernel.appcore.dll
LoadLibrary C:\Users\cstolk1\AppData\Local\Programs\Python\Python313\DLLs\_bz2.pyd
LoadLibrary C:\Users\cstolk1\AppData\Local\Programs\Python\Python313\DLLs\_lzma.pyd
LoadLibrary C:\Users\cstolk1\AppData\Local\Programs\Python\Python313\DLLs\_ctypes.pyd
LoadLibrary C:\Windows\System32\ole32.dll

...

LoadLibrary C:\Windows\System32\wintrust.dll
LoadLibrary C:\Windows\System32\imagehlp.dll
LoadLibrary C:\Windows\System32\crypt32.dll
LoadLibrary C:\Windows\System32\msasn1.dll
LoadLibrary C:\Users\cstolk1\Venv\Library\bin\mkl_rt.2.dll
LoadLibrary C:\Users\cstolk1\Venv\Lib\site-packages\ngsolve\ngslib.pyd
LoadLibrary C:\Users\cstolk1\Venv\Lib\site-packages\netgen\libngsolve.dll
Failed \Device\HarddiskVolume3\Users\cstolk1\Venv\Lib\site-packages\netgen\libngsolve.dll
Failed \Device\HarddiskVolume3\Users\cstolk1\Venv\Lib\site-packages\ngsolve\ngslib.pyd
Traceback (most recent call last):
  File "C:\Users\cstolk1\TestNgSolve\test5.py", line 10, in <module>
    import ngsolve
  File "C:\Users\cstolk1\Venv\Lib\site-packages\ngsolve\__init__.py", line 38, in <module>
    from .ngslib import __version__, ngstd, bla, la, fem, comp, solve
ImportError: DLL load failed while importing ngslib: A dynamic link library (DLL) initialization routine failed.

@chriss
Does the problem still persist?
If so, which numpy version is installed (if it’s not installed, install it and try again).

Hi everyone,

I ran into the same issue after a recent update. I completely removed all previous Python installations from my PC and started with a clean installation (Python 3.13.9).
During the setup process, I installed all required packages via pip without any errors or warnings. numpy (version 2.3.4) was installed successfully as well.

However, when trying to import ngsolve, I get the following error message:

from .ngslib import __version__, ngstd, bla, la, fem, comp, solve
ImportError: DLL load failed while importing ngslib: A DLL initialization routine failed.

Any hints on what might be causing this DLL initialization error would be greatly appreciated.

Thanks in advance!

I’ve got the same problem as you since yesterday, Sebastian, on Python 3.12.5. Tried on both main python install path as well as with a local virtual environment, same issue.

Edit: managed to make it work in vscode by setting up a virtual environment and installing ipykernel, ngsolve, netgen, numpy in that directory. Don’t really understand why it didn’t work before

It seems this issue is happening with specific Versions of the Microsoft Visual C++ Runtime libraries. That could explain why it’s failing for some people only, and only in certain environments. VS Code for instance might ship with a newer runtime version.

To anyone having this issue, try updating the x64 runtimes:
https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170#latest-supported-redistributable-version