Skip to content Skip to sidebar Skip to footer

Proper Relative Imports: "unable To Import Module"

I have a project structured like this: . └── myapp    ├── app.py    ├── models    │   ├── hello.py    │   └── world.py    └�

Solution 1:

The error you are receiving is one that's reported by a python linter named pylint. So the problem isn't really specific to the vscode extension.

There are two solutions:

  1. Please try adding an .env file in your project directory with the vape PYTHONPATH=./myapp, this will tell pylint where to find your modules

  2. Or just open the folder myapp in vscode directly instead of opening the parent directory in vscode.

Solution 2:

The error is coming from pylint. You need to add this line into settings.json file (VS Code):

"python.linting.pylintArgs":["--init-hook","import sys; sys.path.append('<absolute path to myapp directory>')"],

Solution 3:

Since hello.py and world.py are in the same folder (aka package), you should import the Hello class in world.py as follow:

from .helloimportHello

As described in this thread: What does a . in an import statement in Python mean?

The . is here to indicate the import from the current package.

Solution 4:

In your your .vscode/settings.json (in the root directory of your workspace) you need these two lines:

one to use the pylint in your virtual environment (if you have one) so that pylint is aware of it. You'll want to adjust the below if your pylint or virtual environment is located in a different place.

"python.linting.pylintPath":"${workspaceFolder}/api/venv/bin/pylint"

and one, as Shtefan mentions above, to let pylint know where the python part of your project is:

"python.linting.pylintArgs":["--init-hook","import sys; sys.path.append('${workspaceFolder}/api')"]

This additional line may be helpful if you don't already have vscode setup with your virtual environment, once again you may have to modify if your virtual environment path does not match the below.

"python.pythonPath":"${workspaceFolder}/api/venv/bin/python",

Post a Comment for "Proper Relative Imports: "unable To Import Module""