I'm trying to debug my python app using VSCode. But I cannot configure my environment right. Attempting to import a class from one folder in my source path to another, gives me this message:
Traceback (most recent call last):
File "/Users/mb/Library/source/sandbox/lib/lib.py", line 1, in <module>
from app.main import MyClass
ModuleNotFoundError: No module named 'app'
I created a simple app to exhibit the problem. My source path looks like this:
sandbox
+-- .vscode
--- launch.json
+-- app
--- __init__.py
--- main.py
+-- lib
-- lib.py
# main.py
class MyClass:
def __init__(self):
print('Creating object...')
def print_message(self, message):
print(message)
# lib.py
from app.main import MyClass
myclass = MyClass()
myclass.print_message('hello, world!')
Trying to run lib.py
using the default configuration to run current file I get the error message above.
Additionally, I created a .vscode/launch.json
to set the working directory, but to no avail. Here is my launch.json
:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "main"
}
]
}
I'm sure, I'm missing a simple setup, but I can't put my finger on it.
Note
- The interpreter used is: Python 3.7.5 64-bit
- The above code works fine in PyCharm.
- After some reading on VSCode documentation, I added
"env": {"PYTHONPATH": "${workspaceFolder}"}
tolaunch.json
, but that didn't do any good. - I also created a VSCode task to echo the
${workspaceFolder}
variable, and the output was stripped of all the\
from the path, as if they were not escaped. Here is the task in.vscode/tasks.json
:
{
"version": "2.0.0",
"tasks": [
{
"label": "workspaceFolder",
"type": "shell",
"command": "echo ${workspaceFolder}"
}
]
}
And here is the output that got from it:
> Executing task: echo C:\Users\mb\Library\source\sandbox <
C:UsersmbLibrarysourcesandbox
Terminal will be reused by tasks, press any key to close it.