Visual Studio Code – ModuleNotFoundError, different sys.path in terminal and VSCode?
Question:
I try running code in Visual Studio code but I keep getting a ModuleNotFoundError. When I run the code in my terminal or in debug mode in VS with the activated conda environment it works fine.
System: Mac M1 12.3
Conda Environment, selected in Visual Studio Code.
I have added this
import os
print('cwd is %s' %(os.getcwd()))
import sys
print('executable is %s' %(sys.executable))
print('path is %s' %(sys.path))
and running in terminal gives:
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']
running in VS via Run Python File (upper right corner button) gives:
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']
running in VS via Debug Python File (upper right corner button) gives:
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']
I am confused – How can I get this running in VS Code?
—- Update March 23 2022:
When I add
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
}
to
settings.json
(see [https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm] 2 from @Kyouya Sato)
and run Run Code it works and I also get
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']
without changing
settings.json
it does also not work using Run Code.
Run Python File is not working at all.
Answers:
you mean, when use code runner? if your answer is yes, check this: https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm
Workaround:
- Set "python.terminal.activateEnvironment" to false in the settings.json.
- Downgrade to the previous version of the extension which works fine(avoid
conda run
).
- Try out the following VSIX which has the potential fix: https://github.com/microsoft/vscode-python/suites/5578467772/artifacts/180581906, Use Extension: Install from VSIX command to install the VSIX.
Reason:
Conda has some problems:
In order to debug main.py VSCode needs to know explicit library paths. This can be done by setting the environment (‘env’) variable in launch.json. First step is create a ‘launch.json’ inside the .vscode folder.
/
├── .vscode/
│ └── launch.json
├── mySubdir/
│ └── myLib.py
└── main.py
If main.py wants to import myLib.py as module, VSCode can only do this if mySubDir is part of the Python path.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}",
"env": {"PYTHONPATH": "${workspaceFolder}/mySubdir${pathSeparator}${env:PYTHONPATH}"},
}
]
}
{workspaceFolder} and {pathSeparator} are predefined variables which will be substituted by VSCode.
This thread gives a more extensive explanation: How to correctly set PYTHONPATH for Visual Studio Code
I try running code in Visual Studio code but I keep getting a ModuleNotFoundError. When I run the code in my terminal or in debug mode in VS with the activated conda environment it works fine.
System: Mac M1 12.3
Conda Environment, selected in Visual Studio Code.
I have added this
import os
print('cwd is %s' %(os.getcwd()))
import sys
print('executable is %s' %(sys.executable))
print('path is %s' %(sys.path))
and running in terminal gives:
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']
running in VS via Run Python File (upper right corner button) gives:
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']
running in VS via Debug Python File (upper right corner button) gives:
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']
I am confused – How can I get this running in VS Code?
—- Update March 23 2022:
When I add
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
}
to
settings.json
(see [https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm] 2 from @Kyouya Sato)
and run Run Code it works and I also get
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']
without changing
settings.json
it does also not work using Run Code.
Run Python File is not working at all.
you mean, when use code runner? if your answer is yes, check this: https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm
Workaround:
- Set "python.terminal.activateEnvironment" to false in the settings.json.
- Downgrade to the previous version of the extension which works fine(avoid
conda run
). - Try out the following VSIX which has the potential fix: https://github.com/microsoft/vscode-python/suites/5578467772/artifacts/180581906, Use Extension: Install from VSIX command to install the VSIX.
Reason:
Conda has some problems:
In order to debug main.py VSCode needs to know explicit library paths. This can be done by setting the environment (‘env’) variable in launch.json. First step is create a ‘launch.json’ inside the .vscode folder.
/
├── .vscode/
│ └── launch.json
├── mySubdir/
│ └── myLib.py
└── main.py
If main.py wants to import myLib.py as module, VSCode can only do this if mySubDir is part of the Python path.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}",
"env": {"PYTHONPATH": "${workspaceFolder}/mySubdir${pathSeparator}${env:PYTHONPATH}"},
}
]
}
{workspaceFolder} and {pathSeparator} are predefined variables which will be substituted by VSCode.
This thread gives a more extensive explanation: How to correctly set PYTHONPATH for Visual Studio Code