Skip to content
QGIS
Troubleshooting

ImportError: DLL load failed while importing _C in QGIS on Windows

ImportError: DLL load failed while importing _C in QGIS on Windows

Install the Microsoft Visual C++ Redistributable (x64) from aka.ms/vs/17/release/vc_redist.x64.exe, then restart Windows. PyTorch ships compiled DLLs that need the Microsoft C runtime, and Windows refuses to load them when it is missing or out of date.

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.

Applies to Windows 10 and 11, QGIS 3.22 and later, any PyTorch 2.x installed into the QGIS Python or into a plugin environment.

Why it happens

Four causes print the same line. In the order they turn out to be the real one:

  1. The Visual C++ Redistributable (x64) is missing or old. fbgemm.dll and the other torch DLLs link against it.
  2. A second Python on your machine (Anaconda, Miniconda, a standalone install) puts its own torch DLLs where Windows finds them first. Since Python 3.8, extension DLLs come from os.add_dll_directory() rather than from PATH, and a mixed setup registers the wrong folder.
  3. The installed wheel does not match the machine. A CUDA build on a computer with no matching NVIDIA driver fails the same way.
  4. Antivirus or a corporate application-control policy quarantined one .pyd or .dll after the install finished.

To tell them apart, run where python in a Command Prompt. Several results point at cause 2. One result, and the error surviving a reboot after the redistributable, points at cause 3 or 4.

The fix

Install the Visual C++ Redistributable (x64)

Download vc_redist.x64.exe from Microsoft and run it. Take the x64 build even on a machine that also has the x86 one.

Restart Windows

A reboot, not just a QGIS restart. The DLL search list is read once per process, and the installer often leaves work for the restart.

Test the import on its own

Open the OSGeo4W Shell from the QGIS folder in the Start menu and run python -c "import torch". A clean prompt means it worked. Inside QGIS, the same test is Plugins, then Python Console, then import torch.

Remove the second Python from PATH

If where python returned more than one path, open Start, type "environment variables", open Edit the system environment variables, then Environment Variables, then Path. Take out the entries that belong to the Python you do not use in QGIS. Sign out and back in.

Reinstall the wheel that matches the machine

On a computer 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.

If that did not work

  • Some machines have the redistributable and still miss one dependency, libomp140.x86_64.dll. It arrives with Visual Studio Community, which is a free install. Do not download a loose DLL from a download site.
  • Check Windows Security, then Virus and threat protection, then Protection history. Restore anything it took from your site-packages folder, and add that folder to the exclusions before reinstalling.
  • In a conda environment, install torch with conda rather than pip, so the runtime libraries come from the same place as the package.