What to do when "OS Error: [WinError 126] The specified module cannot be found" appears in import torch

Introduction

So far, I have mainly studied machine learning of table data, but I purchased the book "Development Deep Learning by Pytorch" to study image processing as well. Immediately, I installed Pytorch to execute the sample code, but I got a module import error when executing the code. I had a hard time resolving the event, so I will leave a remedy.

** * By the way, this is Qiita's first post **

Execution environment

OS: Windows10 Anaconda3 environment (Python 3.7.6)

Event that occurred

First, check the installation command of Pytorch corresponding to your environment on the official website of Pytorch, and execute the command on Anaconda Prompt.

In my case, the following command (Version is pytorch == 1.6.0 torchvision = = 0.7.0)

conda install pytorch torchvision cpuonly -c pytorch

After the installation of Pytorch is completed, when the sample code attached to the book is executed on Jupyter Notebook, the following error occurs on the line ** "import torch" **. (It has been confirmed that Pytorch is installed properly with the conda list command)

OSError:[WinError 126]The specified module cannot be found. Error loading"C:\Users\●●●\anaconda3\lib\site-packages\torch\lib\asmjit.dll" or one of its dependencies.

What I tried

I tried various things, but as a result, it was solved by installing the old version Pytorch of ④.

① Install with pip instead of conda

In the following article, when I reinstalled with pip, it was written that the error disappeared, so I installed it again with pip.

[Python] What to do if import torch fails due to an error

However, re-installing with pip did not solve the problem.

② Reinstall Anaconda

Since the Pytorch module existed properly in the folder where the module is installed, it is assumed that the path of the environment variable cannot be passed properly, or that it is affected by the Python version. However, installing Anaconda again did not resolve the issue.

③ Reinstall CUDA

When I installed CUDA about half a year ago, my PC's OS was still Windows 8.1. After that, I updated the OS to Windows 10, so I assumed that it was bad. However, installing CUDA for Windows 10 again did not solve the problem (Is it irrelevant because the sample code executed does not use GPU in the first place?).

④ Install the old version of Pytorch

In an overseas article, I found a case where installing an older version of Pytorch solved the problem, so I tried it.

error while import pytorch module. (The specified module could not be found.)

In my case, the following command (Version is pytorch == 1.5.0 torchvision == 0.60)

conda install pytorch==1.5.0 torchvision==0.6.0 -c pytorch

As a result, this has resolved the event that caused this error! I was able to execute the sample code as well.

It took a few days to resolve

It seems that it was solved in an instant when I wrote it in sentences, but it took me several days to solve it because I spent time changing the versions of Anaconda and CUDA in ② and ③ and reinstalling. (If I had tried ④ from the beginning, it would have been solved with haste ...)

There were few articles in Japanese that a similar event occurred, so I will leave it as a memo this time. Please refer to those who are having trouble with the same event.

Recommended Posts

What to do when "OS Error: [WinError 126] The specified module cannot be found" appears in import torch
When I import TensorFlow to Python, I get "Import Error: DLL load failed: The specified module cannot be found."
Error in deep learning "ImportError: DLL load failed: The specified module cannot be found. How to know the "specified module" in.
[Docker] What to do when error Couldn't find the binary git appears
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
What to do if you get the error Target WSGI script'/var/www/xxx/xxx.wsgi' cannot be loaded as python module
[Ubuntu 18.04 LTS] What to do when the screen resolution cannot be selected [NVIDIA]
[openpyxl] What to do when IllegalCharacterError appears in pandas.DataFrame.to_excel
What to do when "cannot import name xxx" [Python]
What to do when is not in the sudoers file.This incident will be reported.
What to do when "Type Error: must be _socket.socket, not socket" appears on GAE
Ll load failed in Anaconda environment construction: The specified module cannot be found. (import numpyas np file ....) Note that the error has been resolved.
What to do if the package dependency cannot be repaired
What to do when an error occurs with import _ssl
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
What to do when "Invalid HTTP_HOST header" appears in Django
What to do if you get Swagger-codegen in python and Import Error: No module named
What to do if ʻObject arrays cannot be loaded when allow_pickle = False` occurs in numpy.load ()
What to do when a video cannot be read by cv2.VideoCapture
[Beanstalk] What to do when an error occurs with import uuid
What to do when ModuleNotFoundError: No module named'XXX' occurs in Python
What to do when the value type is ambiguous in Python?
[For IT beginners] What to do when the rev command cannot be used with Git Bash
[Corrective Measures] error: [WinError 2] The specified file cannot be found. ERROR: Command errored out with exit status 1
What to do when the result downloaded via scrapy is in English
What to do when pyinstaller: error: argument --add-binary: invalid add_data_or_binary value: appears
What to do if No Python documentation found for ... appears in pydoc
[OSX] [pyenv] What to do when an SSL error occurs in pip
What to do if you get the message "" ~ .pkg "is corrupted and cannot be opened" when installing wxPython on Mac OS X
What to do if you get a "No versions found" error in pipenv
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
[python] What to do when an error occurs in send_keys of headless chrome
What to do when "TypeError: must be string, not int…" appears when using strptime
What to do when SSL error occurs in pip in Windows10, miniconda, VScode environment
What to do if pip cannot be installed
What to do when "TypeError: data type not understood" appears in python's numpy.zeros
What to do if you are told "Import Error: cannot import name'HTTPSHandler'" when building a virtual environment using virtualenv
What to do when [Errno 2] No such file or directory appears in Python
What to do when the graph does not appear in jupyter (ipython) notebook
Notes on what to do if "macOS 11 or later required!" Appears in Big Sur or pyarrow2.0.0 cannot be installed
What to do if a version error occurs in the selenium Chrome driver
What to do when only the window is displayed and nothing is displayed in pygame Note
What to do if the print command itself causes an error in Maya python
What to do if you get an error when importing matplotlib in Python (Mac)
When I try to import pandas on macOS I get the error No module named'_bz2'
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
[Go 1.13] What to do when unexpected directory layout: appears
When import matplotlib.pyplot cannot be done in pyenv-virtualenv environment
What to do if you install openCV3 and make a symbolic link, but when you do "import cv2", "No module named'cv2'" appears
[Small story] How to install the module when pip cannot be used due to proxy etc.
What to do if you get a Cannot retrieve metalink for repository error in yum
Dealing with Tensorflow error "Import Error: DLL load failed: Specified module not found" in deep learning
What to do if you get an error when running "certbot renew" in CakePHP environment
What to do when a Remove Error occurs when updating conda
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
Error that Qt plugin "cocoa" cannot be found in python-opencv
OSError: [Errno 40] What to do when Message too long appears
ImportError: No module What to do when you are told
What to do if you get an error saying c compiler cannot create executables in configure
What to do if you get a must override `get_config` error when trying to model.save in Keras