105

I tried to install torch using:

pip install torch

Installation started, but after a few seconds I got the error:

from tools.nnwrap import generate_wrappers as generate_nn_wrappers

ModuleNotFoundError: No module named 'tools.nnwrap'

OS: Windows

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Monu
  • 2,092
  • 3
  • 13
  • 26
  • 1
    What OS are you on? (Also, as a formatting note, you want to use a backtick (the key above the tab key), not a single quote for code blocks. ) – blackbrandt Jul 02 '19 at 21:04
  • 1
    Windows Anaconda > Environments base (root) > Press play for cmd window. wasn't working in powershell. `pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html` – Paul Totzke Apr 15 '20 at 04:09

17 Answers17

86

Anyone who is looking for the solution refer below:

It seems command to install torch not is working as expected, instead, you can try to install PyTorch using below command.

It's working and solved my above-mentioned issue.

Run below command(for below-specified OS, package-manager, Language):

# for OS: Windows, package-manager: pip, Language: python3.6 (below command is valid for only mentioned python 3.6)

pip3 install https://download.pytorch.org/whl/cu90/torch-1.1.0-cp36-cp36m-win_amd64.whl
pip3 install https://download.pytorch.org/whl/cu90/torchvision-0.3.0-cp36-cp36m-win_amd64.whl

For another version/type of the software (OS, package, Language) installed, the command must be generated from the below-mentioned link.

https://pytorch.org/get-started/locally/

Also, look for the Python version in your IDE(If you are using PyCharm) from the terminal using the command: python. If it returns 32bit this could happen, instead install Python 64-bit.

