[SOLVED] Python : how to correctly setup VSCode to use Openhab Helper Libraries

Tags: #<Tag:0x00007fc20852afe0>
  • Platform information:
    • Hardware: Dell Latitude
    • OS: Ubuntu 19.04
    • Java Runtime Environment: Oracle JVM v8
    • openHAB version: Snapshot
  • Issue of the topic:
    I’ve setup the automation environment (folders, jython jar…) to use it. It works great, really good work.
    Now I’m starting to move slowly my rules to this but I can not understand what I need to setup in order to have autocompletion working and avoid “unresolved import” errors in VSCode :
3 Likes

These are pylint errors only, your scripts should work fine. Can disable them for a while file, a block, or a single line. I don’t remember the exact way but look in the pylint docs.

You won’t need to do it for anything in automation/lib/python if you add that to your Python path in your vscode workspace. I’m on my phone so I can’t look at mine right now to give you a more detailed example, but again, look in the vscode docs for Python paths.

The Helper Library imports are not recognized because they are not in a path in syspath. The rest are Java or openHAB packages that are not available to the Python context that pylint is running in.

Thanks for your answer.
I confirm that my scripts works well, it’s not my point. My point is “can I have VSCode configured in a way he does not identify false errors, and even more…points me to real errors :slight_smile: ?”
If so, how to do that ?

You won’t be able to have VSC figure it out automatically unfortunately. There are ways around it though.

First you can get pylint to see the Python libs by creating a file {openhab_conf}/.env and adding this:

PYTHONPATH="./automation/lib/python"

Second you can exclude the Helper Libraries from pylint, since they are not strictly part of your openHAB conf::

    "python.linting.ignorePatterns": [
        "**/automation/**/python/core/**/*.py",
        "**/automation/**/python/community/**/*.py"
    ]

And lastly, in any files you create, you can put pylint directives around the import blocks it won’t be able to resolve:

# pylint: disable=import-error
from org.openhab.core.library.items import SwitchItem
# pylint: enable=import-error

Further reading about pylint Message Control and VSCode Python Linting might help you get this just to your liking

Edit: update ignore patterns to recurse directories under core and community

Edit: I have made a PR to the Helper Libraries documentation that will add a page detailing these steps.

4 Likes

@glhopital I’ve discovered that pylint can be manipulated to not require any of the ignores!

Check it out here:

1 Like

Sorry to invade this thread…
I’m struggling with vscode complaining about unresolved import 'core.jsr223' and 'configuration' for a util.py file located in my personal libs dir to contain this code:

# pylint: disable=import-error, no-name-in-module
from core.jsr223 import scope
# pylint: disable=import-error, no-name-in-module
from configuration import admin_email
....

As the script itself to use this util.py seems to work I suspect that’s not a real error message but just from pylint, am I correct there ?
So why is that ? I think it should know where to retrieve core.* from, shouldn’t it ? And even if not, the first line should silence the linter for that, shouldn’t it ?
I’m still at the beginning of all of this so bear with me I guess it must be something simple and silly.

I’m currently trying to lean Python along the lines of writing/converting my rules into.
Now if the linter gives an error, in the first place I wonder if I once again have not understood some of the Python coding issues. That now is very confusing in a learning process.

It ain’t (just) for my own’s sake but I guess that’s how it’ll be too for many OH users to come. I’m already afraid of all the “why does it not work” posts to show just slightly wrong or even correct code.
That’s why I feel this thing to be very important.

Just to be sure: OHCONF is /etc/openhab2 on openHABian/Raspi, right ? It’s where my .vscode is with its workspace setting init files is located, too.
I have put the pylintrc from your PR there and renamed it, restarted VS, too, but the error message persists.

Check through this topic, Markus. I haven’t looked through what Michael submitted… after I had asked him not to submit a PR for this. I have something to commit and hope to get to it this weekend.

The PR I submitted updates the docs, you said you had one ready for the repo. My mistake if they are the same, I interpreted them as being separate.