0

I have made a Python library whose tree directory structure looks like

lorentz_enrichment
├── __init__.py
│   └── lorentz_enricher.cpython-312.pyc
├── configs
│   ├── __init__.py
│   │   ├── logger.cpython-312.pyc
│   │   ├── s3.cpython-312.pyc
│   │   ├── settings.cpython-312.pyc
│   │   └── tqdm_handler.cpython-312.pyc
│   ├── logger.py
│   ├── s3.py
│   ├── settings.py
│   └── tqdm_handler.py
├── db
│   ├── __init__.py
...
├── lorentz_enricher.py
├── main.py
...
├── py.typed
...

32 directories, 157 files

This is what my __init__.py contains:

import logging
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from .configs.settings import LorentzEnricherSettingsProvider
    from .db.mongo.claim_rules_repository import ClaimRulesRepository
    from .db.mongo.lorentz_claims_repository import LorentzClaimsRepository
    from .db.mongo.mongo_base_repository import MongoBaseRepository
    from .db.mongo.queue_messages_repository import QueueMessagesRepository
    ...

from .configs.settings import LorentzEnricherSettingsProvider
from .db.mongo.claim_rules_repository import ClaimRulesRepository
from .db.mongo.lorentz_claims_repository import LorentzClaimsRepository
from .db.mongo.mongo_base_repository import MongoBaseRepository
from .db.mongo.queue_messages_repository import QueueMessagesRepository
...

logging.getLogger(__name__).addHandler(logging.NullHandler())

__all__ = [
    'LorentzEnricher',
    'LorentzEnricherSettingsProvider',
    ...
]


def __dir__() -> list[str]:
    return list(__all__)

Everything under TYPE_CHECKING is imported outside too. And this is my pyproject.toml:

[tool.poetry]
name = "lorentz-enrichment"
version = "0.27.0-b5"
description = "Tools to enrich Lorentz claims with meteorological data"
authors = ["asant <***@***.eu>"]
readme = "README.md"
packages = [{ include = "lorentz_enrichment" }]

[tool.poetry.dependencies]
python = "^3.12"
...


[tool.poetry.group.dev.dependencies]
black = "24.10.0"
ruff = "0.11.11"
...

[tool.mypy]
exclude = [".venv"]
ignore_missing_imports = true
enable_error_code = "possibly-undefined"
disable_error_code = ["import-untyped", "override"]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

In my microservice the imports like from lorentz_enrichment import LorentzEnricher works correctly, however when in my code I type some classes like LorentzEnricher() without importing it first, VS Code won't suggest the import like it does for pydantic BaseModel for example.

What am I missing here?

1 Answer 1

0

You'll need to add your package's path to VS Code's "python.autoComplete.extraPaths" setting. And for good measure you may also want to add it to "python.analysis.extraPaths".

Open your settings.json file and add the following:

"python.autoComplete.extraPaths": [
    "<path to your module>"
],
"python.analysis.extraPaths": [
    "<path to your module>"
]

Mind your commas and make sure your JSON is properly formatted, and you should be good to go!

Sign up to request clarification or add additional context in comments.

4 Comments

what the path should be? Consider that I am publishing my library on an internal nexus. Why do I need to perform extra steps whereas libraries like pydantic, s3fs, pandas require no additional steps to work properly with autosuggestions?
The path should be the root of wherever your module is actually being installed (i.e. <install path>/lorentz_enrichment/. I suspect that it's ending up somewhere VS Code "can't see" out of the box. FWIW, if the install ends up somewhere typical, e.g. site-packages, it should pick up automatically - but sometimes VS Code is finicky.
Yes, it is installed in the site-packages of the venv folder, like any other library, what is not working for me?
I'm not sure specifically why it isn't working for you (nothing stands out to me in the information you've provided), but I know I've had to do this for my own packages in the past in order to get VS Code to cooperate. Apologies, I know it's not the most satisfying answer. (Also, StackOverflow won't let me reply to your second comment, so I'm being forced to reply here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.