Visual Studio 代码 - ModuleNotFoundError,终端中的 sys.path 与 VSCode 不同?
Visual Studio Code - ModuleNotFoundError, different sys.path in terminal and VSCode?
我在 Visual Studio 代码中尝试 运行ning 代码,但我一直收到 ModuleNotFoundError。当我 运行 我的终端中的代码或在 VS 中的调试模式下使用激活的 conda 环境时,它工作正常。
系统:MacM1 12.3
Conda 环境,在 Visual Studio 代码中选择。
我添加了这个
import os
print('cwd is %s' %(os.getcwd()))
import sys
print('executable is %s' %(sys.executable))
print('path is %s' %(sys.path))
and 运行ning 在终端中给出:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
运行在 VS 中通过 运行Python File(右上角按钮)给出:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/lib/python39.zip', '/Users/USERNAME/miniforge3/lib/python3.9', '/Users/USERNAME/miniforge3/lib/python3.9/lib-dynload', '/Users/USERNAME/miniforge3/lib/python3.9/site-packages']
运行在 VS 中通过 DebugPythonFile(右上角按钮)给出:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
我很困惑 - 如何在 VS Code 中得到这个 运行ning?
---- 2022 年 3 月 23 日更新:
我有三个选择
当我添加
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
}
至
settings.json
(参见来自@Kyouya Sato 的 [https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm] 2)
和运行 运行代码有效,我也得到
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
不变
settings.json
它也无法使用 运行 代码。
运行 Python 文件 根本不工作。
你是说,什么时候用code runner?如果您的回答是肯定的,请检查:https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm
解决方法:
- Set "python.terminal.activateEnvironment" to false in the settings.json.
- Downgrade to the previous version of the extension which works fine(avoid
conda run
).
- 尝试以下具有潜在修复的 VSIX:https://github.com/microsoft/vscode-python/suites/5578467772/artifacts/180581906,使用扩展:从 VSIX 安装命令安装 VSIX。
原因:
Conda 有一些问题:
我在 Visual Studio 代码中尝试 运行ning 代码,但我一直收到 ModuleNotFoundError。当我 运行 我的终端中的代码或在 VS 中的调试模式下使用激活的 conda 环境时,它工作正常。
系统:MacM1 12.3
Conda 环境,在 Visual Studio 代码中选择。
我添加了这个
import os
print('cwd is %s' %(os.getcwd()))
import sys
print('executable is %s' %(sys.executable))
print('path is %s' %(sys.path))
and 运行ning 在终端中给出:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
运行在 VS 中通过 运行Python File(右上角按钮)给出:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/lib/python39.zip', '/Users/USERNAME/miniforge3/lib/python3.9', '/Users/USERNAME/miniforge3/lib/python3.9/lib-dynload', '/Users/USERNAME/miniforge3/lib/python3.9/site-packages']
运行在 VS 中通过 DebugPythonFile(右上角按钮)给出:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
我很困惑 - 如何在 VS Code 中得到这个 运行ning?
---- 2022 年 3 月 23 日更新:
我有三个选择
当我添加
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
}
至
settings.json
(参见来自@Kyouya Sato 的 [https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm] 2)
和运行 运行代码有效,我也得到
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
不变
settings.json
它也无法使用 运行 代码。
运行 Python 文件 根本不工作。
你是说,什么时候用code runner?如果您的回答是肯定的,请检查:https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm
解决方法:
- Set "python.terminal.activateEnvironment" to false in the settings.json.
- Downgrade to the previous version of the extension which works fine(avoid
conda run
). - 尝试以下具有潜在修复的 VSIX:https://github.com/microsoft/vscode-python/suites/5578467772/artifacts/180581906,使用扩展:从 VSIX 安装命令安装 VSIX。
原因:
Conda 有一些问题: