Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49129 > unrolled thread

Unable to import NHunspell.dll using ctypes in Python

Started byakshay.ksth@gmail.com
First post2013-06-24 23:23 -0700
Last post2013-06-25 07:52 -0700
Articles 11 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Unable to import NHunspell.dll using ctypes in Python akshay.ksth@gmail.com - 2013-06-24 23:23 -0700
    Re: Unable to import NHunspell.dll using ctypes in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-25 08:07 +0100
      Re: Unable to import NHunspell.dll using ctypes in Python akshay.ksth@gmail.com - 2013-06-25 00:32 -0700
        Re: Unable to import NHunspell.dll using ctypes in Python Dave Angel <davea@davea.name> - 2013-06-25 03:45 -0400
        Re: Unable to import NHunspell.dll using ctypes in Python Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-06-25 09:54 +0200
        Re: Unable to import NHunspell.dll using ctypes in Python Dave Angel <davea@davea.name> - 2013-06-25 09:55 -0400
    Re: Unable to import NHunspell.dll using ctypes in Python akshay.ksth@gmail.com - 2013-06-25 00:58 -0700
      Re: Unable to import NHunspell.dll using ctypes in Python Dave Angel <davea@davea.name> - 2013-06-25 10:05 -0400
    Re: Unable to import NHunspell.dll using ctypes in Python akshay.ksth@gmail.com - 2013-06-25 01:04 -0700
      Re: Unable to import NHunspell.dll using ctypes in Python Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-06-25 10:32 +0200
    Re: Unable to import NHunspell.dll using ctypes in Python akshay.ksth@gmail.com - 2013-06-25 07:52 -0700

#49129 — Unable to import NHunspell.dll using ctypes in Python

Fromakshay.ksth@gmail.com
Date2013-06-24 23:23 -0700
SubjectUnable to import NHunspell.dll using ctypes in Python
Message-ID<23e633db-afbe-4edc-9291-5a9e242e86d0@googlegroups.com>

Im required to import ha certain dll called 'NHunspell.dll' which is used for Spell Checking purposes. I am using Python for the software. Although I checked out several websites to properly use ctypes, I have been unable to load the dll properly.

When I use this code.

  from ctypes import *
  hunspell = cdll.LoadLibrary['Hunspellx64.dll']

I get an error

  hunspell = cdll.LoadLibrary['Hunspellx64.dll']
  TypeError: 'instancemethod' object has no attribute '__getitem__'

I guess it might a problem with the structure of the dll. But I have no idea how to import the dll properly.

[toc] | [next] | [standalone]


#49131

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-06-25 08:07 +0100
Message-ID<mailman.3799.1372144029.3114.python-list@python.org>
In reply to#49129
On 25/06/2013 07:23, akshay.ksth@gmail.com wrote:
>
>
> Im required to import ha certain dll called 'NHunspell.dll' which is used for Spell Checking purposes. I am using Python for the software. Although I checked out several websites to properly use ctypes, I have been unable to load the dll properly.
>
> When I use this code.
>
>    from ctypes import *
>    hunspell = cdll.LoadLibrary['Hunspellx64.dll']
>
> I get an error
>
>    hunspell = cdll.LoadLibrary['Hunspellx64.dll']
>    TypeError: 'instancemethod' object has no attribute '__getitem__'
>
> I guess it might a problem with the structure of the dll. But I have no idea how to import the dll properly.
>

 >>> from ctypes import *
 >>> help(cdll.LoadLibrary)
Help on method LoadLibrary in module ctypes:

LoadLibrary(self, name) method of ctypes.LibraryLoader instance.

So looks as if you need:-

hunspell = cdll.LoadLibrary('Hunspellx64.dll')

-- 
"Steve is going for the pink ball - and for those of you who are 
watching in black and white, the pink is next to the green." Snooker 
commentator 'Whispering' Ted Lowe.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#49135

Fromakshay.ksth@gmail.com
Date2013-06-25 00:32 -0700
Message-ID<4c1ad40e-cc53-4414-8822-101d5591f598@googlegroups.com>
In reply to#49131
Thanks for the reply Mark. I did what you suggested. 
But now I'm getting an error like this.

