Fail to "import torch" on Windows debug build

I try to build pytorch from source on Windows, using Anaconda3, Ninja, and “DEBUG=1”. “python setup.py 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 www.python.org 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\torch_init_.py”, 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\random.py”, 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 setup.py develop” and set the following enviroments:

set DEBUG=1
set USE_DISTRIBUTED=0
set USE_MKLDNN=0
set USE_CUDA=0
set BUILD_TEST=0
set USE_FBGEMM=0
set USE_NNPACK=0
set USE_QNNPACK=0
set USE_XNNPACK=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 setup.py 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?