Monu
  • 2,092
  • 3
  • 13
  • 26
  • 9
    But why does one need such a workaround? What's wrong with the pytorch version on PyPI? – Frank Meulenaar Dec 28 '19 at 19:27
  • @FrankMeulenaar It looks like they want to install a version with/without CUDA support, depending on the hardware they have. Wish they would update the PyPI package to tell us this directly! – Josiah Yoder Jun 20 '20 at 20:59
  • 2
    Sadly, this did not work for me. Although the pip install succeeded, I still could not `import torch`. And [this is a common experience](https://github.com/pytorch/pytorch/issues/4827) – Josiah Yoder Jun 22 '20 at 12:29
  • 3
    And how can I fix this problem when "torch" should be mentioned as one of direct dependencies of "my_python_package" in its "requirements.txt"?? – Elias Oct 06 '20 at 11:13
  • You need to have version of Python that pytorch has compiled package of. For example in link https://download.pytorch.org/whl/torch_stable.html check cp36 cp37 etc. they refer to Pyton 3.6 and Python 3.7 etc.. If you have any other version of Python which there is no wheel for it you need to compile from source. – Gorkem Dec 01 '20 at 21:13
42

Follow the instructions on https://pytorch.org/get-started/locally/

Choose Package "Pip" if you want to pip install

It will give you output such as

pip3 install https://download.pytorch.org/whl/cu90/torch-1.1.0-cp37-cp37m-win_amd64.whl
pip3 install https://download.pytorch.org/whl/cu90/torchvision-0.3.0-cp37-cp37m-win_amd64.whl

Edit: new install instructions from pytorch.org shows commands such as

pip3 install torch===1.3.1 torchvision===0.4.2 -f https://download.pytorch.org/whl/torch_stable.html
Slim
  • 648
  • 4
  • 15
  • 5
    I actually feel kinda bad that I came to stack overflow for someone to tell me to go to the website and follow the instructions... *sigh...* if only being lazy weren't so quick and easy. – Nathan Chappell Jun 18 '20 at 09:13
  • I had to [use Python 3.7 rather than Python 3.8](https://stackoverflow.com/a/58857307/1048186) for this to work for me. – Josiah Yoder Jun 22 '20 at 12:40
  • I tried being lazy, but it seemed like too much effort. Any easier alternative? – Mark Lavin Nov 03 '20 at 15:29
  • I tried the install command above, then imported torch, no problem, but when I tried ```import torchvision``` I got that avcodec-58.dll is missing. Might the fact that I'm running Windows 7 be the problem? Any work-around? – Mark Lavin Nov 03 '20 at 15:36
15

To install stable version of pytorch in windows 10 for pycharm, following command should work.

pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html
Demotte
  • 664
  • 5
  • 14
13

For what it's worth, on Windows 10 in Anaconda, the only command which worked for me was:

pip3 install https://download.pytorch.org/whl/cu90/torch-1.0.1-cp37-cp37m-win_amd64.whl
Liz Av
  • 2,864
  • 1
  • 25
  • 35
Inna
  • 131
  • 1
  • 3
9

This worked for Windows 10 using Python-3.x pip

pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

Bussller
  • 1,961
  • 6
  • 36
  • 50
healer101
  • 131
  • 1
  • 3
6

For me this was fixed by checking my python version - I got this error with python 3.8, but the error went away when I downgraded to 3.7 (install instructions on the site only appear to go up to 3.7).

James Owers
  • 7,948
  • 10
  • 55
  • 71
  • `brew unlink python` and `brew switch python 3.7.5` for those wondering how to switch versions – gdavtor Jan 07 '20 at 17:23
  • [Slim's approach](https://stackoverflow.com/a/56944210/1048186) worked for me once I used Python 3.7 instead of Python 3.8. – Josiah Yoder Jun 22 '20 at 12:39
4

How I fix it in Windows 10 (x64)

31 March 2020

  1. Install CUDA from here: https://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal

    • Version: cuda_10.2.89_441.22_win10.exe
  2. Use official website pytorch: https://pytorch.org/get-started/locally/

    • This command in CMD: pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html
  3. In Anaconda3 I tried to use same website just change Package to conda:

    • Command in jupyter notebook: conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
    • enter image description here
    • And after restart it does NOT work
    • So I tried to use pip again in jupyter notebook: pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html
    • enter image description here
    • And it WORKS (if no, you can try to restart kernel):
    • enter image description here
Jackssn
  • 1,346
  • 1
  • 14
  • 16
2

I was getting the same error, then I found that the latest pytorch install requires CUDA 10.1, and once I installed that and the matching cuDNN, then this command line succeeded:

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
Chris
  • 1,101
  • 8
  • 13
  • Just a hint - now 10.2 is the latest cudatoolkit version, but it will change later, of course. Worked fine with Python 3.7. – JustAMartin Jul 28 '20 at 21:47
2

For me (Python 3.7.6 x64 on Windows 10) this line worked:

pip install torch==1.5.0 torchvision==0.6.0 -f https://download.pytorch.org/whl/torch_stable.html
meduz
  • 3,903
  • 1
  • 28
  • 40
2

If you are using Anaconda (3.7) in windows then you need to install via .whl file. Download .whl file from here:- https://download.pytorch.org/whl/torch_stable.html then put in the working directory and install it using pip.

For my system, I have renamed anaconda's pip to pip37

pip37 install torch-1.5.1+cpu-cp37-cp37m-win_amd64.whl
pip37 install torchvision-0.6.1+cpu-cp37-cp37m-win_amd64.whl

It will install Torch 1.5 & Torchvision 0.6.1 in your anaconda environment

Kushal Bhavsar
  • 388
  • 3
  • 6
2

Try this code. It worked for me-

pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
2

To whoever that has problem with torch installation: I think instead of following any of the commands posted here, you better first check pytorch official website (https://pytorch.org/). There you can easily specify all your setup info and get the proper command for download.

pegah
  • 791
  • 8
  • 15
  • 1
    The downvotes seem to indicate a problem with this answer. Is there anything I can do to address this? I found all the other posted commands accepted as OK by others to be found exactly from pytorch website itself, so I thought of sharing. – pegah Nov 19 '20 at 12:20
2

for the latest version of torch use the command below.

Without CUDA Support(CPU only):

pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

With CUDA Support:

pip install torch===1.7.0+cu110 torchvision===0.8.1+cu110 torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

Check this link for the latest versions and get the command there itself from pytorch.

Venkatesh Dharavath
  • 500
  • 1
  • 5
  • 18
1

i got rid of this in next way: uninstall Python 32 bit and install 64bit.

Here you can find all versions

And here you can see pip/conda etc cmd commands to run to install torch

zzfima
  • 1,528
  • 1
  • 14
  • 21
0

Had to fix this at two locations:

DEV windows 10 machine: CD to C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\Scripts execute: conda install pytorch

DEV Windows MSSQL Server Database Python Services CD to C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\Scripts execute: pip3 install torch

0

Try to uninstall python 3.8 32-bit and install 64-bit. it worked for me, then:

pip3 install numpy
pip3 install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
Nightcap79
  • 620
  • 7
  • 13
0

I got the same error and fixed it with:

pip install torch===1.7.0 torchvision===0.8.1 torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

I'm on Windows 10, Python 3.8.6.

Alternatively:

  1. Go to https://pytorch.org/
  2. Scroll down to the center of the page where you'll find a section with header "INSTALL PYTORCH"
  3. Select your preferences
  4. Copy the command to the right of where it says: "Run this Command:"
  5. Paste it on your termminal
  6. Click enter (I know, sounds obvious, but it wasn't for my old self when I first started)
Leobeeson
  • 500
  • 6
  • 13