Fail to "import torch" on Windows debug build

I try to build pytorch from source on Windows, using Anaconda3, Ninja, and “DEBUG=1”. “python install”. It failed with fatal error LNK1104 “LNK1104: ╬▐╖¿┤≥┐¬╬─╝■í░python37_d.libí▒”. I found the “python37_d.lib” is not in Anacoda. So in order to get “python37_d.lib” available, I installed python3.7 from and select “Download debug binaries”. Then I copied the whole newly installed python folder to Anaconda3\envs\gt37 and replace existing old files.
After seting enviroment variable “LIB” to the Anaconda3\envs\gt37\libs, I successully built the debug version.
However, when I execute “import torch” in python, it shows the following error:

(gt37) C:\Users\tegao\mygit\v0512\pytorch>python
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>>\ import torch
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\tegao\mygit\v0512\pytorch\”, line 247, in
from .random import set_rng_state, get_rng_state, manual_seed, initial_seed, seed
File “C:\Users\tegao\mygit\v0512\pytorch\torch\”, line 4, in
from torch._C import default_generator
ImportError: cannot import name ‘default_generator’ from ‘torch._C’ (unknown location)

Then I tried using python3.8, still failed with same error.
Then I tried using “python develop” and set the following enviroments:

set DEBUG=1
set USE_CUDA=0
set LIB=C:\Users\tegao\Anaconda3\envs\gt38\libs

This time the error changed when “import torch”:

(gt38) C:\Users\tegao\mygit\pytorch>python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import torch
Fatal Python error: _PyInterpreterState_Get(): no current thread state
Python runtime state: unknown

I make sure I have run “python clean” before each try.
But it could succeed on Ununtu. Building release version of pytorch on Windows could also succeed.
Is my method wrong(using self-installed python files to replace the ones under Anaconda in order to have pytorch37_d.lib)? How to successfully build debug version of pytorch on windows?

I had this issue before.

I uninstalled the anaconda

I installed again. But this time i selected the pip and specifically if you don’t have cuda select NONE

Thanks a lot! But install from pip can’t debug into the code. I want to step into the pytorch to learn the calling stack of some operators or functions. So I have to build from source.

I guess you can try set REL_WITH_DEB_INFO=1 instead of set DEBUG=1. In that way, you could also step into it and get the stack trace.


Hi, I have the same problem. Have you solved it?

Has anyone solved this issue. this is imp for dev. I also face same prb. With set DEBUG=1 build, I can;t import torch.

1 Like

@ptrblck Could you please take a look at this?

I’m unfortunately not familiar enough with developing on Windows, but did the suggestion from @peterjc123 work?

Yeah, actually it did. Thank you @peterjc123 and thank you @ptrblck for your response.
Still, I liked to know if it is possible to have the DEBUG mode running :slight_smile: