Skip to content
QGIS
Troubleshooting

QGIS: DLL Load Failed Importing _C (Solved)

Published Updated

ImportError: DLL load failed while importing _C means Windows found PyTorch and then refused to load one of the compiled DLLs underneath it. You hit it in QGIS after a plugin installs torch into the QGIS Python, ours included. Install the Microsoft Visual C++ Redistributable (x64), then restart Windows: that clears it on most machines.

Traceback (most recent call last):
  File "C:\...\site-packages\torch\__init__.py", line 141, in <module>
    raise err
OSError: [WinError 126] The specified module could not be found.
Error loading "C:\...\site-packages\torch\lib\fbgemm.dll" or one of its dependencies.

ImportError: DLL load failed while importing _C: The specified module could not be found.

The same fault names a different file on some machines, and the fixes below are the same:

Error loading "C:\...\site-packages\torch\lib\shm.dll" or one of its dependencies.

Applies to Windows 10 and 11, QGIS 3.22 and later, and PyTorch 2.4.0 through 2.8.0, the Windows wheels that ship fbgemm.dll. Version 2.9.0 dropped that file.

If your error says [WinError 127] instead of 126, stop here. That one means the DLL loaded and a symbol inside it is missing, which is a version mismatch between packages, not this bug.

The quick fix

Install the Visual C++ Redistributable (x64)

Run vc_redist.x64.exe from aka.ms/vc14/vc_redist.x64.exe. Take x64 even if you already have x86. They are separate packages, and torch needs x64.

Restart Windows

A reboot, not a QGIS restart. The installer leaves part of its work for the restart.

Look for a second Python

In a Command Prompt, run where python. More than one result means an Anaconda, Miniconda or standalone Python is competing with the QGIS one.

Take that Python off PATH

Start, "environment variables", Edit the system environment variables, Environment Variables, Path. Delete the entries for the Python you do not use in QGIS, then sign out of Windows and back in.

If the error is unchanged, go to Fix 2.

Check it worked

Open the OSGeo4W Shell from the QGIS folder in the Start menu and run:

python -c "import torch; print(torch.__version__)"

A version number on its own line, with no traceback above it, means the DLLs load. Now repeat the test where it matters: in QGIS, Plugins, then Python Console, then import torch. A silent prompt there is the result you want. If one of the two passes and the other fails, they are running different Pythons, so go back to step 3.

Fix 2: a dependency of fbgemm.dll is missing

Some machines have the redistributable and still lack libomp140.x86_64.dll. The durable fix is to move to PyTorch 2.9.0 or later, which ships no fbgemm.dll at all. If you are pinned to an older version, users on the PyTorch tracker report that copying in libomp140.x86_64.dll, which arrives with the free Visual Studio Community, gets them running. That is a workaround, never a maintainer fix, and no PyTorch wheel bundles the file. Do not pull a loose DLL off a download site.

If the error is unchanged, go to Fix 3.

Fix 3: the wheel does not match the machine

A CUDA build on a computer with no matching NVIDIA driver fails on the same line. With no NVIDIA GPU, force the CPU build:

pip install --force-reinstall torch --index-url https://download.pytorch.org/whl/cpu

With a GPU, take the command the PyTorch install page gives for your CUDA version. Inside a conda environment, install torch with conda instead of pip, so the runtime libraries come from the same place as the package.

If the error is unchanged, go to Fix 4.

Fix 4: antivirus took a file after the install

Open Windows Security, then Virus and threat protection, then Protection history. Restore anything it removed from your site-packages folder, add that folder to the exclusions, then reinstall torch. The install reported success and the folder still looks full, so nothing on screen points here.

If the error is unchanged, go to Still broken? below.

Why it happens

PyTorch ships compiled DLLs rather than pure Python. fbgemm.dll and its neighbours in torch/lib link against the Microsoft C runtime, and Windows refuses to load a DLL when it cannot find that DLL's own dependencies. It reports the failure at the top of the chain, on _C, the torch extension module doing the import, which is why the message names a file that is sitting right there on disk.

The second cause is Python 3.8. Since that release, only the system paths, the directory holding the DLL or PYD file, and directories added with add_dll_directory() are searched. PATH and the current working directory are no longer used at all. So a machine carrying both Anaconda and QGIS can hand the loader a torch/lib from the wrong Python, and the error says nothing about which one it picked. Start QGIS from its own Start menu shortcut, never from a shell where you ran a conda activate.

Still broken?

AI Segmentation is the plugin of ours that installs PyTorch into the QGIS Python, so it is the one that can put this error in front of you. Its cloud modes run the models on our servers and need no local torch, which does not repair torch for any other plugin that wants it. Send the QGIS version, the full traceback and the plugin version to stephane.barbot@terra-lab.ai. We will tell you which of the four it is.

Questions people ask

Why does the error name _C when the missing file is fbgemm.dll?

_C is the torch extension module doing the import, so it sits at the top of the chain and Windows reports the failure there. The file named on the line above, fbgemm.dll or shm.dll, is normally present on disk. What that file depends on is not.

Which PyTorch versions are affected?

The Windows wheels from 2.4.0 through 2.8.0 ship fbgemm.dll. Version 2.9.0 dropped it, so moving up removes this particular failure. The _C wording can still appear for another reason, such as a CUDA build on a machine with no matching driver.

What is the difference between WinError 126 and WinError 127?

126 means Windows could not find the module or one of its dependencies, and that is this bug. 127 means the DLL loaded and a symbol inside it is missing, which is a version mismatch between two packages. None of the fixes above helps a 127.

Can I download libomp140.x86_64.dll from a DLL site?

No. Take it from the free Visual Studio Community install, which is where the people reporting the workaround on the PyTorch tracker get it. A copy from a download site is not the same file, and no PyTorch Windows wheel bundles this one anyway.

If you hit the same wording on GDAL, the _gdal version of this error has its own fixes. For what AI in QGIS does once torch loads, start at the QGIS AI hub.