The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
[Originally posted by grandinc]

Hi Guys (and everyone else reading this):
Here's my problem:

I want to get into writing extensions in C for Python. I read pages 269-278,
and did the example on pages 271-275. I am using MSVC++ 6.0 Professional to
compile a DLL, and then I copy the example1.dll into Python21DLLs. When I
write a simple program to import example1:

--start Python code--
import example1
print i,"+",j,"=",example1.add(i,j)
--end python code--

I get the following error:
--start of error--
Traceback (most recent call last):
File "", line 301, in
exec codeObject in __main__.__dict__
File "", line 1, in ?
import example1
ImportError: dynamic module does not define init function (initexample1)
--end of error--

I tried renaming the init function 'initexample1' as the error suggests, but I
get the same result.

Any ideas? This is one of those things that once the solution is found, it's
smooth sailing...
Happy Summer
import-bot (20211) [Avatar] Offline
Re: Problem with importing C extensions.
[Originally posted by daryl harms]

Hi Chris,

This was one of Ken's chapters, but I'll do my best to try and answer you.

Changing example1_init to initexample1 should have fixed your problem. Its
possible you weren't seeing your modification.

There have been some other changes since the book was written that might help
you out. The disutils tool has made things much easier for the compile and
installation steps. Just create a file like:

-------start of
import sys
from distutils.core import setup, Extension

example1_ext = Extension('example1', sources=['example1.c'])

setup (name = "example1",
ext_modules = [ example1_ext ]

-----------end of

and place it along with example1.c in the same directory and type the
following into a command shell where you've moved into the directory:

C:My DocumentsPythonmodules>python install

This should compile and install for you. Try moving to this method of creating
your dll and see if you still are having problems.

See "Installing Python Modules" and "Distributing Python Modules" on the
Python documentation page that comes with Python 2.1 for more details.

You also probably will want to move on to the "Extending and Embedding the
Python Interpreter" document off of the "Extending and Embedding" link on the
Python documentation page. It has been nicely expanded since the book was
written and is more up to date than the book.


import-bot (20211) [Avatar] Offline
Re: Problem with importing C extensions.
[Originally posted by grandinc]

Thank you so much!!
Your solution worked perfectly and I can now get to writing all the
functionality I need from C into Python.

Just for your information (and maybe interest at what your readers are up to),
I wrote a conversion routine for GIS systems, from UTM (Universal transverse
mercator) to standard Latitude/Longitude and vice versa. I used Python to
rapidly produce the code and then switched it over to C (which took a little
longer). Now I want to make a new Python module which uses the C code. C
code is considerably faster for such tasks, but Python is excellent for
wrapping this code. Anyways I thank you very much again, and good luck for
the future!