[Python-Dev] Importing DLLs on Windows

Tim Peters tim.one@home.com
Sat, 21 Apr 2001 03:51:09 -0400


Sorry for the delay -- I had a hard time understanding what this writeup
meant, so had to download the package and try it.

[M.-A. Lemburg]
> Python or Windows seems to have trouble importing DLLs when
> inside packages. I'm working on an extension which pulls in a
> DLL gmp31.dll which lives inside a package dir mx/Number/mxNumber
> along side the Python wrapper extension mxNumber.pyd.

Concretely, I have these files now, under my Python 2.1 installation
directory:

C:\Python21>dir/b/on mx\Number\mxNumber
gmp31.dll
mxNumber.pyd
mxNumber.h
test.py
__init__.py

C:\Python21>

> Currently, I'm using this code in the subpackage's __init__.py:

And by "the subpackage" here I believe you mean the tail-end mxNumber
directory, previously called "a package".  IOW, you're talking specifically
about

    \Python21\mx\Number\mxNumber\__init__.py

If you meant something else, scream.

> # On Windows we also distribute the GMP DLL (in the win32 subdir). To
> # have Win32 find the DLL we need to be explicit about the path in
> # sys.path though. This trick works for all startup directories
> # *except* \PythonXX (for some reason this fails), but is not thread
> # safe...
> import sys, os
> if sys.platform[:3] == 'win':
>     abspath = os.path.abspath(__path__[0])
>     sys.path.insert(0, abspath)
>     from mxNumber import *
>     sys.path.remove(abspath)
>
> else:
>     from mxNumber import *

> I don't have any clue why the import works

Which import are you talking about?  Please show exactly what you do that
fails -- I haven't been able to find any problem here.  For example, I
replaced

    \Python21\mx\Number\mxNumber\__init__.py

which contains the code you showed above, with this two-liner:

from mxNumber import *
from mxNumber import __version__

Having done that, here's a shell session started in the installation
directory, and after a reboot "just to make sure":

C:\Python21>python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> from mx.Number import *
>>> Integer(928349238479328749L)**2
861832308585149602001042573617905001
>>>

So nothing failed.  What do *you* do that fails?  Here's another session
started from a "random" directory:

C:\Code>\python21\python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> from mx.Number import *
>>> Integer(92387498327493243879L)**2
8535449847212566935021074270390170966641
>>>

Same thing.

> from everywhere *except* the Python21 install directory...

It would more helpful to name a specific directory than to make an untrue
claim <wink -- but you didn't try every directory other than Python21, and
the specific directories you actually did try may be important>.

BTW, it's a mondo cool package!  I had a lot of fun with it.  But then I was
able to, since I stopped trying to guess what your problem was <wink>.
What's up?  I was running Win98SE in the above, but that shouldn't make a
difference.  Perhaps, during development, you left crap sitting around in
your installation directory that's confusing your attempts to import things?
If not, please be very explicit about what you do that fails, and what
"fails" means (crash, ImportError, Windows error box, ...?).