Fail to "import torch" on Windows debug build

teng: <p>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 <a href="http://www.python.org" rel="noopener nofollow ugc">www.python.org</a> and select “Download debug binaries”. Then I copied the whole newly installed python folder to Anaconda3\envs\gt37 and replace existing old files.<br>
After seting enviroment variable “LIB” to the Anaconda3\envs\gt37\libs, I successully built the debug version.<br>
However, when I execute “import torch” in python, it shows the following error:</p>
<blockquote>
<p>(gt37) C:\Users\tegao\mygit\v0512\pytorch&gt;python<br>
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32<br>
Type “help”, “copyright”, “credits” or “license” for more information.<br>
&gt;&gt;&gt;\ import torch<br>
Traceback (most recent call last):<br>
File “”, line 1, in <br>
File “C:\Users\tegao\mygit\v0512\pytorch\torch_<em>init</em>_.py”, line 247, in <br>
from .random import set_rng_state, get_rng_state, manual_seed, initial_seed, seed<br>
File “C:\Users\tegao\mygit\v0512\pytorch\torch\random.py”, line 4, in <br>
from torch._C import default_generator<br>
ImportError: cannot import name ‘default_generator’ from ‘torch._C’ (unknown location)</p>
</blockquote>
<p>Then I tried using python3.8, still failed with same error.<br>
Then I tried using “python setup.py develop” and set the following enviroments：</p>
<p>set DEBUG=1<br>
set USE_DISTRIBUTED=0<br>
set USE_MKLDNN=0<br>
set USE_CUDA=0<br>
set BUILD_TEST=0<br>
set USE_FBGEMM=0<br>
set USE_NNPACK=0<br>
set USE_QNNPACK=0<br>
set USE_XNNPACK=0<br>
set LIB=C:\Users\tegao\Anaconda3\envs\gt38\libs</p>
<p>This time the error changed when “import torch”:</p>
<blockquote>
<p>(gt38) C:\Users\tegao\mygit\pytorch&gt;python<br>
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32<br>
Type “help”, “copyright”, “credits” or “license” for more information.<br>
&gt;&gt;&gt; import torch<br>
Fatal Python error: _PyInterpreterState_Get(): no current thread state<br>
Python runtime state: unknown</p>
</blockquote>
<p>I make sure I have run “python setup.py clean” before each try.<br>
But it could succeed on Ununtu. Building release version of pytorch on Windows could also succeed.<br>
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?</p>

jmandivarapu1: <p>I had this issue before.</p>
<p>I uninstalled the anaconda</p>
<p>I installed again. But this time i selected the pip and specifically <strong>if you don’t have cuda select NONE</strong></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c1426e9509bc1c3f6cb7645553e980f579b7a06.png" data-download-href="https://discuss.pytorch.org/uploads/default/2c1426e9509bc1c3f6cb7645553e980f579b7a06" title="Screen Shot 2020-05-15 at 1.56.33 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c1426e9509bc1c3f6cb7645553e980f579b7a06_2_690x493.png" alt="Screen Shot 2020-05-15 at 1.56.33 PM" data-base62-sha1="6hWbpTpws66wweriEG7fMc4dIz4" width="690" height="493" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c1426e9509bc1c3f6cb7645553e980f579b7a06_2_690x493.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c1426e9509bc1c3f6cb7645553e980f579b7a06_2_1035x739.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c1426e9509bc1c3f6cb7645553e980f579b7a06_2_1380x986.png 2x" data-dominant-color="F3E7E7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2020-05-15 at 1.56.33 PM</span><span class="informations">1862×1332 216 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

teng: <p>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.</p>

peterjc123: <p>I guess you can try <code>set REL_WITH_DEB_INFO=1</code> instead of <code>set DEBUG=1</code>. In that way, you could also step into it and get the stack trace.</p>

ayl: <p>Hi, I have the same problem. Have you solved it?</p>

Kareem_Shaik: <p>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.</p>

taless474: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Could you please take a look at this?</p>

ptrblck: <p>I’m unfortunately not familiar enough with developing on Windows, but did the suggestion from <a class="mention" href="/u/peterjc123">@peterjc123</a> work?</p>

taless474: <aside class="quote no-group" data-username="peterjc123" data-post="4" data-topic="81347">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/peterjc123/48/2790_2.png" class="avatar"> peterjc123:</div>
<blockquote>
<p>set REL_WITH_DEB_INFO=1</p>
</blockquote>
</aside>
<p>Yeah, actually it did. Thank you <a class="mention" href="/u/peterjc123">@peterjc123</a> and thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> for your response.<br>
Still, I liked to know if it is possible to have the <code>DEBUG</code> mode running <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

seven-mile: <p>still broken in 2023 &gt; &lt;</p>

odionne: <p>Seems like this is still broken in 2024 but env setting below works</p>
<pre><code class="lang-auto">set REL_WITH_DEB_INFO=1
</code></pre>