Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

AI and LLM integration · Decision guide

import torch crashes on the line that loads torch._C

Short answer

When import torch fails at from torch._C import *, Python could not load PyTorch's compiled core. On Windows the usual cause is a missing Microsoft Visual C++ Redistributable, so install the x64 package. Running Python inside a PyTorch source checkout, or an LD_LIBRARY_PATH that points at an older CUDA on Linux, gives the same line. Applies to PyTorch 2.15.

  • Applies to PyTorch 2.15.0 (pip wheels), Python 3.11 to 3.15, Windows x64, Linux x86_64
  • Updated
  • By Maaz Zindani, Senior Software Engineer

from torch._C import * is the line in PyTorch's __init__.py that loads the compiled core. When it fails, Python found the torch package but could not load the native library inside it. The cause depends on the operating system and on which folder you started Python in.

Ask Python which torch it would import before changing anything. The path it prints sorts you into one of the cases below.

Find your case

  1. If you are on Windows and see a Visual C++ Redistributable warning or DLL load failedThe C++ runtime files are missing.
  2. If you cloned the PyTorch repository and Python starts inside itThe repository's own torch folder shadows the install.
  3. If you are on Linux and see undefined symbol in libcusparse, libcublas or libnvJitLinkAn older CUDA from LD_LIBRARY_PATH is found first.

Find out which torch Python picks

find_spec locates the module without running it. A path inside site-packages on Windows points to the first case, a path under your current folder to the second, a site-packages path on Linux to the third.

Which torch, on which system

python -c "import importlib.util, platform; print(platform.system(), importlib.util.find_spec('torch').origin)"

Windows needs the Visual C++ runtime

The pip wheel does not bundle the Microsoft C++ runtime. PyTorch's start-up code tries to load vcruntime140.dll and msvcp140.dll first and prints a warning when it cannot, and the PyTorch Windows FAQ lists this import error.

Install the latest supported x64 Visual C++ v14 Redistributable from Microsoft, then open a new terminal so the DLLs are found.

Check for the runtime, then install it

python -c "import ctypes; [ctypes.CDLL(d) for d in ('vcruntime140.dll', 'msvcp140.dll', 'vcruntime140_1.dll')]; print('runtime found')"
Start-Process "https://aka.ms/vc14/vc_redist.x64.exe"

Python started inside a PyTorch checkout

A cloned PyTorch repository contains its own torch folder. Run Python from the repository root and that folder wins over the installed package. Its plain _C folder is not the compiled library, so PyTorch stops with a message about the torch/_C folder of the repository.

Move to another directory before starting Python. When you are building PyTorch from that checkout on purpose, install it in editable mode, as the error text suggests.

Leave the repository, or build in place

cd ~
python -c "import torch; print(torch.__file__)"
# or, when you build from the checkout:
python -m pip install --no-build-isolation -v -e .

Linux and an old CUDA in LD_LIBRARY_PATH

Linux CUDA wheels load their NVIDIA libraries from pip packages. A system CUDA toolkit listed in LD_LIBRARY_PATH can be found first, and the PyTorch source describes the result: a newer libcusparse paired with an older libnvJitLink, ending in an undefined symbol at the torch._C import.

Test the theory by starting Python without the variable. If that works, remove the old toolkit entry from the file that exports it.

Prove it, then find the line to delete

echo "$LD_LIBRARY_PATH"
env -u LD_LIBRARY_PATH python -c "import torch; print(torch.__version__)"
grep -n "LD_LIBRARY_PATH" ~/.bashrc ~/.profile

Open a new terminal after editing, so the old value is gone. Then check torch.cuda.is_available() and a small tensor on the GPU.

A NumPy message is a different problem

If the import fails with a NumPy message instead, the cause is a NumPy build mismatch, not the C++ runtime or CUDA paths, and a different fix applies.

In short

  • The failing line only says the compiled core would not load.
  • Windows: install the x64 Visual C++ runtime. Anywhere: do not start Python inside a PyTorch checkout.
  • Linux: drop an old CUDA path from LD_LIBRARY_PATH when using pip CUDA wheels.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. 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.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.