How to get available modules with pythonnet - python

I'm trying to import a .net dll into python and would like to figure out how to see what modules are available.
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form
After the clr.AddReference line how can I see what is available to import? I would like to know that System.Windows.Forms is available and that Form is available.
I have an internal .net dll I am trying to work with, and couldn't figure out how to see what the modules I needed to call without an example. Once I got this point, I can see what's available with something like
form = Form()
print(dir(form))
I was able to get the info I needed with the following code:
dll_ref = System.Reflection.Assembly.LoadFile(full_path)
print(dll_ref.FullName)
print(dll_ref.Location)
for i in range(len(dll_ref.DefinedTypes)):
print(dll_ref.DefinedTypes[i])

There are tools that can show you members in .NET DLL. One is Object Explorer in Visual Studio. There is also a free tool from JetBrains called dotPeek.
If you want to do it from Python, you need to either dir on a namespace that you must know in advance, or use .NET reflection to inspect the DLL programmatically.

Related

How do I change the name of the python DLL when compiling a library like pillow?

Question:
I want to compile a third-party library like Pillow or Numbpy but I want to change the name of the python27.dll to corepython27.dll. How do I do this during the compile process? Is it something I need to change in the setup.py? Or the distutils library?
I should explain that I have no experience in compiling at all. I just know that I will need to make this change as I learn more about the basics of compiling.
Explanation:
Corel's PaintShop Pro uses an embedded python interpreter to run scripts inside the program. And I would like to be able to use third-party libraries like pillow and numpy but they always fail to load. The version of python that is included with PaintShop Pro is 2.7.5. I've made sure to download the appropriate versions of these libraries but it always fails with a "DLL module doesn't exist" type error.
Using a PE viewer I was able to see that other libraries like TKinter were using imports corepython27.dll instead of python27.dll like pillow was.
Also pillow for 2.7 was using msvcr90.dll but the custom version of the tkinter library included with PSP was compiled with msvcr110.dll. Do you think this will be an issue? Do I need to compile pillow with the appropriate version of msvcr DLL? Or is matching versions (2.7) and making sure it uses the correct python.dll (corepython27.dll) the only important thing?
You can create a symbolic link named corepython27.dll showing to the installed python27.dll. You can do this in your console via the command
MKLINK <path_to_corepython.dll> <path_to_python27.dll>

Embedded Python in C++ -- rolling in numpy -- where's the import lib?

I'm on Windows using/embedding Python 3.4
I'm trying to embed Python in an existing C++ application. I've pretty much done the job, but I can't get numpy to work.
I have numpy installed via the whl file from http://www.lfd.uci.edu/~gohlke/pythonlibs/ Everything works just fine in python.exe, but when I do something like...
PyRun_SimpleString("import numpy\n");
...in the C++ application, I get a traceback which ends with:
\Python\lib\site-packages\numpy\core\__init__.py, line 6, in <module>
from . import multiarray
cannot import 'multiarray'
Well, this made some sense to me. The module multiarray is in ...\Python34\Lib\site-packages\numpy\core\multiarray.pyd I think these .pyd files are basically DLLs, which means an import library is needed.
For example, ...\Python34\lib contains import libraries (I think) for other modules that are contained in pyd files. You have to link against the import lib. in order to import the module in the C++\Python (eg via PyRun_SimpleString("import _elementtree\n"); )
I suppose I need an import library for numpy to link my C++ application against. Anybody know off hand if building numpy from source pops these out? The build process looks tricky, but maybe it's the only options here.
Also, obviously when Python is built, it can't be linked against import libraries for modules that aren't installed yet. In other words, my python.exe wasn't linked against this magic import library I'm looking for, but it can still import numpy, how? Can I recreate this in my application?

Excuting cpp file from Python

I need to execute C++ code to acquire images to process in python.
I need to use these commands from python:
make and
./name_of_the_executable
Could anybody please help know me how to do it?
The standard module os provides the system call that allows execution of arbitrary operating system commands from Python. For example:
import os
os.system('ls -latr');
Look For python.net which is cable of making a call to the interfaces written in .net supported languages.
What all you need to do is
Steps:
Download and put it two files Python.Runtime.dll and clr.pyd in your DLLs folder.
From you >>>(triple greater than prompt )Python prompt Try
>>>import clr
if it doesn't gives any error you are good to go .
Next You need to do is put Your C++ Dll inside Lib/site-packages Folder .
(This is not mandatory but good for beginners).
Next to import clr try importing your Dll as a module import YourDllName
If step 5 doesn't gives you a error . Hola You are done That's All Folks :)

Python - How do you import downloaded directories/modules?

This is the first time I have attempted to use anything other than what's provided by python.
I have recently gotten into pythons provided Tkinter, though due to some issues I decided to use another GUI, and heard that PyQt was highly recommended, so I downloaded that and looked into various tutorials. In these tutorials, I cannot seem to execute any of the import statements in said tutorials that relate to PyQt, primarily PyQt5 (I have checked I have the correct version number by the way).
So for instance:
import PyQt5
raises the error:
Traceback (most recent call last):
File "/Users/MEBO/PycharmProjects/Music/testing.py", line 1, in <module>
import Qt
ImportError: No module named 'Qt'
[Finished in 0.1s with exit code 1]
I have a lot of research into this. I've heard people talk of using pip to install modules, and I have done this be safe (as well as downloading it from the internet), I've tried changing the project interpreter to versions Python3/ 2.7/ 2.6, appending the path name to the sys.path directory, (which I really know nothing about to be honest, I was hoping I'd get lucky), though nothing seems to work.
Are you supposed to be able to just import a module off the bat, or do you have to set some things up first?
For windows download the package and extract it to (path where python installed)\Python27\Lib and then try to import.
Specific to PyQt
This package cannot just be downloaded and imported, it must be built because it is not pure python, it uses Qt (C++) and requires dependancies. Read this tutorial on installation.
There is also a very complete python package distribution, Anaconda, that includes pyqt and much more. Almost all the packages I ever looked at are in there.
In general to pure python code
In other cases, if you place modules/code that has been download into the directory that your python script is run from, you can import off the bat, or you can append/insert any folder to the sys.path.
# importer will search here last
sys.path.append('/path/to/code/')
# importer will search here second, right after script's directory
# this can be useful to override a module temporarily...
sys.path.insert(1,'/path/to/code/')

Extract GUID from ActiveX COM DLL using Python

How would you extract the GUID of an ActiveX COM DLL using only Python in a cross-platform manner?
I ran across pythoncom, which comes with pywin32. It has a LoadTypeLib function which can do just that:
import pythoncom
dll = pythoncom.LoadTypeLib("C:\\path\to\\my.dll")
print str(dll.GetLibAttr()[0])
This is better than my current solution which relies on an external CLI application, however a cross-platform solution would be even better as I don't believe pywin32 can be installed on a Linux CI easily.
I ended up using the wonderful comtypes package:
from comtypes.typeinfo import LoadTypeLibEx
lib = LoadTypeLibEx("C:\\path\to\\my.dll")
print lib.GetLibAttr().guid
The code is effectively the same; it literally calls the same LoadTypeLib from OleAut32 so it is Windows-only, however comtypes is installable through pip which makes the distribution easier.

Categories

Resources