2

I have been trying and failing to get autocomplete to properly handle conditional imports (ie: imports os and uses if/elif/else check for os dependent library import) when importing a library (I believe the engine that handles it is IntelliSense but not entirely sure).

I first ran across the problem when importing serial and can call the correct module manually but not using the conditional. Although this issue has been replicated in other libraries that contain conditional imports.

I am using the installed Python 3.7 from Anaconda and the Microsoft Python Extension in VS Code. My system paths point to the Anaconda folder and libraries and imports arn't having any issue finding requested files.

The results I am getting for auto complete are dependent on whether I have Jedi Enabled as the IntelliSense engine or Microsoft Python Analysis Engine.

#Microsoft Python Analysis Engine
import serial
serial.Serial()
    #Dose reference module but incorrect one: serialjava.py for the Serial() function

#Setting > Python > Jedi Enabled (as IntelliSense engine)
import serial
serial.Serial() #not found
serial.serialwin32.Serial() #dose find function if use path to os dependent module

If I write the code in Spyder or Jupiter the import paths are being parsed correctly. I checked to make sure it wasn't just my computer and I am having the same issue on the Lab's macs.

Serial librarie's __init__.py (just the conditional check section):

if sys.platform == 'cli':
    from serial.serialcli import Serial
else:
    import os
    # chose an implementation, depending on os
    if os.name == 'nt':  # sys.platform == 'win32':
        from serial.serialwin32 import Serial
    elif os.name == 'posix':
        from serial.serialposix import Serial, PosixPollSerial, VTIMESerial  # noqa
    elif os.name == 'java':
        from serial.serialjava import Serial
    else:
        raise ImportError("Sorry: no implementation for your platform ('{}') available".format(os.name))

Environment: VS Code w/ Microsoft Python extension
OS: Windows 10 & macOS 10
Backend: Python 3.7 from Anaconda install
Imported Library: pyserial

Shakrii
  • 21
  • 2

0 Answers0