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
):
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.
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:
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:
References
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With