Can't import torch.nn - new installation

I just installed PyTorch on Windows Linux Subsystem (WSL):

root@TESLA:~# conda install pytorch torchvision -c soumith
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment /root/miniconda2:

The following NEW packages will be INSTALLED:

    pytorch:     0.1.11-py27_5 soumith
    torchvision: 0.1.8-py27_2  soumith

Proceed ([y]/n)? y

root@TESLA:~#
root@TESLA:~# python
Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>>
>>>
>>>
>>> import torch
>>>
>>> import torch.nn as nn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named nn
>>>
>>>
>>>
>>>
>>> exit()
root@TESLA:~#
root@TESLA:~# ll /root/miniconda2/pkgs/pytorch-0.1.11-py27_5/lib/python2.7/site-packages/torch
total 63M
drwxrwxrwx 2 root    0 Apr 26 08:55 autograd
drwxrwxrwx 2 root    0 Apr 26 08:55 backends
drwxrwxrwx 2 root    0 Apr 26 08:55 cuda
drwxrwxrwx 2 root    0 Apr 26 08:55 distributed
drwxrwxrwx 2 root    0 Apr 26 08:55 legacy
drwxrwxrwx 2 root    0 Apr 26 08:55 lib
drwxrwxrwx 2 root    0 Apr 26 08:55 multiprocessing
drwxrwxrwx 2 root    0 Apr 26 08:55 nn
drwxrwxrwx 2 root    0 Apr 26 08:55 optim
drwxrwxrwx 2 root    0 Apr 26 08:55 sparse
drwxrwxrwx 2 root    0 Apr 26 08:55 _thnn
drwxrwxrwx 2 root    0 Apr 26 08:55 utils
-rwxrwxr-x 1 root  63M Mar 31 09:53 _C.so
-rwxrwxr-x 1 root  18K Mar 31 09:53 _dl.so
-rw-rw-r-- 2 root 3.6K Mar 31 09:32 functional.py
-rw-rw-r-- 2 root 4.9K Mar 31 09:53 functional.pyc
-rw-rw-r-- 2 root 8.4K Mar 31 09:32 __init__.py
-rw-rw-r-- 2 root  11K Mar 31 09:53 __init__.pyc
-rw-rw-r-- 2 root  14K Mar 31 09:32 serialization.py
-rw-rw-r-- 2 root  14K Mar 31 09:53 serialization.pyc
-rw-rw-r-- 2 root 3.3K Mar 31 09:32 storage.py
-rw-rw-r-- 2 root 5.8K Mar 31 09:53 storage.pyc
-rw-rw-r-- 2 root  34K Mar 31 09:32 _tensor_docs.py
-rw-rw-r-- 2 root  30K Mar 31 09:53 _tensor_docs.pyc
-rw-rw-r-- 2 root  15K Mar 31 09:32 tensor.py
-rw-rw-r-- 2 root  19K Mar 31 09:53 tensor.pyc
-rw-rw-r-- 2 root  11K Mar 31 09:32 _tensor_str.py
-rw-rw-r-- 2 root  11K Mar 31 09:53 _tensor_str.pyc
-rw-rw-r-- 2 root 101K Mar 31 09:32 _torch_docs.py
-rw-rw-r-- 2 root 100K Mar 31 09:53 _torch_docs.pyc
-rw-rw-r-- 2 root 3.5K Mar 31 09:32 _utils.py
-rw-rw-r-- 2 root 4.0K Mar 31 09:53 _utils.pyc
-rw-rw-r-- 2 root   31 Mar 31 09:50 version.py
-rw-rw-r-- 2 root  172 Mar 31 09:53 version.pyc
root@TESLA:~#

Looks like I can import torch fine, but not any of its modules. Can anyone help me with this?

Just tried it on a standalone Ubuntu 14.04 machine: same problem.

Here’s my PATH:

root@Pascal:/home/michael/NN/PyTorch# echo $PATH
/root/miniconda2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

I noticed an interesting thing: being able to import torch modules depends on my current directory. Also, it depends on the presence of the pytorch source code file in the current directory. For example, I have torch.py file containing just two lines:

import torch
import torch.nn

If I try to execute this file “python torch.py” it will fail to import torch.nn module. If I remove this file from current directory, watch:

root@Pascal:/home/michael/NN#
root@Pascal:/home/michael/NN# python
>>> import torch
>>> import torch.nn
>>> exit()
root@Pascal:/home/michael/NN#
root@Pascal:/home/michael/NN# cp PyTorch/torch.py .
root@Pascal:/home/michael/NN#
root@Pascal:/home/michael/NN#
root@Pascal:/home/michael/NN# python
>>> import torch
>>> import torch.nn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named nn
>>>
>>> exit()
root@Pascal:/home/michael/NN#
root@Pascal:/home/michael/NN# rm torch.py*
root@Pascal:/home/michael/NN#
root@Pascal:/home/michael/NN# python
>>> import torch
>>> import torch.nn
>>>
>>> 

Weird, right?

this is expected. it’s how python works.

If you have a file torch.py in your current directory, it’ll take precedence for import.

You can debug these issues by printing which torch got imported.

>>> import torch
>>> print(torch)

Oh wow! I haven’t realized I can’t use name torch.py! Thanks!