10

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.

Zev Spitz
  • 10,414
  • 4
  • 49
  • 114
  • Looking at different things Windows Python Extensions, Visual Studio Code. This seems not to be possible as of now. Probably when they starting supporting typeshed (*.pyi) extensions then it would be easier to map these things – Tarun Lalwani Dec 22 '17 at 13:55

3 Answers3

4

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:

References

pylang
  • 28,402
  • 9
  • 97
  • 94
  • Python Extensions for Windows comes with the `makepy` tool, which generates static definitions in `c:\Program Files\Python36\Lib\site-packages\win32com\gen_py`. – Zev Spitz Dec 29 '17 at 12:31
3

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

Community
  • 1
  • 1
Markus Schwer
  • 66
  • 1
  • 5
  • I'm sorry, I missed that. I've tried out your example, and it works fine for me. Have you only tried "app.Visible" or have you also tried the code before that ("app = win32com.client.Dispatch...")? Because for me, everything except the "app.Visible" part works. – Markus Schwer Dec 27 '17 at 09:14
  • Does the IntelliSense for "app = win32com.client.Dispach..." work? – Markus Schwer Dec 27 '17 at 10:53
  • I tried the same in PyCharm and it couldn't do the IntelliSense on `app` either. I think (correct me if I'm wrong) the IntelliSense you show in your example only works because you have already written the possible parameters, and IntelliSense has remembered. For more information see pylang's answers. – Markus Schwer Dec 27 '17 at 11:45
  • I don't think so. I opened the editor, and typed the code. It's possible that the PyWin editor (which comes with the Python Extensions for Windows) has special handling of the generated files. – Zev Spitz Dec 27 '17 at 12:05
  • I've rewritten the answer now. – Markus Schwer Dec 28 '17 at 01:32
  • Python Extensions for Windows comes with the `makepy` tool, which generates static definitions in `c:\Program Files\Python36\Lib\site-packages\win32com\gen_py`. – Zev Spitz Dec 29 '17 at 12:30
0

I think your problem is related to defining Python interpreter.

Choose proper Python interpreter by executing python interpreter command in VS Code command palette by pressing f1 or ctrl+shift+p key.

mcrunix
  • 835
  • 1
  • 7
  • 16