I have a USB to one-wire adapter on my desk, I would like to automate the use of it. I use it for programming dallaskeys, which are badges that are programmed and read over onewire.
There is a managed DLL that offers an API, I love python so I would like to use this third party managed DLL from "python for .NET" aka pythonnet.
My script looks as follows, and runs without problems:
import os
import clr
from clr import System
oneWireLibrary = clr.FindAssembly("OWdotNET")
System.Reflection.Assembly.LoadFile(os.path.abspath(oneWireLibrary))
BUT, I can not figure out how to use the classes in the library. I have tried clr.OWdotNET
but that gives me an attribute error.
I have tried clr.AddReference("OWdotNET") as well.
Links:
http://files.maxim-ic.com/sia_bu/softdev/owdocs_400beta2/Docs/OW.NET/OW.NET_Primer.html
http://pythonnet.github.io/
I'm answering this from the viewpoint of IronPython, but I believe it is similar in Python for .NET.
The clr.AddReference call can be thought of as similar to adding the .NET assembly path to sys.path, you are telling the runtime about it, but then you still need to import the namespace into the current scope.
import clr
clr.AddReference("OWdotNET")
from com.dalsemi.onewire import OneWireAccessProvider
adapter = OneWireAccessProvider.getDefaultAdapter()
Related
I have been coding in python for about 2 months, but I'm only familiar with basic object-oriented programming, so I do not really understand things like how searching for modules is implemented. (Basically I'm a noob.)
I pip installed a package called Opentrons Opentrons 2.5.2 and all its dependencies into the samefolder as a python script I'm currently writing. However when I tried to import the module below[1], I get an error saying that "Opentrons is not a module". Then, I tried shifting it into the python library because I found out the search path using the pprint module and it seems to work. I was wondering if I can specify the search path from the .py file itself instead of manually printing the search path and putting the file into the library that the script searches for. (Willing to put in images of the directories I put the opentrons package in if it helps.)
[1]
import sys
import pprint
pprint.pprint(search.path)
from opentrons import robot, containers, instruments
Edit: I realise that the fact that I am running all my scripts in a Spyder console located in a python 3.6 environment might be important.
You can try using the __import__ function, or importlib. This should allow you to specify the path.
I have some code in a module called XMLModel.py that parses an XML object with lxml.etree. When I try importing lxml.etree generically in an IPython or regular Python shell, it works fine. Command line versions of my code all work fine.
But when I try to have Apache execute the code as part of a web page, I get a bizarre import error:
File "/var/www/html/../ws/python-util/src/util/XMLModel.py", line 4, in <module>
import lxml.etree
ImportError: /opt/epd/7.3-2/lib/libxslt.so.1: undefined symbol: xmlXPathCompiledEvalToBoolean
I've searched for this "undefined symbol" problem but cannot make any sense about it. It might have something to do with building a static instead of dynamic version of lxml but I cannot find anything that spells it out clearly.
Has anyone else had this kind of problem specifically within a browser setting? What could make the import suddenly fail when the code is used that way?
My operating system is Red Hat 4.1.2-48. The directory /opt/epd is just where I store the Enthought Python distribution and then also place related modules, .so stuff, etc. It's all very standard.
I'm using CPython and I have a C# dll. I'm trying to use Python for .NET to make them talk. I can't use IronPython because I need to integrate this into an existing CPython system.
I'm completely new to Python for .NET, and I actually have very little experience with Python and no experience with C#. So please forgive me if my question seems very basic.
I'm using Python 2.7.3, and I downloaded
pythonnet-2.0-alpha2-clr2.0_131_py27_UCS2 and unzipped it into a folder named pyfornet_test, which also contains the dll I'm trying to use (called DotNet4Class.dll)
Then I run this:
import sys
import os
import clr
sys.path.append(r"C:\pyfornet_test")
clr.AddReference("DotNet4Class.dll")
Which gives me this error:
System.IO.FileNotFoundException: Unable to find assembly 'DotNet4Class.dll'.
at Python.Runtime.CLRModule.AddReference(String name) in C:\Users\Barton\Documents\Visual Studio 2008\Projects\PyShar
p\trunk\pythonnet\src\runtime\moduleobject.cs:line 375
Any advice would be much appreciated. Thank you!
One reason can be Windows was not enabling it to load from "external sources". To fix this:
Right-click on the .dll
"Properties"
Under "General", click "Unblock"
Try this (without extension .dll):
clr.AddReference(r"C:\pyfornet_test\DotNet4Class")
Is DotNet4Class.dll built against .NET 4? I assume so based on the naming of the dll.
Note the issue here: http://sourceforge.net/tracker/?func=detail&aid=3293169&group_id=162464&atid=823891
clr.AddReference fails when assembly is built with .NET 4.0 - ID: 3293169
I'd read the solution, but essentially, you need to rebuild and recompile the python for .NET project under .NET 4.
I'll also mention that projects like this, that aren't actively developed and used by lots of people, generally have subtle idiosyncrasies that make knowledge of the platform essential to work around problems such as this. It sounds like you're trying to hack this solution in without understanding much about python or .NET which is always going to be fraught with problems.
Did you try clr.FindAssembly?
import clr
import sys
assemblydir = r"C:\pyfornet_test"
assemblypath = r"C:\pyfornet_test\DotNet4Class.dll"
sys.path.append(assemblydir)
clr.FindAssembly(assemblypath)
I don't know why it works, but this code works on my computer (Python 2.7, .NET4)
I have code like this (I copied MyRightClickMenuService.dll to the same directory as my script.py). It is built against .Net 4.0.
# script.py
import clr
import os
import sys
sys.path.append(os.path.dirname(__file__))
clr.AddReference('MyRightClickMenuService')
clr.AddReference('System')
clr.AddReference('System.Security')
from MyRightClickMenuService import (
AclSecuredNamedPipeBinding,
MyMenuItem,
MyContextMenuService,
etc
)
Checklist
The folder(s) containing the DLL(s) is/are added to sys.path before loading. You may append, or sys.path.insert(0, dll_folder) to put it first on the list.
You call clr.AddReference('my_dll') without the dll extension (for my_dll.dll), after adding the folder to sys.path
The DLL Target Architecture is the same as the CPython version bitness. That is, if Architecture is x64, use 64-bit python, and if Architecture is x86, use 32-bit python. (instructions for this below)
How to check target Architecture for DLL?
I Used ILSpy (free and open source) -> Open DLL -> Check the output. Below example output.
What worked for me was to Unblock the dll file.
if u download the dll file or took it from different computer it might be blocked. So unblocked solved the issue for me.
To unblock right click on the properties if the dll file and check the Unblock box at the bottom
I've got a copy of Lion with all the dev tools installed. Both Python (2.7) and Ruby (1.8) are running just fine. I've installed the Natural Language Tool Kit for Python and tried it out in the Python interpreter and it works
import nltk
>>true
So that works. I've also installed the RubyPython gem, and it seems to work too, but it can't find the nltk module. It's possible I'm doing something wrong. In irb:
require "RubyGems"
require "rubypython" #both true
RubyPython.start # true
n = RubyPython.import "nltk" # RubyPython::PythonError: ImportError: No module named nltk
c = RubyPython.import "cPickle" # works!
RubyPython.stop
I can't figure this one out. The PythonError seems to indicate to me (because it's just a call to the direct C APIs) that the nltk module can't be found by any form of python. But the interpreter finds it just fine. RubyPython, however, cannot.
I've also tried forcing RubyPython to use python2.7 but no change.
What am I missing?
It's simple! For some reason, RubyPython was looking in the wrong place for my Python modules. This was verified by importing sys in both the RubyPython script and in Python, and comparing sys.path. I ended up fixing it by taking the path list of sys in the Ruby script and adding what was missing from the pure Python's path. Then I could load NLTK.
I am using python 2.6.5 on an Ubuntu intalled server.
I need to integrate an API for our applicaion, in that case, i needed to use a DLL given to me by the API provider. Their example of code about api integration is written in Visual Basic... I made a search on google and found some examples of using ctypes , and i try using cdll and pydll, which caused the following error...
OSError: /home//some.dll: invalid ELF header
One possibility is using IronPython, but i do not have much information about ironpython so i am not sure if it will handle my needs completely..
Is there any available module that let me use that dll on python (or aynthing that i am missing from the exixting ones). It is hard to upgrade my python version?
DLLs may be windows creatures, but if a DLL is 'pure .NET' and doesn't utilize executables specific to windows etc., then it can work often in Linux, through Mono. (mono ipy.exe).
Ironpython's System and similiar windows modules are customized to be os agnostic (to a untested degree).
I have successfully run NHibernate, FluentNHibernate, log4net, and a few other commonly used DLLS in Ubuntu.
import clr
import sys
sys.path.append(os.path.abspath('./DLL')) #where your dlls are
clr.AddReference('System')
clr.AddReference('FluentNHibernate')
from FluentNHibernate.Cfg.Db import PostgreSQLConfiguration
The key seems to be to import DLLs in this fashion. If a dll imports another (fluentnhibernate imports nhibernate), you don't need to import Nhibernate for example.
DLLs are Windows creatures. The only way you'll be able to use a DLL is by using a Windows build of Python. You'll be able to run Windows Python on Ubuntu by having Windows installed inside a virtual machine. You also might be able to run it using Wine.
An alternative, of course, is to ask your API provider if they have a Linux version of the API.
First, check if your DLL is a .NET Assembly file. An "Assembly DLL file" has nothing to do with the assembler. It's simply a way the .NET framework stores its bytecode inside a DLL file!
Do file library.dll in Linux. If it says something like this:
PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
then you're lucky: it's an assembly file. You can run it on Linux.
Install Mono. Install Python.NET. Forget IronPython: it's dead.
Now, in Python.NET, you can do this:
import clr
clr.AddReference('./library.dll')
# the library has just registered a namespace we can use
from LibraryName import *
but how do you know what to import?
Auto-complete.
Or use monop tool to inspect the DLL like this:
$ monop -r library.dll
Assembly Information:
LibraryName
Version=9.9.3.0
Culture=neutral
PublicKeyToken=null
LibraryName.ClassName
...
$ monop -r library.dll LibraryName.ClassName
public class ClassName {
public ClassName (string inputString);
...
}
and it will tell you everything about that library