Reference a library in IronPython using Visual Studio - python

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.

Related

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

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.)

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

Creating dll of python script with functions using OpenCV functions

I created a dll file using Visual Studio 2010 for python script which contains only python function and used it in labview using "Call Library Function Node" VI available in labview. It works fine.
Followed the same procedure in the link below
"http://www.originlab.com/doc/OriginC/guide/Access-Python-via-External-DLL"
But if I am creating a dll file for python script containing opencv and python functions labview throws memory issue.
Do I need to add some other dependencies of OpenCV as well. If so, then what should be added?

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.

pywintypes.__import_pywin32_system_module doesn't work on server [PYTHON]

I wrote a program that uses win32 library.
To be more specific - this program uses win32.client package, When I turn on the script on the computer, everything works. the problem appears when trying to write cgi which uses the code of my program.
What may causes this problem?
why the server program can not download the needed resource?
When trying to find the cause, I cameacross on the file pythoncom.py
Code below:
import pywintypes
pywintypes.__import_pywin32_system_module__("pythoncom", globals())
P.S. I copied the folder from the library to the win32 directory ktrym project is located
Problem resolved! unnecessarily I copied folder with library win32 to fiolder with my project, and pythons interpreter had a problem with localization this library.

Categories