pythonnet clr how to add reference to a dll assembly? - python

I am trying to import a dll package into python through pythonnet clr. I am aware that the package CLR and pythonnet both end up having a namespace called clr so the command "import clr" can be equivocal. Long story short, I seem to need pythonnet not the other one. I would like to be able to specify the address of the dll assembly; this one works:
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
Apath=os.path.normpath("C://Folder//Folder//Folder//AssemblyA.dll")
clr.AddReference(Apath)
but this one fails (got the idea of this one from here):
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
BfolderPath=os.path.normpath("C://Folder//Folder//Folder")
sys.path.append(BfolderPath)
clr.AddReference('AssemblyB.dll')
I get the following error when I try running the second one:
"System.IO.FileNotFoundException: Unable to find assembly 'AssemblyB.dll'.
at Python.Runtime.CLRModule.AddReference(String name)"
this one also fails
import os as os
import clr
#https://pypi.org/project/pythonnet/#:~:text=NET%20Common%20Language%20Runtime%20(CLR,to%20embed%20Python%20into%20a%20.
import sys
BfolderPath=os.path.normpath("C://Folder//Folder//Folder")
clr.AddReferenceToFileAndPath(Bpath)
"AttributeError: module 'clr' has no attribute 'AddReferenceToFileAndPath'"
ps1. I need the second or the third way to work because I have to be sure the second assembly is not confused with another one with a similar name.
ps2. I cannot find the documentation of pythonnet or see what kind of commands are available in my clr. Any idea?
Any tip is appreciated.

In second idea,
remove .dll from clr.AddReference('AssemblyB.dll') and use clr.AddReference('AssemblyB') because clr.AddReference() Requires only assembly name whether is it .exe of .dll not the folder path
That's why first idea is not working!
And for third idea clr.AddReferenceToFileAndPath() is not working because it part of Ironpython not pythonnet.
Ironpython is also used like pythonnet, but Ironpython is using Managed python code, while pythonnet is not using Managed code.

Related

Why is python unable to detect notification module in plyer?

I can't get this simple statement to work:
from plyer import notification
getting:
ImportError: cannot import name 'notification'
the import statement is correct and is used the same way in examples.
I couldn't find any special instructions to use this library so I'm assuming there aren't any.
I installed plyer using pip and it installed successfully. verified the files are in place. I tried using python 3.5 and 3.6, same result.
It seems the package is recognized but just the module isnt found?
Would appreciate some insight :)
A common cause for this kind of problem is having a script or module by the same name in a location that comes before the expected module or package's location in sys.path so it gets imported instead of the expected module or package.
The simple way to sort this out is to add this simple line before:
import plyer; print(plyer);
and check the result which will diplay the path of whatever named plyer was first found. Chances are it's a script in your current working directory...

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?

Python: cannot import from PyQt5.QtCore (attempting example script)

I'm introducing myself to PyQt5 through one of its included examples. On this windows 7 machine, I have installed Python 3.4 x64, and PyQt5 using its binary provided on riverbankcomputiong.com. The documentation says that the binary already includes everything necessary to run. I (perhaps incorrectly) assumed that i can safely skip the "configure" and "build" steps at Riverbank's installation guide, since the guide only talks about .zip, .tar, etc. files.
I used the tutorial located here:
http://www.pythonschool.net/pyqt/introduction-to-pyqt/
Which also says "just run the binary to install pyqt4 there is no step three."
When i attempt to run any tutorial containing reference to PyQt4 or PyQt5:
from PyQt5.QtCore import *
I get the following error message:
ImportError: DLL load failed: The specified module could not be found.
But when i enter the following:
import PyQt5
The interpreter seems to be okay with it -- no errors.
I can't help but think I've done something wrong installation, because even when I run the examples included with PyQt4/PyQt5, i get importerrors. It seems as though QtCore doesn't even exist in relation to PyQt4 or PyQt5. What's going on here?
There seem to be a few possibilities:
You might need to update your PATH environment variable to include the location where the library is installed (i.e. directory location where the DLLs are)
Take note of where you're launching the interpreter or scripts from as their current path may not necessarily include what you expect.
You can check this with:
>>> import os
>>> os.environ['PATH'].split(os.pathsep)
Make sure the installation process was truly successful (i.e. zero errors) and that you downloaded the correct binary package for your PC's architecture.
I ran into a similar issue with PySide where import PySide would work but import PySide.QtCore would fail.
I'd expect the code to work after verifying those things:
>>> from PyQt5.QtCore import *
>>>
If it doesn't, update the question with additional details like things you checked, their values, error messages, etc.

How to import a module from a directory?

On my system I have two versions of Python (to call them I type python and python2 in the command line). When I use the first version of Python, I cannot import sklearn module but I can do it in the second version of Python.
I would like to use the first version of python (because other modules are available there) and, at the same time, I would like to be able to import sklearn from this version of Python.
My solution was to use:
import sys
sys.path.append('location_of_the_sklearn_module')
To find the location of the sklearn module I started a python session (using the second version of python, in which sklearn works). The I type:
import sklearn
sklearn.__file__
As a result I got:
/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn/__init__.pyc
In the session of the first version of Python I tried:
import sys
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
import sklearn
Unfortunately it did not work. As a result I got: ImportError: No module named sklearn
Does anybody know what I am doing wrong and if it is possible to reach the goal in the way I try?
When importing packages, you need to add the parent directory of the package to PYTHONPATH, not the package directory itself, so just change...
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
...to...
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages')
...although it may not necessarily import correctly in Python 3.x.

Not Able to import PIL in Sikuli

I want to import PIL in Sikuli for doing that I have added the code below
import sys
sys.path.append("C:\\Python27\Lib\site-packages")
sys.path.append("C:\\Python27\Lib\site-packages\PIL")
import PIL
import Image
So it imported the above modules but when I am trying to import ImageFont, it's giving me the error below:
def getmask2(self, text, mode="", fill=Image.core.fill):
File "C:\Python27\Lib\site-packages\PIL\Image.py", line 39, in __getattr__
raise ImportError("The _imaging C module is not installed")
ImportError: The _imaging C module is not installed
I am able to import PIL and Image and ImageFont successfully in Python scripts and code is running fine.
Any idea how to fix this?
The Sikuli IDE uses Jython so packages installed to your local Python version won't be able to be imported within the Sikuli IDE and Jython doesn't support c extensions as far I'm aware which PIL requires ( see http://bugs.jython.org/issue1388 ).
I think you would need to use a Java library, maybe something like ImageJ ( see http://fiji.sc/wiki/index.php/Jython_Scripting )
Since the following (as I think valuable information) was rejected as an edit to the original question (my intention: people should see this in the first place), I put it as an answer now:
I want to add:
Python modules can be imported in Jython/Sikuli scripts as long as they contain only Python code. There are many modules written for Python, that hold this condition (e.g. xlrd, xlwt). But as correctly mentioned: PIL contains C-based stuff and this can normally (exception JNI libs) not be loaded (hence the above error message, which is true, but might lead in the wrong direction).

Categories

Resources