19

I'm working with Python 3.5.1 on a computer having CentOS Linux 7.3.1611 (Core) operating system.

I'm trying to use PyTorch and I'm getting started with this tutorial.

Unfortunately, the #4 line of the example creates troubles:

>>> torch.Tensor(5, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'Tensor'

I cannot understand this error... of course in Torch the 'torch' does have an attribute 'Tensor'. The same command works in Torch.

How can I solve this problem?

DavideChicco.it
  • 3,318
  • 13
  • 56
  • 84

6 Answers6

15

The Python binary that you are running does not have torch installed. It does have a directory named torch on the module search path, and it is treated as a namespace package:

$ pwd
/some/path
$ python3 -c 'import torch; print(torch); print(torch.__path__)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
$ mkdir torch
$ python3 -c 'import torch; print(torch); print(torch.__path__)'
<module 'torch' (namespace)>
_NamespacePath(['/some/path/torch'])

Any directory without a __init__.py file present in it, located on your module search path, will be treated as a namespace, provided no other Python modules or packages by that name are found anywhere else along the search path.

This means that if torch was installed for your Python binary, it doesn't matter if there is a local torch directory:

$ ls -ld torch/
drwxr-xr-x  2 mjpieters users  68 Nov 23 13:57 torch/
$ mkdir -p additional_path/torch/
$ touch additional_path/torch/__init__.py
$ PYTHONPATH="./additional_path" python3 -c 'import os.path as p, sys; print(*(t for t in (p.join(e, "torch") for e in sys.path) if p.exists(t)), sep="\n")'
torch
/some/path/additional_path/torch
$ PYTHONPATH="./additional_path" python3 -c 'import torch; print(torch); print(torch.__path__)'
<module 'torch' from '/some/path/additional_path/torch/__init__.py'>
['/some/path/additional_path/torch']

The above shows that sys.path lists the torch directory first, followed by additional_path/torch, but the latter is loaded as the torch module when you try to import it. That's because Python gives priority to top-level modules and packages before loading a namespace package.

You need to install torch correctly for your current Python binary, see the project homepage; when using pip you may want to use the Python binary with the -m switch instead:

python3.5 -m pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp35-cp35m-manylinux1_x86_64.whl 
python3.5 -m pip install torchvision

So replace the pip3 the homepage instructions use with python3.5 -m pip; python3.5 can also be the full path to your Python binary.

Do use the correct download.pytorch.org URL for the latest version.

You don't have to move the directory aside, but if you do want to and don't know where it is located, use print(torch.__path__) as I've shown above.

Again, note that if you do have an __init__.py file in a local torch directory, it becomes a regular package and it'll mask packages installed by pip into the normal site-packages location. If you have such a package, or a local torch.py single-file module, you need to rename those. The diagnostic information looks different in that case:

$ pwd
/some/path
$ python3 -c 'import torch; print(torch); print(torch.__path__)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
$ mkdir torch
$ touch torch/__init__.py  # make it a package
$ python3 -c 'import torch; print(torch); print(torch.__path__)'
<module 'torch' from '/some/path/torch/__init__.py'>
['/some/path/torch']
$ rm -rf torch/
$ touch torch.py           # make it a module
$ python3 -c 'import torch; print(torch); print(torch.__file__)'
<module 'torch' from '/some/path/torch.py'>
/some/path/torch.py

Note the differences; a namespace package, above, uses <module 'name' (namespace)>, while a regular package uses ), while a plain module uses`.

Such packages and modules (not namespace packages) are found first and stop the search. If the found package or module is not the one you wanted, you need to move them aside or rename them.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Thanks, but it does not work. I followed all your steps but nothing changed. Additional ideas? – DavideChicco.it Feb 16 '18 at 22:28
  • @DavideChicco it can you create a pastie or gist with your output, so we can diagnose what might be going wrong? Does `python3.5 -m pip freeze` list torch at all? – Martijn Pieters Feb 16 '18 at 23:15
  • Here's the output of `python3.5 -m -pip freeze`: `python3.5 -m pip freeze numexpr==2.6.1 numpy==1.11.1 olefile==0.44 pandas==0.18.1 Pillow==4.3.0 python-dateutil==2.5.3 pytz==2016.6.1 PyYAML==3.12 scikit-learn==0.17.1 scipy==0.17.1 six==1.10.0 tables==3.2.3.1 torch==0.2.0.post3 torchvision==0.1.9` – DavideChicco.it Feb 19 '18 at 14:43
  • @DavideChicco.it: so `torch` is there as version `0.2.0.post3`. Does `python3.5 -c 'import torch'` still produce the traceback in your question? – Martijn Pieters Feb 19 '18 at 14:54
  • `python3.5 -c 'import torch'` to works fine. Also `import torch` from the Python3.5 shell works fine. Any idea? Thanks – DavideChicco.it Feb 20 '18 at 15:55
  • @DavideChicco.it: I thought I had already responded, sorry for the delay. I asked for the wrong thing, `import torch` clearly worked before. What does `python3.5 -c 'import torch; print(torch)` produce? Does `torch.Torch` still fail with an attribute error? – Martijn Pieters Feb 22 '18 at 20:25
  • Thanks for your interest. `python3.5 -c 'import torch; print(torch)` prints nothing. `torch.Torch` prints/does nothing. – DavideChicco.it Feb 22 '18 at 20:58
  • @DavideChicco.it: that's.. **very** unexpected. `torch.Torch` is not a valid attribute (I meant to ask for `torch.Tensor`). Even the first one should produce output. I see I missed out a closing `'` quote, so perhaps the shell is waiting for more input. The correct commands are `python3.5 -c 'import torch; print(torch)'` and `python3.5 -c 'import torch; print(torch.Tensor)`. If the latter produces an attribute error, then the first one tells us what was imported instead of the expected package. – Martijn Pieters Feb 23 '18 at 14:42
  • Thanks. I ran `python3.5 -c 'import torch; print(torch)'` and I got: ``. I ran `python3.5 -c 'import torch; print(torch.Tensor)` and got `Traceback (most recent call last): File "", line 1, in AttributeError: module 'torch' has no attribute 'Tensor'`. Any suggestion? – DavideChicco.it Mar 02 '18 at 16:24
  • @DavideChicco.it: that's a change from before, you now have a `torch` package *in your homedirectory*. What is `/home/davide/torch/` and why is there a `__init__.py` file in it? It is now masking the versions installed by `pip`. – Martijn Pieters Mar 02 '18 at 16:30
  • `/home/davide/torch/` is my normal install folder for Torch. I think my problems might come up because I have installed Torch years ago and now I'm trying to install PyTorch, and there might be some conflict. What should I do? Thanks – DavideChicco.it Mar 02 '18 at 17:39
  • @DavideChicco.it rename or move that folder. – Martijn Pieters Mar 02 '18 at 18:13
  • @DavideChicco.it: You don't need to have Lua Torch installed to run PyTorch. Also, Python only adds `.` (the current working directory) to the Python path, so if you changed directories to somewhere else (`mkdir pytorch_workarea; cd pytorch_workarea`) then Python would also not try to import `torch` from the home directory. – Martijn Pieters Mar 07 '18 at 16:46
  • @DavideChicco.it: Last but not least, it is safe to rename the `torch` directory in your home directory. You can then edit the `. /home/davide/torch/bin/torch-activate` line in your `/home/davide/.bashrc` file to point to the new directory name, and re-run the `./install.sh -s` in the Lua Torch directory to update any local paths. – Martijn Pieters Mar 07 '18 at 16:48
