成功pip安装python-Levenshtein后无法导入,VS Code中Pylance提示导入未解决
It sounds like you've got the right packages installed in your virtual environment, but VS Code's Pylance isn't picking up the interpreter correctly. Let's walk through some fixes step by step:
1. Double-check VS Code is using your virtual environment's interpreter
This is the most common culprit when imports work in the terminal but not in the editor:
- Look at the bottom-left corner of your VS Code window—you'll see the current Python interpreter version/path. Click on it.
- A menu will pop up with all available interpreters. Select the one from your
.venvfolder (it should look something like./.venv/bin/pythonon macOS/Linux or.venv\Scripts\python.exeon Windows). - Alternatively, open the command palette with
Ctrl+Shift+P(Windows/Linux) orCmd+Shift+P(Mac), type Python: Select Interpreter, and pick the correct virtual environment option.
2. Restart the Pylance Language Server
Sometimes the language server gets stuck and doesn't refresh after you install packages:
- Open the command palette again, type Python: Restart Language Server, and run the command. Wait 10-15 seconds for it to reload, then check if the import error is gone.
3. Resolve conflicting Levenshtein packages
I notice you have both Levenshtein 0.18.1 and python-Levenshtein 0.12.2 installed in your env. These are two separate packages (the newer Levenshtein is maintained by the rapidfuzz team, while python-Levenshtein is the older, original version). This conflict might be confusing Pylance.
- Open your activated virtual environment terminal and run:
pip uninstall python-Levenshtein - Keep only the newer
Levenshtein 0.18.1package, then try importing again withimport Levenshteinorfrom Levenshtein import distance.
4. Verify your VS Code settings aren't overriding the interpreter path
- Open your user/workspace settings by pressing
Ctrl+,(Windows/Linux) orCmd+,(Mac). - Search for python.pythonPath (note: this setting is deprecated in newer VS Code versions, but if you see it, make sure it points directly to your
.venv's Python executable). - For newer versions, check Python: Default Interpreter Path instead, and ensure it's set to your virtual environment's Python.
5. Clear Pylance and Python caches
If all else fails, cached data might be causing issues:
- Delete the
__pycache__folders inside your.venv/lib/pythonX.X/site-packages/Levenshteindirectory (replaceX.Xwith your Python version). - Restart VS Code entirely, then open your project again and test the import.
After trying these steps, your Levenshtein import should work as expected—since you can see the package in pip list and other packages are importing fine, it's almost certainly an interpreter or language server configuration issue.
内容的提问来源于stack exchange,提问作者Chris B.

