網(wǎng)上很多用pycharm的,但我是vscode的死忠粉(●'?'●)
vscode上的python配置是之前就弄好的沦辙,所以先整好能跑“ print("hello world") ”哈
1
先按照其它教程的步驟猜煮,安裝 anaconda 和 pytorch 笔时,到這一步:可見(jiàn),我建了一個(gè)名為pytorch的環(huán)境。
2
打開(kāi)vscode蛛淋,點(diǎn)擊左下角選擇解釋器:問(wèn)題一:找不到torch模塊
創(chuàng)建一個(gè)pytorch.py文件,輸入:
import torch
報(bào)錯(cuò):
解決辦法:
將settings.json文件改為如下內(nèi)容枕磁,注意其中的python項(xiàng)改成自己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
}
問(wèn)題二:mkl-service模塊導(dǎo)入失敗blabla
和問(wèn)題一相同的代碼
出現(xiàn)很長(zhǎng)很長(zhǎng)的報(bào)錯(cuò):
【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: ????????????顣】
解決辦法:
是環(huán)境變量配置的問(wèn)題嗡善,這邊困擾我很久辑莫。
因?yàn)樵诎惭banaconda時(shí)點(diǎn)了自動(dòng)配環(huán)境變量,我一直默認(rèn)自己環(huán)境變量沒(méi)問(wèn)題罩引。
其實(shí)應(yīng)該配上我建立的名為pytorch環(huán)境相關(guān)的路徑各吨!
所以最終添加了一下path環(huán)境變量:
最終代碼運(yùn)行成功: