AttributeError: module 'torch' has no attribute 'randn'

minister_79: <p>I have installed Pytorch, but have been geting the above error. I installed it but still did not work. Please, how can I solve this problem? Thank you.</p>

ptrblck: <p>Are other PyTorch methods working?<br>
Which version are you using? You can see it using <code>print(torch.__version__)</code>.</p>

minister_79: <p>Only import torch method is working. In fact when I try printing the pytorch version, I get the following error: AttributeError: module ‘torch’ has no attribute ‘<strong>version</strong>’</p>

ptrblck: <p>Could you try to print <code>torch.__file__</code>?</p>

minister_79: <p>I did that and this is the error it returned: AttributeError: module ‘torch’ has no attribute ‘<strong>file</strong>’</p>

ptrblck: <p>OK, in that case it looks like your PyTorch install is broken.<br>
How did you install it? Could you try to remove PyTorch completely and install it again?<br>
You can find the instructions on the <a href="https://pytorch.org/get-started/locally/" rel="nofollow noopener">website</a>.</p>
<p>If you are using conda, I would recommend to create a new virtual environment for the new install and see if it’s working.</p>

minister_79: <p>Thanks. I followed the instruction for uninstalling it using pip uninstall torch, but didn’t work, how do I go about it?</p>

ptrblck: <p>Did you called <code>pip uninstall torch</code> twice?<br>
What kind of message did you get?<br>
Could you additionally try <code>conda uninstall pytorch</code>?</p>
<p>Did you use pip, conda to install PyTorch or did you build it from source?</p>

minister_79: <p>Thank you. I’m surprised I did exactly that twice as you said (bash-3.2$ pip uninstall torch), but returns the following:<br>
Skipping torch as it is not installed.<br>
You are using pip version 10.0.1, however version 18.1 is available.<br>
You should consider upgrading via the ‘pip install --upgrade pip’ command.</p>
<p>import torch doesn’t return error, but throws error when importing other packages.</p>

ptrblck: <p>What do you get if you run <code>conda list | grep torch</code>?</p>

minister_79: <p>It returns nothing (no error)</p>

ptrblck: <p>Thanks for the info.<br>
Could you run the same again using <code>pip</code>?</p>
<pre><code class="lang-bash">pip list | grep torch
</code></pre>
<p>If you don’t find any <code>torch</code> or <code>torch_nightly</code> installations, I’m not sure what to do besides maybe reinstalling conda and try to install PyTorch again.</p>

javaria-qadeer: <p>hi! i am tring to print the pytorch version it  gives an error while print(pytorch file ) return the file location. what should i do to resolve this issue</p>

ptrblck: <p>You can print the version via <code>print(torch.__version__)</code>. The <code>.__file__</code> attribute was just a debugging step for the original issue.</p>

Echizen: <p>How do you name your file? I named it as torch.py and it tried to import this file. Of course it has nothing there. The solution could be: 1. install pytorch properly; 2. rename your python file other than torch.py.</p>
<p>Ref: <a href="https://discuss.pytorch.org/t/macos-conda-install-fails/76181" class="inline-onebox">MacOS conda install fails</a></p>

BastiNi: <p>I had the same issue and for me, it was caused by the Conda environment being called “torch”. I got Pytorch to work by deleting the environment and creating a new one called torchenv</p>
<pre><code class="lang-auto">conda deactivate torch
conda env remove -n torch
conda create -n torchenv python=3.9
conda activate torchenv
pip install torch
</code></pre>
<p>hope this helps <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>

H-eXcelsior-T: <p>You can check if your py file is called “torch.py”. If so, it is probably due to a naming conflict. Try renaming it to something like “torch1.py”.</p>