Cannot import Pytorch [WinError 126] The specified module could not be found
Asked Answered
A

9

27

I'm trying to do a basic install and import of Pytorch/Torchvision on Windows 10. I installed a Anaconda and created a new virtual environment named photo. I opened Anaconda prompt, activated the environment, and I ran:

(photo) C:\Users\<user>\anaconda3\envs>conda install pytorch torchvision cudatoolkit=10.2 -c pytorch**

This installed pytorch successfully. Running conda list I see:

  pytorch            pytorch/win-64::pytorch-1.5.0-py3.7_cuda102_cudnn7_0

  torchvision        pytorch/win-64::torchvision-0.6.0-py37_cu102

Then I open a python command prompt while in the virtual environment, and type:

import torch

The following error is printed:

Traceback (most recent call last): File "", line 1, in File "C:\Users\njord\anaconda3\envs\photo\lib\site-packages\torch__init__.py", line 81, in ctypes.CDLL(dll) File "C:\Users\njord\anaconda3\envs\photo\lib\ctypes__init__.py", line 364, in init self._handle = _dlopen(self._name, mode) OSError: [WinError 126] The specified module could not be found

I have uninstalled/reinstalled python and anaconda but still run into the same issue. Advice appreciated.

Animadversion answered 28/4, 2020 at 19:57 Comment(0)
C
29

Refer to below link: https://discuss.pytorch.org/t/cannot-import-torch-on-jupyter-notebook/79334

This is most probably because you are using a CUDA variant of PyTorch on a system that doesn’t have GPU driver installed. That is to say, if you don’t have a Nvidia GPU card, please install the cpu-only package according to the commands on https://pytorch.org.

Conda

conda install pytorch torchvision cpuonly -c pytorch

Pip

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

Cassandry answered 12/5, 2020 at 15:45 Comment(8)
Hi, I'm having the same problem but I do have a dedicated Nvidia card. I tried forcing conda and python to use that card instead of the intel integrated one, but that didn't help either.Bobseine
@YechiamWeiss It rather sounds as if your BIOS is not in the default mode? Because when I put in a cuda supporting graphics card, it automatically switched to the new card, with the old integrated card being disabled by default. You could enable both cards only by leaving the default.Sift
@Lorenz I'm not entirely sure; ever since I got my computer it had the auto-select feature from nvidia that chooses which card to use depending on the application used. But I figured out the issue, I stupidly forgot that I need to actually download the Cuda driver :)Bobseine
@YechiamWeiss That is not stupid. See superuser.com/questions/1572640/…. I had the same stupidity, by accident?? This bad information policy cost me many hours as well. I have opened a lot of questions around cuda and cudatoolkit only because no one is telling you clearly in an overview how to use which cuda install (and if at all) in practice. Now my many questions about it have partly led to my account being banned for questions.Sift
@YechiamWeiss For example, the standalone conda cudatoolkit should not be installed for pytorch. Pytorch has its own binary install of that cudatoolkit (incl. cuDNN), it should be installed directly with the respective parameter to get the dependencies right. Even up to now, I still do not know if I must install pytorch and tensorflow in separated environments if I want to install both, due to their possibly different dependencies on cuda, or whether the environments are just the recommendation to get a better package organisation.Sift
@Lorenz Yeah it's not very clear, even in PyTorch tutorials, if and how exactly Cuda is inserted in this mess. Thanks anyway :)Bobseine
The question if you need to install Tensorflow and Pytorch in differents conda environments is now open at superuser.com/questions/1577853/….Sift
Worked for me after I followed @YechiamWeiss comment regarding installing the Cuda driver manually (developer.nvidia.com/…)Salado
P
6

Uninstall your pytorch which you have installed and try this using conda

conda install PyTorch -c PyTorch

If it wasn't work run this code in cmd

pip3 install torchvision
Prow answered 28/4, 2020 at 20:29 Comment(8)
Same issue in both scenarios.Animadversion
I now uninstalled cudatoolkit, torch and torchvision and reinstalled with pip, same issue.Animadversion
Try to run anaconda as adminProw
I reran the above steps while running Anaconda Prompt as admin, no luck again...Animadversion
Uninstall all libraries which are related to pytorch and including pytorch. Then run this command conda install pytorch torchvision cudatoolkit=9.2 -c pytorch -c defaults -c numba/label/dev Prow
And also make sure you gpu support for cudaProw
I don't know about you but I have this problem only when trying to import torch when inside ipython cmd line or when trying to run python scripts. When I open Jupyter lab or notebook I can import torch without problemsFinbur
For how to fully uninstall pytorch, see #43664944.Sift
C
5

If you want to run on the CPU and tried all of the above solutions but it is not working, you can try to use this command:

pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/cpu

It will to download torch with CPU only support.

Celibacy answered 15/5 at 10:31 Comment(2)
Tried without the versions, didn't work. With the versions: suddenly successful.Orlantha
same with @Orlantha with the versions it work. why?Perryperryman
P
2

You should use the command line to install PyTorch on Windows or any supported platform. It is trivial:

  • Open URL in web browser https://pytorch.org/
  • Find the "Quick Start Locally" section
  • Select your platform
  • Run the command on your end (highlighted in yellow)

enter image description here

This way you can install it with any following package:

  • Conda
  • Pip
  • LibTorch
  • Source
Pliocene answered 18/9, 2020 at 14:10 Comment(0)
C
2

If everything you are trying is not working, then try older version of torch and torch vision. I was initially trying:

pip install torch torchvision

which installs the latest version and does not work!

When I switched to:

pip install torch==2.3.1 torchvision==0.18.1

it worked easily.

Claro answered 1/8 at 10:11 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Nonconformist
Wow, it also worked for me! Thank you for the insight!Lillylillywhite
M
1

Encountered the same issue. I used the fix suggested in by the link below: (Fix 2 worked for me)

https://discuss.pytorch.org/t/winerror-126-when-import-torch/80249/8

Fixes:

1.Update to Python 3.8 from 3.7

2.Apply https://github.com/pytorch/pytorch/pull/37763 manually.

Replace your local copy with https://gist.github.com/peterjc123/bcbf4418ff63d88e11313d308cf1b427 (e.g. C:\ProgramData\Anaconda3\Lib\site-packages\torch)

Monotonous answered 27/5, 2020 at 7:24 Comment(1)
Not sure if step (2) is good, as the latest comment on the pull request says: This PR were reverted, as it caused TestTypeHints failure, see for example app.circleci.com/pipelines/github/pytorch/pytorch/167977/…Bobseine
F
1

I have installed Microsoft Build Tools from https://visualstudio.microsoft.com/visual-cpp-build-tools/ I have checked all boxes regarding C++ section.

Before this I could only import torch from Jupyter notebook but not from ipython or python shell. I don't why that's so but it works now.

Finbur answered 22/7, 2020 at 22:1 Comment(0)
R
1

You can try using pip instead of pip3 on whatever version you are trying to install

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117

Realism answered 2/6, 2023 at 17:53 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Nonconformist
Can you elaborate a little?Feinstein
P
1

I fix this error by installing Microsoft Visual C++ Redistributable latest version was 2015 to 2022.

Pismire answered 15/7 at 15:2 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Nonconformist

© 2022 - 2024 — McMap. All rights reserved.