Juicy
Juicy

Reputation: 12510

VSCode/Pylint can't find module in same directory

I have the following directory structure:

.
├── Pipfile
├── Pipfile.lock
└── src
    ├── config.py
    ├── __init__.py
    └── main.py

The content of config.py is:

FOO = 'bar'

The content of main.py is:

import config

print(config.FOO)

The __init__.py is empty.

With the above setup, VSCode\Pylint is complaining that config has no FOO member.

If I delete the __init__.py the warning disappears.

Why is this happening and what is the correct way to fix this problem?

Upvotes: 4

Views: 3480

Answers (1)

Brett Cannon
Brett Cannon

Reputation: 15990

It's happening because import config is an absolute import which means Python is trying to find config as a top-level package or module. But when you have an __init__.py file you make your src/ directory a package, so that makes Python treat src/config.py not as config but as src.config. When you delete the __init__.py, the Python extension for VS Code assumes you want your src/ directory to just be treated as a folder where you keep code and thus does extra work so Python runs from with src/ and not the top of your workspace.

Upvotes: 3

Related Questions