[Tutor] Declaring a Python/C Package and Accessing Python Constants from C API

Adam Pridgen atpridgen at mail.utexas.edu
Sat May 12 01:12:02 CEST 2007


I guess to follow this question up, I am trying to combine an
Extension module and Python module under a single package, and I am
not having any luck with it. I know I have an error in my
configuration script, but I am not sure where or how.    Here is my
directory structure:

I am trying to setup my modules so that I can do the following:
import python_foo.foo_defines
import python_foo.foo_interface

setup.py
build/ <-- results from set install
python_foo/
        __init__.py
        foo_defines.py
        foo_interface.c
        foo_interface.h


import os
from distutils.core import setup, Extension
from distutils.sysconfig import get_python_inc

incdir = os.path.join(get_python_inc(plat_specific=1))

module = Extension('foo_interface',
	include_dirs = [incdir],
	libraries = [],
	library_dirs = [],
	sources = ['python_foo/foo_interface.c'])

setup(name = 'python_foo',
		packages = ['python_foo'],
		package_dir = {'python_foo' : 'python_foo'},
    	ext_modules = [module],
		py_modules = ['python_foo.foo_defines' ]
		)

I perform: python setup.py install and it compiles and installs without error,
but when I try the following:

import python_foo
from python_foo import foo_interface

It says it can not find the module.

Thanks for any help in advance,

Adam


On 5/11/07, Adam Pridgen <atpridgen at mail.utexas.edu> wrote:
> Hello everyone,
>
> I am trying to create a python package that includes both a native
> python module and then a python module created using the Python/C API.
>  I have several questions pertaining to this area.  First, how do I
> make the  C API module aware of the python module.  Specifically, I
> have declared all of my constant variables because it was much easier
> and cleaner, but I want to be able to access those constants from my C
> API module.  I know I can declare the python module globally, but the
> python module and the C API module are part of the same package.  I
> have read Python C/API guide, and I could not find anything like an
> example or explanation related to performing this task.  I have also
> seen a couple of posts online, but they seem to miss over this point.
> Is there anyone out there that has done something like this before?  I
> am on a Posix/Linux system.
>
> Thanks in advance,
>
> Adam
>


More information about the Tutor mailing list