Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete for Automation objects in VS Code and Python

I have the Python Extensions for Windows installed. Within the PythonWin IDE I can get autocomplete on Automation objects (specifically, objects created with win32com.client.Dispatch):

PythonWin IDE with autocomplete

How can I get the same autocomplete in VS Code?

I am using the Microsoft Python extension.

The Python Windows Extensions has a tool called COM Makepy, which apparently generates Python representations of Automation objects, but I can't figure out how to use it.

Update

Apparently, the Microsoft Python extension uses Jedi for autocompletion.

I've filed an issue on the extension project on Github.

Note that in general I have Intellisense in Python; it's only the Intellisense on Automation objects that I am missing.

like image 220
Zev Spitz Avatar asked Dec 20 '17 06:12

Zev Spitz


2 Answers

Review

I have confirmed your problem in VSCode, although it may be possible IntelliSense works fine. Note Ctrl+Space invokes suggestions for a pre-defined variable:

enter image description here

However, there does not appear to be public attributes available for win32com.client by default. This may be why IntelliSense does not appear to work.

Test

Having installed win32com for Python 3.6, I have confirmed the following code in Jupyter notebook, IPython console and the native Python REPL:

import win32com.client


app = win32com.client.Dispatch("Word.Application")
len(dir(app))
# 55
[x for x in dir(app) if not x.startswith("_")]
# []

This issue of hidden attributes is not a new. Please confirm this test in another environment/IDE. It may be your environment or particular version of PythonWin pre-loads certain variables in the global namespace.

Verify the following:

  • The Python extension is installed
  • A Python interpreter is selected
  • A Python file is selected; this starts up the Python server

References

  • Post by the extension's creator for troubleshooting autocompletion issues.
  • Thread on how autocompletion works via PyScriptor
like image 148
pylang Avatar answered Oct 15 '22 08:10

pylang


I don't think that the example you show with PythonWin is easily reproducible in VS Code. The quick start guide of win32com itself (cited below) says, its only possible with a COM browser or the documentation of the product (Word in this case). The latter one is unlikely, so PythonWin is probably using a COM browser to find the properties. And because PythonWin and win32com come in the same package, its not that unlikely that PythonWin has a COM browser built in.

How do I know which methods and properties are available?

Good question. This is hard! You need to use the documentation with the products, or possibly a COM browser. Note however that COM browsers typically rely on these objects registering themselves in certain ways, and many objects to not do this. You are just expected to know.

If you wanted the same functionality from the VS Code plugin a COM browser would have to be implemented into Jedi (IntelliSense of the VS Code plugin).

Edit: I found this suggestion, on how a auto-complete, that can find these hidden attributes, could be done:

These wrappers do various things that make static analysis difficult, unless we were to special case them. The general solution for such cases is to run to a breakpoint and work in the live runtime state. The auto-completer should then include the complete list of symbols because it inspects the runtime.

The conversation is from an mailing list of the python IDE wingwide. Here you can see, that they implemented the above mentioned approach:

Auto-Completer Icons

like image 3
Markus Schwer Avatar answered Oct 15 '22 10:10

Markus Schwer