Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
AI and LLM integration

import torch on Windows fails with a DLL initialization error

Your error

OSError: [WinError 1114] A dynamic link library (DLL) initialization routine failed. Error loading "...\Lib\site-packages\torch\lib\c10.dll" or one of its dependencies.1 more line it can show

Fix it fast

Most common cause, 1 of 2

The Visual C++ runtime on the machine is older than 14.40

Install the newest supported x64 Visual C++ v14 Redistributable from Microsoft's permalink, then open a fresh terminal.

Fix
# download https://aka.ms/vc14/vc_redist.x64.exe, then in that folder:
.\vc_redist.x64.exe /install /passive /norestart

# or move to the release that carries the workaround
python -m pip install --upgrade "torch==2.15.0"
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

OSError [WinError 1114] on import torch, naming c10.dll or torch_python.dll, hits PyTorch 2.9 to 2.14 Windows wheels when the msvcp140.dll in the process is older than 14.40: an outdated Visual C++ Redistributable, or a package such as PyQt6 imported first with its own copy. Install the latest x64 redistributable, import torch first, or move to PyTorch 2.15.

  • Applies to PyTorch 2.15.0 (built with the mutex workaround), PyTorch 2.9.0 to 2.14.1 Windows wheels, Visual C++ v14 Redistributable, latest x64, Windows 10 and 11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

In a new PowerShell window with the project's environment active, print the installed torch version without importing it, read the Visual C++ runtime version that Windows has registered, then try import torch alone. A failing bare import points to cause 1; a bare import that works points to cause 2.

Run this first
python -c "import importlib.metadata as m; print('torch', m.version('torch'))"
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64" -Name Version).Version
python -c "import torch; print('import ok', torch.__version__)"

Then find the line you see in this list and open its fix:

  1. 1The Visual C++ runtime on the machine is older than 14.40The registry shows a version below v14.40 (the first report had 14.36), and even a bare python -c "import torch" fails
  2. 2Another package loaded its own older msvcp140.dll firstThe bare import works, but the app fails when PyQt6 or a similar library is imported before torch

What does the error look like?

  • OSError: [WinError 1114] A dynamic link library (DLL) initialization routine failed. Error loading "...\Lib\site-packages\torch\lib\c10.dll" or one of its dependencies.
  • Error loading "...\Lib\site-packages\torch\lib\torch_python.dll" or one of its dependencies.

Cause 1 of 2

The Visual C++ runtime on the machine is older than 14.40

You seeThe registry shows a version below v14.40 (the first report had 14.36), and even a bare python -c "import torch" fails

Check

WinError 1114 on import torch with a 2.9.0 to 2.14.x wheel comes from an msvcp140.dll older than 14.40. Those wheels were compiled with MSVC 14.42, whose std::mutex has a constexpr constructor that only runtime 14.40 and later understand, so c10.dll crashes during its own start-up. PyTorch's built-in runtime probe stays silent here, because the DLLs it looks for exist in every 14.x release.

Check
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64" -Name Version).Version

Fix

Install the newest supported x64 Visual C++ v14 Redistributable from Microsoft's permalink, then open a fresh terminal. Microsoft's rule: the installed runtime version has to match or exceed the build tools a program was compiled with. Upgrading to PyTorch 2.15 is the other route: its Windows build defines _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR, so the mutex part no longer needs 14.40. The current runtime is still the better first step, because it covers every DLL the wheels load.

Fix
# download https://aka.ms/vc14/vc_redist.x64.exe, then in that folder:
.\vc_redist.x64.exe /install /passive /norestart

# or move to the release that carries the workaround
python -m pip install --upgrade "torch==2.15.0"

Verify

The registry value now reads v14.40 or higher, and import torch prints a version number.

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

Confirmed by github.com

Did this fix it?

Cause 2 of 2

Another package loaded its own older msvcp140.dll first

You seeThe bare import works, but the app fails when PyQt6 or a similar library is imported before torch

Check

In this case the system runtime may be fully current. Windows keeps the msvcp140.dll that is already in the process, so a library that ships an older private copy decides the version once it is imported ahead of torch. PyTorch issue 166628 shows the pattern with PyQt6 on torch 2.9.0: flipping the two import lines makes the error come and go.

Check
python -c "from PyQt6.QtWidgets import QApplication; import torch"
python -c "import torch; from PyQt6.QtWidgets import QApplication"

Fix

Put import torch at the very top of the entry script, above the GUI toolkit and any other package that brings C++ runtime files. Moving to PyTorch 2.15 removes this ordering trap for the mutex problem.

Fix
# main.py
import torch  # must load before PyQt6
from PyQt6.QtWidgets import QApplication

Verify

The application window opens and torch reports its version from inside the same process.

Verify
python main.py

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Ship vc_redist.x64.exe /install /passive /norestart as a step in your Windows installer or setup script, so no machine keeps a runtime from before 14.40.
  • Pin torch in requirements and test import torch on a clean Windows machine before moving to a new minor, since the MSVC version behind the wheels can change (2.9.0 moved to MSVC 14.42).
  • Keep import torch as the first heavy import in entry points that also load a GUI toolkit or another library that ships its own C++ runtime files.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.