I have some Python files in a directory.
my-project/
utils/
a.py
b.py
__init__.py
main.py
They all run from main.py
, but files inside the utils
folder all import from one another.
# a.py
from .b import *
function_from_b()
The problem is, VSCode seems to have no clue where I'm importing from.
Specifically all my local imports, just lead to a bunch of annoying reportUndefinedVariable
warnings in my problems window and distracting yellow underlines on virtually all my functions. VSCode thinks every function is undefined, even though my local import is there.
It's really frustrating though because my code works fine. I'm running everything from my main.py
file and it works, but I still get the undefined variable warning when importing files from anything inside the utils
folder. Even debugging works using the main.py
file. The only time it doesn't work, is when I run individual files directly from the utils folder— like running current file debug or just running from the terminal (for example running $ python a.py
from terminal inside utils
). Then I get the following error.
ImportError: attempted relative import with no known parent package
This seems like a VSCode issue, so if possible I'd like to keep my code unchanged. My issue is not the code, but rather the countless annoying warnings from VSCode about a problem that doesn't exist. The example files I provided above are just a general outline of my problem, so if extra context about the specifics of my project are needed, I'm happy to provide them.
If your sample code is in a.py
, there will be no yellow squiggly line warning for imports in the same folder. This also depends on the folder workspace you have open.
Adding the following two settings to your import folder path will resolve the yellow squiggly line warning.
"python.analysis.extraPaths": [
"path/here"
],
"python.autoComplete.extraPaths": [
"path/here"
]