問題描述
win10下vscode python導入失敗 (vscode python import failure in win10)
我一直在使用 Visual Studio Code 開發一些 python 程序。我導入的模塊包括 numpy
和 cssutils
。有時 VS CODE 要求我通常同意的升級。然後今天VS CODE突然找不到cssutils
。我和我的學生檢查了所有的環境變量,路徑變量,並在互聯網上進行了調查,但沒有找到任何線索。有人說這是作者聲稱已修復但實際上沒有修復的 VS CODE 錯誤。有人建議在 launch.json
文件和 setting.json
中添加一些聲明。
我們嘗試了所有方法,但都沒有成功。我們也關閉了PC,重新運行了vscode。
最後,我們卸載了cssutils
並再次 pip 安裝它。然後問題就變成了 numpy
無法導入。然後我們嘗試了上面列出的所有方法並在互聯網上進行了調查。我們還決定卸載並重新安裝 vs code。但是沒有一個成功。
有人可以幫忙嗎?
我們嘗試了以下方法:
- pip installed numpy
- pip3 installed numpy
- pip卸載了numpy,然後pip安裝了它。
- 我們在vs代碼環境設置中檢查了python解釋器的路徑。
- 我們嘗試添加
@{workspaceRoot}
到啟動文件中的環境屬性。 - 我們嘗試重新啟動 vs code。
- 我們嘗試關閉計算機,然後再次打開它。
- 我們嘗試重新安裝 vs code。
但以上都沒有奏效。
我們也試過在powershell中導入numpy
,效果很好。
PS C:\Users\XXXXXXXXXXXXXXXXXXX> & 'C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe' 'c:\Users\FARN\.vscode\extensions\ms‑python.python‑2019.8.30787\pythonFiles\ptvsd_launcher.py' '‑‑default' '‑‑client' '‑‑host' 'localhost' '‑‑port' '50166' 'C:\Users\XXXXXXXXXXXXXXXXXXX/src/CnTaaD.py' '‑L'
python 3.x
1.here: C:\Users\XXXXXXXXXXXXXXXXXXXXXXX
T0, starting testing task: <CnUserManager.CnUserManagerClass object at 0x000001BAC67866A0>
CnServerConnector: rootDir = C:\Users\XXXXXXXXXXXXXXXXXXXXXXXX
FM cmdLineJsonFilePath: C:\Users\XXXXXXXXXXXXX\YYYYYYYY.json
try opening file in python 3 in utf8!
try loading json file!
success in loading json file: <_io.TextIOWrapper name='C:\\Users\\XXXXXXXXXXXXXXXXXX\\YYYYYYY.json' mode='r' encoding='utf8'>
After loading the recorded projects json!
icoPath: C:\Users\XXXXXXXXXXXXXXXXX\src\pictures\animation\setDefault\logocute64.ico
‑‑‑[ testerFrankWang02 userRecord already exists!]‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
Traceback (most recent call last):
File "c:\Users\FARN\.vscode\extensions\ms‑python.python‑2019.8.30787\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Users\FARN\.vscode\extensions\ms‑python.python‑2019.8.30787\pythonFiles\lib\python\ptvsd\__main__.py", line 432, in main
run()
File "c:\Users\FARN\.vscode\extensions\ms‑python.python‑2019.8.30787\pythonFiles\lib\python\ptvsd\__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\FARN\Documents\LINUX\realtime\PRGM\TaaDPlaygroundClient/src/CnTaaD.py", line 35, in <module>
CnUserManager.CnUserManagerClass(sys.argv)
File "C:\Users\FARN\Documents\LINUX\realtime\PRGM\TaaDPlaygroundClient/src\CnUserManager.py", line 1334, in __init__
import CnProjectManager
File "C:\Users\FARN\Documents\LINUX\realtime\PRGM\TaaDPlaygroundClient/src\CnProjectManager.py", line 30, in <module>
import numpy as np
ModuleNotFoundError: No module named 'numpy'
Press any key to continue . . .
參考解法
方法 1:
It looks like you're using an installation of Python from Visual Studio instead of your own installed copy. Is that on purpose? And is that the installation of Python you installed numpy into?
The easiest way to see if numpy was installed where you expected is to run & 'C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe' '‑m' 'pip' 'list'
(that quoting might be wrong, but basically you want ‑m pip list
passed to the Python interpreter). That will list what pip has installed.
The best solution to this is to use a virtual environment and install into that.
(by Farn Wang、Brett Cannon)