Traceback (most recent call last):
  File "start.py", line 15, in <module>
    hunspell = cdll.LoadLibrary('/home/kuro/Desktop/notepad/Hunspellx64.dll')
  File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/kuro/Desktop/notepad/Hunspellx64.dll: invalid ELF header

I am really new to using ctypes and dll files. Can you please guide me out. 

[toc] | [prev] | [next] | [standalone]


#49136

FromDave Angel <davea@davea.name>
Date2013-06-25 03:45 -0400
Message-ID<mailman.3801.1372146364.3114.python-list@python.org>
In reply to#49135
On 06/25/2013 03:32 AM, akshay.ksth@gmail.com wrote:
> Thanks for the reply Mark. I did what you suggested.
> But now I'm getting an error like this.
>
> Traceback (most recent call last):
>    File "start.py", line 15, in <module>
>      hunspell = cdll.LoadLibrary('/home/kuro/Desktop/notepad/Hunspellx64.dll')
>    File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
>      return self._dlltype(name)
>    File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
>      self._handle = _dlopen(self._name, mode)
> OSError: /home/kuro/Desktop/notepad/Hunspellx64.dll: invalid ELF header
>
> I am really new to using ctypes and dll files. Can you please guide me out.
>

This is why it's frequently useful to supply the information with your 
original question:  what version of Python, and what OS.

You're on Linux or similar, and dll's are the way a Windows executable 
is named.  So chances are you're trying to install a Windows dll on 
Linux, which is unlikely to work except under very special circumstances 
(eg. within WINE).

Try going back to where you downloaded this file, and see if you can get 
the one for your OS.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#49137

FromChris “Kwpolska” Warrick <kwpolska@gmail.com>
Date2013-06-25 09:54 +0200
Message-ID<mailman.3802.1372146868.3114.python-list@python.org>
In reply to#49135
On Tue, Jun 25, 2013 at 9:45 AM, Dave Angel <davea@davea.name> wrote:
> You're on Linux or similar, and dll's are the way a Windows executable is
> named.

dll’s are libraries for windows, not executables (/lib not /bin)

> Try going back to where you downloaded this file, and see if you can get the
> one for your OS.

That is not necessary.  Because it is a sane OS, it is likely that
hunspell is packaged in the repositories of OP’s system, and might be
even installed there.  And if the package is installed, hunspell
should reside in /usr/lib/libhunspell.so or a similar place.
--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html

[toc] | [prev] | [next] | [standalone]


#49161

FromDave Angel <davea@davea.name>
Date2013-06-25 09:55 -0400
Message-ID<mailman.3830.1372168539.3114.python-list@python.org>
In reply to#49135
On 06/25/2013 03:54 AM, Chris “Kwpolska” Warrick wrote:
> On Tue, Jun 25, 2013 at 9:45 AM, Dave Angel <davea@davea.name> wrote:
>> You're on Linux or similar, and dll's are the way a Windows executable is
>> named.
>
> dll’s are libraries for windows, not executables (/lib not /bin)
>
>> Try going back to where you downloaded this file, and see if you can get the
>> one for your OS.
>
> That is not necessary.  Because it is a sane OS, it is likely that
> hunspell is packaged in the repositories of OP’s system, and might be
> even installed there.  And if the package is installed, hunspell
> should reside in /usr/lib/libhunspell.so or a similar place.

Sorry, since I was talking about a DLL, I used Microsoft's terminology. 
  Certainly in Unix terms, a DLL is roughly equivalent to a shared library.

DLL is one of the dozens of file extensions commonly used for Microsoft 
executables, or PE files.  Microsoft doesn't generally make the 
distinction between an executable which can be called from the command 
line and one which can only be used with LoadLibraryEx().  In common 
use, only the "main" executable of a program will have an entry point, 
but both common types may be used for library code.

http://msdn.microsoft.com/en-us/library/windows/hardware/gg463119.aspx


As for finding it in the repository, you're certainly right.  I had 
naiively assumed that the OP would have looked there first, and wouldn't 
be in this state if it were in the repository (eg. Synaptic).  It is in 
Ubuntu 12.04.




-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#49138

Fromakshay.ksth@gmail.com
Date2013-06-25 00:58 -0700
Message-ID<bfa57c71-14f9-4c09-8cdb-a56b1f746340@googlegroups.com>
In reply to#49129
Thanks Dave. 
I'm using Python 2.7 and am working on Linux Mint. 
Does it mean that I cant load the functions within the dll whilst on Linux. I thought that was what ctypes was used for.

