配置好torch环境后,import torch,torchvision,numpy却报错

配置好torch环境后,import torch,torchvision,numpy却报错

  • 报错内容
  • note
  • 解决方法--添加环境变量
  • 问题解决啦!!

报错内容

1、import torch

Please note and check the following:

  * The Python version is: Python3.7 from "D:\anaconda\envs\qr_env\python.exe"
  * The NumPy version is: "1.19.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed: 找不到指定的模块。

2、import torchvision

Traceback (most recent call last):
  File "D:/qr_project/encoder/test.py", line 1, in <module>
    import torchvision
  File "D:\anaconda\envs\qr_env\lib\site-packages\torchvision\__init__.py", line 5, in <module>
    from torchvision import models
  File "D:\anaconda\envs\qr_env\lib\site-packages\torchvision\models\__init__.py", line 1, in <module>
    from .alexnet import *
  File "D:\anaconda\envs\qr_env\lib\site-packages\torchvision\models\alexnet.py", line 1, in <module>
    import torch
  File "D:\anaconda\envs\qr_env\lib\site-packages\torch\__init__.py", line 189, in <module>
    from torch._C import *
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 0: invalid continuation byte

3、import numpy

Traceback (most recent call last):
  File "D:\anaconda\envs\qr_env\lib\site-packages\numpy\core\__init__.py", line 22, in <module>
    from . import multiarray
  File "D:\anaconda\envs\qr_env\lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
    from . import overrides
  File "D:\anaconda\envs\qr_env\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed: 找不到指定的模块。

note

配置好torch环境后,import torch,torchvision,numpy却报错_第1张图片
###值得注意的是,本案例通过anaconda创建自己的环境,并且安装torch、torchvision等包,在anaconda prompt命令窗口下是可以正常导入的,而在pycharm编译器中则会报错。
###通过activate qr_env 进行自己创建的conda 环境中,输入python会显示环境中python的版本信息,再依次import,显然不会报错。这是因为在anaconda的环境下有支持import torch的dll,而在pycharm中使用的是你创建的新环境,还缺少相应支持的dll,你仅需在pycharm中添加环境变量:

解决方法–添加环境变量

配置好torch环境后,import torch,torchvision,numpy却报错_第2张图片
配置好torch环境后,import torch,torchvision,numpy却报错_第3张图片
添加PYTHONPATH环境变量,并将你安装的anaconda下的D:\anaconda;
D:\anaconda\Library\mingww64\bin;
D:\anaconda\Library\user\bin;
D:\anaconda\Library\bin;
D:\anaconda\Library\Scripts;D:\anaconda\Lib\site-packages
添加到Value中

问题解决啦!!

你可能感兴趣的:(深度学习,Pytorch环境配置,python,anaconda,pytorch)