Adding reference to .net assembly which has dots in name and namespace - python

I am trying to refer to assembly which has dots in the namespace.
sys.path.append(assemblyPath)
clr.FindAssembly(r"isc.Eng.Hov")
clr.AddReference(r"isc.Eng.Hov")
print 'isc.Eng.Hov' in clr.ListAssemblies(False)
from isc.Eng.Hov import *
Interpreter raises an error:
Traceback (most recent call last):
True
File "/mnt/86f8c6c8-9099-4f32-be68-486a12918546/GoogleDrive/__BACKLOG/RMK_API_LIB/rmkSuppliersDLLswrappers/scr/Hoval/__phex_hoval_dllwrapper.py", line 14, in <module>
from isc.Eng.Hov import *
ImportError: No module named isc.Eng.Hov
How to troubleshoot?

the solution was to use ILSPY to investigate the DLL and find dependencies (right click recursively for each DLL and click on add dependencies). Then I copied all the dependencies to the same folder where the main DLL was. After that, I ran:
print [a for a in clr.ListAssemblies(False)
and get the list of dependencies which are actually involved:
u'isc.Eng.Hov', u'Microsoft.VisualBasic', u'System.Windows.Forms', u'System.Drawing', u'Accessibility'
and left them in the folder.
After doing so the part of code:
sys.path.append(assemblyPath)
clr.AddReference("isc.Eng.Hov")
import isc.Eng.Hov as isk
from isk import *
started work.
Thank you all, especially #denfromufa for help!

Related

ModuleNotFoundError: No module named 'data_management' in PyCharm

In github there are four py Data which I put on my PyCharm. When I run main.py I get this message:
/Users/Armut/Desktop/High_D/Coursera/bin/python /Users/Armut/Desktop/High_D/main.py
Traceback (most recent call last):
File "/Users/Armut/Desktop/High_D/main.py", line 6, in <module>
from data_management.read_csv import *
ModuleNotFoundError: No module named 'data_management'
Here is a screenshots:
Can someone help, what I am doing wrong or how can I fix it?
EDIT (Put folders):
/Users/Armut/Desktop/High_D/Coursera/bin/python /Users/Armut/Desktop/High_D/main.py
WARNING:root:Failed to import geometry msgs in rigid_transformations.py.
WARNING:root:Failed to import ros dependencies in rigid_transforms.py
WARNING:root:autolab_core not installed as catkin package, RigidTransform ros methods will be unavailable
Traceback (most recent call last):
File "/Users/Armut/Desktop/High_D/main.py", line 7, in <module>
from visualization.visualize_frame import VisualizationPlot
ModuleNotFoundError: No module named 'visualization.visualize_frame'
EDIT:
/Users/Armut/Desktop/High_D/Coursera/bin/python /Users/Armut/Desktop/High_D/src/main.py
Traceback (most recent call last):
File "/Users/Armut/Desktop/High_D/src/main.py", line 7, in <module>
from src.visualization.visualize_frame import VisualizationPlot
File "/Users/Armut/Desktop/High_D/src/visualization/visualize_frame.py", line 10, in <module>
from utils.plot_utils import DiscreteSlider
ModuleNotFoundError: No module named 'utils.plot_utils'
Edit (No errors, but I just get a blank picture):
Edit (I installed matplotlib 3.0.3 and got this):
The issue here is, that it is just a picture. If you can see there are buttons like "next". I should be able to click it so I can track it. But how does it work?
Do the following
from read_csv import *
import visualize_frame as vf
The reason why it was not working for you is because you were importing files that dont exist on your system. When you do from data_management.read_csv import *, what you are telling the Python interpreter to do is to search for a folder called data_management inside you're Coursera folder and get everything from read_csv.py.
This is the same case with visualize_frame. Since you have a flat directory structure, you dont need the folder names. You can directly import the .py files as is.
Another thing to note here is that I personally wouldn't do from read_csv import * because I will be flooding my namespace with a lot of things I probably wont use. I would rather use import read_csv as any_alias_you_like. This way I only fill my namespace with what I want by doing the following
x = any_alias_you_like.function_call()
The reason why I didn't do this with the main code solution is because I am not sure where all you are using read_csv functions and classes in your code and if that is not accounted for by prefxing the alias name properly, you will run into a multiple errors. So my advice is to identify all the funcutions/classes that you are using in read_csv.py and prefix them properly with an alias.
I also used the import statement for the visualize_frame differently. This is because, when you do a from import..., you are only partially initializing the module. However, a proper import visualize_frame will ensure that your entire module is initialized in one call and you can use everything it offers by simply prefixing the alias.
Read about the difference between from import and import... here.
Read about how Python searches for libraries here.

How do I import a file in python?

I am trying to import a python file
like this
import tiny
tiny.create_window('test')
and inside tiny is
from tkinter import *
def create_window (Name='Window'):
window = Tk()
window.title(Name)
return window
but when I run it gives an error
Traceback (most recent call last):
File "C:\Users\Harry\Documents\python files\tests\test4.py", line 1, in <module>
import tiny
ModuleNotFoundError: No module named 'tiny'
Is your "tiny.py" file in the same directory as your main "test4.py" file?
In this case are both your tiny.py and test4.py files in
C:\Users\Harry\Documents\python files\tests\
Python interpreter doesn't know what directories to search in to find the file.
Thus both your files must be in the same place for the interpreter to know where to find them.
Alternatively you can look up some tutorials on creating modules and packages but that is a bit more advanced.

How do I install Python module given as .py file?

I do realize this is a noobish question, but I've been trying for an hour and I can't get it right.
So, I have a Python script which I'd like to modify a bit and play around with as a Python beginner. However, at the very beginning of the script, there's this:
from priodict import priority_dict
Now, I have a file named priodict.py that came with the script. But how do I make it available to the script so it can be included like that?
The Python manual has pages and pages on installing modules, but they all seem to refer to "packages" which are to be placed in certain directories etc. What do I do when I have just the .py file?
I know there is probably a banale one-sentence response to this, but I'm getting frustrated and I'm short on time so I decided to take the easy way out and ask Stack overflow about it.
It seems that, if I don't have the priodict.py file, I get this error:
Traceback (most recent call last):
File "C:\Python27\scripts\dijksta.py", line 192, in <module>
main()
File "C:\Python27\scripts\dijksta.py", line 185, in main
D, _ = dijkstra(G, 1, v)
File "C:\Python27\scripts\dijksta.py", line 139, in dijkstra
Q = priority_dict() # est.dist. of non-final vert.
NameError: global name 'priority_dict' is not defined
If I place the file in the same directory as my script, I get this error:
Traceback (most recent call last):
File "C:\Python27\scripts\dijksta.py", line 2, in <module>
from priodict import priority_dict
ImportError: cannot import name priority_dict
These are the files in question:
https://github.com/kqdtran/ADA1/tree/master/dijkstra
Place the file in the same directory, and that will get you started. Seems you've figured out that much. If all you have is a .py file, that's what you're usually expected to do.
If you're unable to import a name from a module, it usually means that name doesn't exist in that module. Try:
import priodict
print dir(priodict)
Is priority_dict listed? If not, is there a similarly named attribute that might be what you're looking for? It may just be that the instructions given you were misspelled, like the _ wasn't needed.
If it fails on the import line, it may be that there's an error in the module code itself that must be corrected first. you'll get an error telling you roughly where it is.

Pycap not working in script

i m trying to capture ethernet packet using pycap http://pycap.sourceforge.net/. when i use following command on python prompt with root privileges, it is working
>>>import pycap.capture
>>>p = pycap.capture.capture("wlan0")
>>>p.next()
(Ethernet(type=0x608, 00:1b:b1:46:53:5d -> ff:ff:ff:ff:ff:ff), ARP(op=0x1, protocol=0x800, 00:1b:b1:46:53:5d (192.16.68.10) -> 00:00:00:00:00:00 (192.16.110.39)), '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 1307898356.222398)
But if i run these commands as a script, i m getting following error
>>>sudo python pycap.py
Traceback (most recent call last):
File "pycap.py", line 2, in <module>
from pycap import *
File "/home/nikhil/Code/Python/pycap.py", line 5, in <module>
p = capture.capture(device)
NameError: name 'capture' is not defined
Any suggestions?
pycap http://pycap.sourceforge.net/ says it is requires python2.3 and im using python2.6. Is that a problem?
Your script using pycap is called pycap itself, so import pycap imports itself (. is usually the first directory on the import path). Because imports are caches, this doesn't lead to infinite recursion but instead gives you a reference to your own module, which of course doesn't define capture or anything else. Rename it.

python cdll can't find module

I have a library consisting of two dll files and one python wrapper.
I currently have code based on these three files being in the same parent directory as my main python file. I am now attempting to refactor things before I continue development and would like to move said library code into it's own lib/ directory. Unfortunately, nothing I've tried helps.
import ctypes
_lib = ctypes.cdll["./my.dll"]
The above code located in the python wrapper file loads the dll perfectly fine in it's original location. I've tried various ways of loading it in the new location such as:
from ctypes import *
import os
path = os.path.dirname(os.path.realpath(__file__))
_lib = ctypes.CDLL(os.path.join(path, 'my.dll'))
However python always throws an exception saying unable to find the module.. I have copy and pasted the path to verify that it is in fact the valid absolute path to the .dll file
Does anyone know what I need to do in order to relocate this library to a sub folder? I could always leave it where it is but I simply hate clutter.
I had the same issue with trying to load magic1.dll - this file depends on two other .dll's, and when I moved magic1.dll from my current working dir - I could not load.
This workaround helped:
pathToWin32Environment = os.getcwd() + "/environment-win32/libmagic/"
pathToDll = pathToWin32Environment + "magic1.dll"
if not os.path.exists(pathToDll):
#Give up if none of the above succeeded:
raise Exception('Could not locate ' + pathToDll)
curr_dir_before = os.getcwd()
os.chdir(pathToWin32Environment)
libmagic = ctypes.CDLL('magic1.dll')
os.chdir(curr_dir_before)
The exact error message would help know for sure, but ctypes.dll(path) doesn't seem valid to me.
eg. I get this, with Python 2.7:
>>> ctypes.dll("kernel32.dll")
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'dll'
Perhaps you meant this instead:
>>> _lib = ctypes.CDLL(os.path.join(path, 'my.dll'))

Categories