Please correct me if I misunderstood what you meant.

[toc] | [prev] | [next] | [standalone]


#49162

FromDave Angel <davea@davea.name>
Date2013-06-25 10:05 -0400
Message-ID<mailman.3831.1372169143.3114.python-list@python.org>
In reply to#49138
On 06/25/2013 03:58 AM, akshay.ksth@gmail.com wrote:
> Thanks Dave.
> I'm using Python 2.7 and am working on Linux Mint.
> Does it mean that I cant load the functions within the dll whilst on Linux. I thought that was what ctypes was used for.
>
> Please correct me if I misunderstood what you meant.
>

ctypes is not for cross-platform development, it's for cross-language 
development.  If you have a shared library for your own system whose 
interfaces are designed for a static-typed language (usually C), ctypes 
lets you bridge the gap, and call it from Python.

Your best bet is probably either to find a Windows machine, or to run an 
actual Windows inside a Virtual Box.  I do that whenever I have to 
support an application that's not available for Linux.  In either case, 
you're actually running Windows, so you'll need a Windows python, which 
knows how to call the Windows LoadLibrary and getProcAddress and other 
such OS-specific interfaces.

There's a possibility that running a Windows version of Python under 
WINE will work to access a Windows DLL, but the likelihood is so high 
that there'll be inconsistencies that I wouldn't bother.

VirtualBox and WINE are both available in most Linuxes, for example in 
Synaptic.  And they should also be available via apt-get, but I don't 
know how to find them that way.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#49139

Fromakshay.ksth@gmail.com
Date2013-06-25 01:04 -0700
Message-ID<947df083-7586-4127-9f48-6d9367cf43ae@googlegroups.com>
In reply to#49129
On Tuesday, June 25, 2013 12:08:17 PM UTC+5:45, aksha...@gmail.com wrote:
> Im required to import ha certain dll called 'NHunspell.dll' which is used for Spell Checking purposes. I am using Python for the software. Although I checked out several websites to properly use ctypes, I have been unable to load the dll properly.
> 
> 
> 
> When I use this code.
> 
> 
> 
>   from ctypes import *
> 
>   hunspell = cdll.LoadLibrary['Hunspellx64.dll']
> 
> 
> 
> I get an error
> 
> 
> 
>   hunspell = cdll.LoadLibrary['Hunspellx64.dll']
> 
>   TypeError: 'instancemethod' object has no attribute '__getitem__'
> 
> 
> 
> I guess it might a problem with the structure of the dll. But I have no idea how to import the dll properly.

@Chris 
I understand that. But then I am supposed to create something that works on a Windows environment using a Windows Dll.Aaandd Im stuck. 

[toc] | [prev] | [next] | [standalone]


#49141

FromChris “Kwpolska” Warrick <kwpolska@gmail.com>
Date2013-06-25 10:32 +0200
Message-ID<mailman.3804.1372149487.3114.python-list@python.org>
In reply to#49139
On Tue, Jun 25, 2013 at 10:04 AM,  <akshay.ksth@gmail.com> wrote:
> @Chris
> I understand that. But then I am supposed to create something that works on a Windows environment using a Windows Dll.Aaandd Im stuck.
> --
> http://mail.python.org/mailman/listinfo/python-list

Then you need to get your hands on a copy of Windows.  Or try messing
with Wine (you would need to install the Python interpreter for
Windows there), but that might not work properly.

And ctypes is used to use functions provided by C libraries on your
system, that is .dll files on Windows and .so files (shared object
files) everywhere else.

--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html

[toc] | [prev] | [next] | [standalone]


#49165

Fromakshay.ksth@gmail.com
Date2013-06-25 07:52 -0700
Message-ID<8dd2d262-2cac-4946-8087-18dd1e36dcf3@googlegroups.com>
In reply to#49129
Thanks everyone. But it still did not work. I instead used a Python wrapper for Hunspell called Pyhunspell. The actual link in PyPi does not work for Python 2.7 but it has been improved and upgraded in [here](https://github.com/akshaylb/nepali-spellchecker-v2/tree/master/pyhunspell).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web