新手:vscode+anaconda运行pytorch出现的几个错误

网上很多用pycharm的,但我是vscode的死忠粉(●'◡'●)
vscode上的python配置是之前就弄好的,所以先整好能跑“ print("hello world") ”哈

1

先按照其它教程的步骤,安装 anaconda 和 pytorch ,到这一步:
1.png

可见,我建了一个名为pytorch的环境。

2

打开vscode,点击左下角选择解释器:
2

问题一:找不到torch模块

创建一个pytorch.py文件,输入:

import torch
报错:
3.png
解决办法:

将settings.json文件改为如下内容,注意其中的python项改成自己python的地址

{
    "code-runner.executorMap":{
        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "E:/DeepLearning/envs/pytorch/python.exe",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "scheme": "csi -script",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run",
        "lisp": "sbcl --script",
        "kit": "kitc --run",
        "v": "v run",
        "sass": "sass --style expanded",
        "scss": "scss --style expanded",
        "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
        "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    },
    "code-runner.runInTerminal": false
}

问题二:mkl-service模块导入失败blabla

和问题一相同的代码

出现很长很长的报错:

【E:\DeepLearning\envs\pytorch\lib\site-packages\numpy_init_.py:143: 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 assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import distributor_init
Traceback (most recent call last):
File "E:\DeepLearning\envs\pytorch\lib\site-packages\numpy\core_init
.py", line 22, in <module>
from . import multiarray
.......省略
ImportError: DLL load failed: �Ҳ���ָ����ģ�顣
During handling of the above exception, another exception occurred:
....省略
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
......省略
Please note and check the following:

  • The Python version is: Python3.7 from "E:\DeepLearning\envs\pytorch\python.exe"
  • The NumPy version is: "1.20.3"
    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: �Ҳ���ָ����ģ�顣】
解决办法:

是环境变量配置的问题,这边困扰我很久。
因为在安装anaconda时点了自动配环境变量,我一直默认自己环境变量没问题。

我的文件结构是这样的:
4

但是它自动配的路径是这样的:
5.png

其实应该配上我建立的名为pytorch环境相关的路径!
6.png

所以最终添加了一下path环境变量:
7.png

最终代码运行成功:
8.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容