I am using Visual Studio Code and would like to use the integrated terminal to run or debug my code.
I have a python file in a scripts subdirectory. Everything runs correctly if I move it up one level to the project root directory. However, in the scripts subdirectory I cannot import the Python modules I have created.
If I add this to the top of my file it will work correctly:
import sys
import os
sys.path.insert(0, os.getcwd())
print(sys.path)
If I don't do that insert statement I get a ModuleNotFoundError
exception.
This is my debug configuration:
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
What do I do to make sure the Python path always includes the project root directory, regardless of where the script file is located?
I got it to work by setting an environment variable in the launch configuration.
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {"PYTHONPATH": "${workspaceFolder}"}
}
The last line did the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.