关于anaconda+VScode+pytorch,import命令可以在终端正常运行,但是VScode显示No module

转载,原文链接
在安装annconda时并没有同时安装VScode,而是后期单独安装,在settings.json文件中添加了

"python.pythonPath": "D://anaconda3//envs//pytorch1.7//python.exe"
  • 在VScode terminal运行,输出正确
    关于anaconda+VScode+pytorch,import命令可以在终端正常运行,但是VScode显示No module_第1张图片
  • 右击->run code 报错
ModuleNotFoundError: No module named 'torch'

解决办法(参考文章开头链接)

终端运行没问题,run code有问题,所以需要修改run code配置

1. 打开settings.json,将下面配置注释掉

"python.pythonPath": "D://anaconda3//envs//pytorch1.7//python.exe"

2. 接着输入

"code-runner.executorMap"

后面会出现

"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": "python -u",
        "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"
    }

3. 修改路径

这段中间有

 "python": "python -u",

修改成自己的python路径,即

 "D://anaconda3//envs//pytorch1.7//python.exe"

4. 运行成功

关于anaconda+VScode+pytorch,import命令可以在终端正常运行,但是VScode显示No module_第2张图片

你可能感兴趣的:(deepLearning,anaconda,python,pytorch,visual,studio,code)