Search code examples
pythonpycharmideclrpython.net

Python getting IDE linter to see from System imports within pythonnet's clr


I am using the pythonnet package in MSMQ communications. In my IDE (PyCharm CE), I have the following sample code:

#!/usr/bin/env python3


import pythoncom

import clr



clr.AddReference("System")

clr.AddReference("System.Messaging")

from System import TimeSpan

from System.Messaging import MessageQueue


Aside: this code works fine when I actually run it using Python 3.6.

And below is a screenshot of what it looks like inside PyCharm.

What I Am Seeing

  • For System it says: Unresolved reference 'System'

Other than using noqa comments, how can I get my PyCharm linting to do the following:

  • Not complain about from System import XYZ?

Versioning Info

  • IDE: PyCharm Community Edition 2019.2
  • pythonnet 2.3.0, installed in a virtual environment

Solution

  • This is my complete answer, according to our exchanges.

    Issue #1

    No module named clr

    After some checks, it seems there was a kind of mix between the operating system environment, and your project's virtual environment.

    Solution: delete and re-create properly virtual environment fixed it


    Issue #2

    Unresolved reference 'System'

    In this case where corresponding modules are loaded dynamically, your best solution is to disable PyCharm's Inspector on these specific lines; thus you won't loose anything else in your IDE.

    You can see How to disable inspection.

    In my sandbox, I just had to:

    • go on corresponding source code lines with warning (one after the other)
    • use the More actions context menu on the error (System in this case)
    • use the Ignore unresolved reference 'pythonnet_tests.System' sub-menu
    • use finally the Suppress for statement

    Each time the corresponding source code line, will be preceded by a comment line:

    # noinspection PyUnresolvedReferences

    And that's it!