I typically have a folder open in VS code which contain my python project with a .venv folder.
I placed my current project in such a way that I have to go back to the parent folder to find my other module.
workspace_folder
-- module_to_import
--- __init__.py
--- submodule1
---- __init__.py
-- project_currently_working_on
When I try to do a relative import as such
import ..module_to_import
I am getting a ImportError: attempted relative import with no known parent package
I open the folder in VS code and configured the launch.json as
// 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: Module - name module",
"type": "python",
"request": "launch",
// "program": "${file}",
"cwd": "${workspaceRoot}",
"env": {"PYTHONPATH": "${workspaceRoot}"},
"console": "integratedTerminal"
}
]
}
Tried this with settings.json
{
"editor.minimap.enabled": false,
"editor.renderWhitespace": "all",
"breadcrumbs.enabled": false,
"[python]": {
"editor.formatOnType": true
},
"python.terminal.launchArgs": [
],
"jupyter.interactiveWindow.creationMode": "perFile",
"jupyter.askForKernelRestart": false,
"workbench.startupEditor": "none",
"C_Cpp.default.compilerPath": "/usr/bin/g++",
"notebook.confirmDeleteRunningCell": false,
"jupyter.askForLargeDataFrames": false,
"[cpp]": {
"editor.wordBasedSuggestions": "off",
"editor.suggest.insertMode": "replace",
"editor.semanticHighlighting.enabled": true
},
"python.analysis.extraPaths": ["../module_to_import"], // <----- added
"explorer.confirmDelete": false,
"jupyter.interactiveWindow.textEditor.executeSelection": true,
}
To no avail.
If I put this instead "python.analysis.extraPaths": [".."], I can see in the IDE the module, and no warning appear. However, I get a ModuleNotFoundError when running the script:
import module_to_import // <--- IDE automatically proposes me this
from module_to_import import submodule1 // <--- Can even see it pop-up in the IDE
Then, I asked chatGPT which proposed to create a workspace in such a way that:
workspace_folder
-- module_to_import
-- project_currently_working_on
With the workspace configuration having:
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.envFile": "${workspaceFolder}/.env",
"python.analysis.extraPaths": ["${workspaceFolder}"]
},
}
Again, same ModuleNotFoundError.
When I try relative import, I can see all of the other projects/modules when typing ... However, I get again a ImportError: attempted relative import with no known parent package when running the script with:
import ..module_to_import // <--- IDE proposes a list of module after typing .. with the option of the module_to_import visible
Any idea how I could get a configuration that would allow me to easily add local module to my current project?