3

I faced a similar issue when installing the pytorch module within my notebook. I had to restart my Jupyter Notebook Kernel which resolved the issue for me.

finn
  • 653
  • 3
  • 15
0

Looks like the notebook is running with python2.
See the metadata section of the notebook file (open in a text editor)

 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 }


FIX
1. Ensure the python3.5 kernel for ipython/jupyter is installed:
python3.5 -m ipykernel install --name python3.5.1

2. Run jupyter notebook and change kernel version in the notebook:
enter image description here



or hackily edit the <notebook.ipynb> directly (not recommended):

 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.5.1",
   "language": "python",
   "name": "python3.5.1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 }
stacksonstacks
  • 8,613
  • 6
  • 28
  • 44
0

I Faced similar issue. Just install pytorch in new conda environment. It will work You may have to install jupyter in new conda environment again, if you want to work on jupyter notebook.

0

I met this problem too, finally I found there is a "copy.py" in my directory, this name caused the chaos, I renamed the file, and problem solved.

-1

I got the same question, one .py works well and another report this error. Try to run the following command:

import torch
print(torch.__path__)
If (you get a wrong path): 
  then you know what you need to do.
Else if (even can not get the path printed): 
  just remove the temp file of your IDE(.idea or __pycache__). 

I do not know why, but it works for me. I hope it helps.

phwt
  • 1,356
  • 1
  • 22
  • 42