win10下vscode python導入失敗 (vscode python import failure in win10)


問題描述

win10下vscode python導入失敗 (vscode python import failure in win10)

我一直在使用 Visual Studio Code 開發一些 python 程序。我導入的模塊包括 numpycssutils。有時 VS CODE 要求我通常同意的升級。然後今天VS CODE突然找不到cssutils。我和我的學生檢查了所有的環境變量,路徑變量,並在互聯網上進行了調查,但沒有找到任何線索。有人說這是作者聲稱已修復但實際上沒有修復的 VS CODE 錯誤。有人建議在 launch.json 文件和 setting.json 中添加一些聲明。

我們嘗試了所有方法,但都沒有成功。我們也關閉了PC,重新運行了vscode。

最後,我們卸載了cssutils 並再次 pip 安裝它。然後問題就變成了 numpy 無法導入。然後我們嘗試了上面列出的所有方法並在互聯網上進行了調查。我們還決定卸載並重新安裝 vs code。但是沒有一個成功。

有人可以幫忙嗎?

我們嘗試了以下方法:

  1. pip installed numpy
  2. pip3 installed numpy
  3. pip卸載了numpy,然後pip安裝了它。
  4. 我們在vs代碼環境設置中檢查了python解釋器的路徑。
  5. 我們嘗試添加@{workspaceRoot} 到啟動文件中的環境屬性。
  6. 我們嘗試重新啟動 vs code。
  7. 我們嘗試關閉計算機,然後再次打開它。
  8. 我們嘗試重新安裝 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 WangBrett Cannon)

參考文件

  1. vscode python import failure in win10 (CC BY‑SA 2.5/3.0/4.0)

#Python #import #visual-studio-code #Windows






相關問題

如何從控制台中導入的文件中訪問變量的內容? (How do I access the contents of a variable from a file imported in a console?)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

為 KeyError 打印出奇怪的錯誤消息 (Strange error message printed out for KeyError)

django 1.9 中的 from django.views.generic.simple import direct_to_template 相當於什麼 (What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9)

查詢嵌入列表中的數組 (Querying for array in embedded list)

如何在 Python 中搜索子字符串是否在二進製文件中? (How to search if a substring is into a binary file in Python?)

為什麼要避免 while 循環? (Why avoid while loops?)

使用python的json模塊解析json請求 (Parse a json request using json module of python)

為什麼使用 py2app 模塊創建 mac 文件時出現錯誤? (Why i am getting Error when creating mac file using py2app module?)

當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)

如何繪製一條帶斜率和一個點的線?Python (How to plot a line with slope and one point given? Python)

Pickle 找不到我不使用的模塊? (Pickle can't find module that I am not using?)







留言討論