Autocomplete for Automation objects in VS Code and Python - 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):
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:
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

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:

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.

Related

What is difference between Python & Pylance VS Code extensions?

I just shifted from my old bud Sublime to VSCode. I really liked the way it works and the features it has. I'm a newbie python developer. I found two popular python extensions for VSCode: Python, and PyLance. My question is, What is the difference between Python and Pylance extension? I searched a lot but didn't find a good comparison.
As an editor, VSCode cannot recognize all languages and many functions cannot be implemented independently. Therefore, when we use Python code in VSCode, we need to install the 'Python' extension, which provides us with functions such as code completion, support for Jupyter notebooks, debugging Python code, etc. Therefore, the Python extension is one of the necessary dependencies for using Python in VSCode.
The extension 'Pylance' needs to be used in conjunction with the Python extension. It cannot be used independently in VSCode. It mainly provides outstanding Python language services (other Python language services such as "Microsoft", "Jedi", don't need to install specific extensions, they can be used as-is after installation). At the same time, it also provides functions such as docstrings. Therefore, the Pylance extension is not a necessary condition, but a recommended extension.
It is recommended that you install and use these two extensions. They are not opposite extensions, they are VSCode extensions that cooperate with each other (To be precise, the 'Pylance' extension relies on the 'Python' extension to use).
And for more related information, you could refer to the VS Code docs on: Using Python in VSCode and Python and Pylance.

Show true python source instead of pycharm stubs for an extention module

When navigating python code with third party libraries - opencv in this case - why does pycharm display its stubs instead of the actual source?
Consider when clicking on the following imshow() method
cv2.imshow("Faces found", image)
Then instead of navigating to the opencv sources - or at least to a decompiled equivalent- we see the following:
This compares poorly to the results on intellij for jvm languages such as java and scala - in which we have the option to Attach sources or at the least to show decompiled code. Are there any better options for python than this?
Python extensions are written in C/C++ (so there is no "python source" to speak of) which, unlike Java, can't be easily decompiled.
To debug them, you'll need to do that like any other binary module, using a native debugger (that would use C-level debug information if present in the module to link machine code to the sources). PyCharm doesn't have a native debugger, this feature is reserved for JetBrains' paid product, CLion.

Is it possible to embed Jedi in an application on a system where Python is not installed?

I'm working on an (Windows and Mac) application that uses Python as an embedded scripting language.
The application includes an internal text editor, implemented using Scintilla, and I'm using Jedi for autocompletion, which generally works great.
However, when attempting autocompletion on a computer that does not have a separate installation of Python, Jedi raises an error:
jedi.api.environment.InvalidPythonEnvironment:
Could not get version information for 'python':
FileNotFoundError(2, 'The system cannot find the file specified', None, 2, None)
Digging into the code, I can see that the underlying code that is throwing the FileNotFoundError is when Jedi attempts to run python using subprocess.Popen. Python is not installed on the computer, so this fails.
I can also reproduce the same issue on a computer that does have Python installed by editing my Path environment variable not to include the location of python.exe.
Ideally, we don't want users of our application to have to install Python just to get autocompletion working.
My questions:
Is it possible to get Jedi not to spawn subprocesses, and instead run its code inside the same instance of Python within which it itself is running? I couldn't find anything about this in the documentation or the source code that deals with Environments, and extrapolating from the discussion here I suspect the answer might be no.
Is it possible somehow to get Jedi to use the same python37.dll that our application is using for its functionality, instead of looking for a .exe file that does not exist?
Is there any way we could make some kind of minimal Python installation within our existing app installation that uses the same DLLs/Python Lib etc? How could I go about doing this?
Is there any other way to get Jedi autocompletion working in our app without requiring the user to install Python, or including a full Python installer as part of our build process?
Is it possible to get Jedi not to spawn subprocesses, and instead run its code inside the same instance of Python within which it itself is running? I couldn't find anything about this in the documentation or the source code that deals with Environments, and extrapolating from the discussion here I suspect the answer might be no.
This is definitely possible. All the tools are there. There are discussions ongoing here: https://github.com/davidhalter/jedi-vim/issues/870.
IMO a patch to Jedi is needed that uses an jedi.api.environment.InterpreterEnvironment in some cases like yours. It's definitely possible, it's just buggy at the moment.

Python script that uses pycharm's refactoring methods

I am trying to create a script that will do the following.
Iterate over each file in project.
_Iterate over each method in the file.
__if some_condition(method_name):
___user PyCharm's refactoring system to refactor the given method.
Couldn't find any reference to the above.
Does not request a given script(though wouldn't mind), any reference and start guidance would be welcome.
Using
python 2.7
PyCharm Community Edition 4.5.3
PyCharm is written in Java. There is no way to invoke PyCharm's refactoring system from a Python script.
Instead, you can write a plugin for PyCharm in Java, and implement that logic through PyCharm's plugin API.

Is it possible to implement automatic error highlighting for Python?

Are there any IDEs for Python that support automatic error highlighting (like the Eclipse IDE for Java?) I think it would be a useful feature for a Python IDE, since it would make it easier to find syntax errors. Even if such an editor did not exist, it still might be possible to implement this by automatically running the Python script every few seconds, and then parsing the console output for error messages.
eclipse+pydev
pycharm
many others ....
If you use VIM or don't have a problem with it, try this extension. https://github.com/klen/python-mode
This is for Emacs as well: https://github.com/gabrielelanaro/emacs-for-python
Also pycharm and eclipse with pydev work fine.
If I don't use vim I really enjoy spyder. It is easy to use and has some really nice features, like integrated debugging and profiling, graphical variable explorer and object inspector. The latter shows, e.g., the integrated documentation for every function of class you use.
I built an extension to Eclipse and PyDev that does what you describe, it runs the Python code as you're typing, and displays all the variable values and any exceptions that occur. It's called Live Coding in Python, and the web site has a tutorial and a demo video.
PyDev can highlight some problems in your code by analysing it, and Live Coding in Python can show you problems that happen when you run it.

Categories

Resources