[Solved] pytorchImportError: numpy.core.multiarray failed to import

Project scenarios

Recently, I’m learning Python . First, I installed the following with the CONDA command provided on the official website:

conda install pytorch torchvision cudatoolkit=10.2 -c pytorch

Then test whether the installation is successful and whether it can be used normally according to the method of the official website

import torch
x = torch.rand(5, 3)
print(x)

If the value of x can be printed normally, it means that it can be used normally, otherwise it is the opposite

Problem description

Error in code operation: importerror: numpy.core.multiarray failed to import

D:\miniconda3\envs\pytorch\lib\site-packages\numpy\__init__.py:138: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL 
initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not 
  from . import _distributor_init
Traceback (most recent call last):
  File "c:/Users/ghgxj/Desktop/pytorch/1_get_started.py", line 1, in <module>
    import torch
  File "D:\miniconda3\envs\pytorch\lib\site-packages\torch\__init__.py", line 189, in <module>
    from torch._C import *
ImportError: numpy.core.multiarray failed to import

Cause analysis

The Python environment comes with a numpy , which will conflict with the numpy version installed with Python

Solutions

Uninstall numpy first

conda uninstall numpy

reinstall numpy

conda install numpy

The final result

According to the above scheme, the values of x are printed normally

tensor([[0.8338, 0.1541, 0.0379],
        [0.4348, 0.0145, 0.3586],
        [0.4098, 0.2363, 0.5405],
        [0.7372, 0.7418, 0.3703],
        [0.5668, 0.9512, 0.8041]])

Let’s see if GPU driver and CUDA can be used:

import torch
print(torch.cuda.is_available())

If the console print shows true , it means it can be used normally

 

Similar Posts: