visual-studio-codepython-importodoo-14pylance

Import "odoo.addons.base_rest.controllers" could not be resolved. Pylance(reportMissingImports)


When i develop rest API for Addons odoo, set import and get problem

Import "odoo.addons.base_rest.controllers" could not be resolved. Pylance(reportMissingImports)

this is a part of my code

from odoo.addons.base_rest.controllers import main 

class PrivateApiController(main.RestController):
   _root_path = '/api/private/helpdesk/'
   _collection_name = "private.helpdesk.service"

and all of code using from odoo.addons.[module_name] import main get problem Pylance(reportMissingImports) however when it runs, it doesn't matter

i use .env and my file structure is :


├── my_addons
│   ├── api_ticket [my addons that i develop and get problem Pylance Missing Report ]
|   |   ├── controllers
|   |   │   ├── controllers.py
|   |   │   ├── __init__.py
|   |   │   ├── main.py
|   |   │   └── __pycache__
|   |   ├── datamodels
|   |   │   ├── __init__.py
|   |   │   ├── __pycache__
|   |   │   └── ticket_datamodel.py
|   |   ├── demo
|   |   │   └── demo.xml
|   |   ├── __init__.py
|   |   ├── __manifest__.py
|   |   ├── models
|   |   │   ├── __init__.py
|   |   │   ├── models.py
|   |   │   └── __pycache__
|   |   ├── __pycache__
|   |   │   └── __init__.cpython-38.pyc
|   |   ├── security
|   |   │   └── ir.model.access.csv
|   |   ├── services
|   |   │   ├── __init__.py
|   |   │   ├── __pycache__
|   |   │   └── ticket_service.py
|   |   └── views
|   |       ├── templates.xml
|   |       └── views.xml
│   ├── auth_api_key
|   ├── base_rest [one of the imported modules and has been installed into the main odoo]
...
│   ├── helpdesk_type
│   my_odoo.conf
├── odoo-14.0 -> ~/P/OdooProject/odoo_base/odoo-14.0 [main odoo]
|   ├── LICENSE
|   ├── MANIFEST.in
|   ├── odoo
|   │   ├── addons
|   │   ├── api.py
...
|   └── setup.py
├── requirements.txt
├── runserver.sh
└── venv14 -> ~/P/OdooProject/odoo_base/venv14

then my setting.json

{
    "python.analysis.extraPaths": [
        "./odoo-14.0", // main odoo
        "./my_addons", // include develop addons and base_rest, etc from oca 
    ],
    "python.defaultInterpreterPath": "venv14/bin/python3"
    
}

i tried google the my problem and found 'Import "Path.to.own.script" could not be resolved Pylance (reportMissingImports)' in VS Code using Python 3.x on Ubuntu 20.04 LTS and one of solution The alternative is to add # type: ignore at the end of the import code. as temporary solution

my question are :

  1. Is there a main way to solve it ?
  2. if one of the main ways is in the settings.json section and add python.analysis.extraPaths. is my writing correct?

Solution

  • Odoo loads addons dynamically as namespace packages and combines them into single namespace. Pylance on the other hand is a static analyzer. It doesn't execute any python code and therefore it is technically impossible to resolve those names. Unfortunately, I don't think there is a workaround this.