Enable Visual Studio Intellisense for .pyd python modules generated with pybind11 - python

Background: I've successfully used pybind11 in a Visual Studio 2022 MSBuild project to create a .pyd library of C/C++ functions which can be imported into python code directly using the instructions here: https://learn.microsoft.com/en-us/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2022
Problem: Intellisense in VS2022 doesn't seem to recognize the imported module contents and display the expected autocomplete pop-ups or hints about exported functions/symbols/signatures/etc when coding in a .py file which imports this .pyd library. Intellisense does seem to know the library exists (i.e. no warning/error squiggles under the import directive).
Question: Is it possible to enable Intellisense to provide details about the imported module like it would when importing a .py file? Either via some configuration flags or additional content added to the PYBIND11_MODULE(){} section of the C/C++ code?
(Note this was not with Visual Studio Code or a CMake project, but I would be interested if this common problem exists for those tools in case the fix is similar. This discussion is along similar lines, but relevant VSCode and mentions something called pydoc which doesn't come up in the instructions linked above from Microsoft.)

Related

How to create custom VS Code Intellisense documentation for a Python library

I am in the process of creating custom Python libraries with simple functions I use in other projects. My main IDE for using Python is Visual Studio Code, and I find its Python Intellisense very useful.
Is there a way to write or include my own custom documentation for my libraries that will show up in VS Code Intellisense?
As UnholySheep mentioned above, VS Code supports Python docstrings. Including these in my code provided the desired result.

How to enable Intellisense of my own .Net library for a Python application in Visual Studio 2017?

I am using pythonnet ( https://github.com/pythonnet/pythonnet ) and have referenced a .Net library within the same solution space but it does not seem to pull in any information to use for Intellisense. In fact, VS gives an error:
"Unable to resolve "ReferenceLibarary". Intellisense may be missing for this module.
So how do I add intellisense to my "module"?
Even though there is an error, the script can still be run (in or out of VS). I have thorough XML already set up as there is also a sandcastle project for a chm output. Can the sandcastle project output the appropriate content that is missing?
Peter Stevens answer seems fine to me. His solution uses IronPython to generate stubs. These stubs are used in turn by the CPython auto-completion mechanism, in for example, Visual Studio Code.
Let's say you have a c# dll located in c:\dev\HTL\bin\release\HTL.dll you want to access from Python and need auto-completion to help code. With IronPython installed and the ironpython-stubs module downloaded, you can do the following:
ipy -m ironstubs make HTL --path="c:\\dev\\HTL\\bin\\release\\HTL.dll" -output stubs.min
Then, as in Peter's response above, in VS Code use File | Preferences | Settings to add the full stubs.min path to "python.autoComplete.extraPaths". Having imported the target dll into your *.py file in the VS Code editor you should now be able to dot to auto-completion for the classes contained in that dll.
Use the ironpython-stubs module.
Make sure your dll is in sys.path, then run from ironpython
ipy -m ironstubs make --output stubs.min
then set "python.autoComplete.extraPaths" to \ironpython-stubs\release\stubs.min
Works like a charm

Reference a library in IronPython using Visual Studio

I'm new at Python, and I'm trying to write my first Python application on Visual Studio CE 2015 using IronPython.
I finally managed to execute the most basic .NET-compatible code importing the clr module (which comes with the whole IronPython package) but, since my goal is writing an application that reads from a USB scale following this blog post, I'm stuck at trying to import the usb module.
I downloaded and installed both pyUSB and libusb, so I thought I had to simply add the reference in the "reference" section like any other normal .NET applications, but I can't find nor the reference nor the DLL file path. What's the missing link??
import clr
clr.AddReferenceToFileAndPath(r"What\Is\The\Path\Going\Here?")
import usb.core
Thanks to #Xaerxess, I finally understand python libraries are just .py files that are to be found in the compiler directory. I copied the usb directory under the compiler's lib directory and it worked.

How do I view python modules? They are .so files and PyCharm does not recognize how to open them

I want to read how some of the modules work. I thought they would be .py files which could teach me the inner workings of then. However when I import a module it says it is coming from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload/math.cpython-36m-darwin.so
I went to the lib-dynload folder under external libraries in PyCharm but these module files are all '.so' files.
What is a .so file?
How can I view a .so file and learn about the code within it?
If it isn't obvious I am very new to python so please be descriptive with your answer.
.so files are native shared libraries. They are written in C rather than Python. If you're curious about them, you can look at Python's source code.

How to profile a python extension module from Visual Studio 2015

I have a C++ static library .lib that I wrap with cython to create a python extension module. I have been able to build the library and extension module with debug information and attach the Visual Studio Community 2015 debugger to the python process and debug the C++ code.
Now I would like to profile the C++ code using instrumentation. If I chose the VS2015 performance wizard option for instrumentation I can only select projects that are either executables or dlls, I can't select the static library project where my code is. I guess that if I could direct VS to use the python .pyd extension module that may work as it's a dll. However, I don't know how to do that.
I have also tried to find some way of starting the profiler while having the debugger attached to the python process, the same way I do for debugging the C++ code, but I haven't found a way. I can see the PerfTips while stepping through the code, but that's not enough.
Does anybody know how could I profile the C++ code in this static library?
Ultimately, if I can't find a way, I could create another VS2015 executable project and call my lib code from there, then profile the executable project. The only inconvenience with this approach is that I'm passing a few multidimensional arrays from python to the extension module and I would have to first save them from python and then load them in the C++ executable project. I'm not sure how will I do it, but it's doable. It would just be a lot more convenient for me if I was able to profile the C++ lib code when being called from python as I'm going to be doing that multiple times in the future and having to save the python data and then loading it from C++ each time it's a bit of a hassle.

Categories