Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8425323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:14:20+00:00 2026-06-10T04:14:20+00:00

I have a Python project with many sub-modules that I package up with distutils.

  • 0

I have a Python project with many sub-modules that I package up with distutils. I would like to build some Python extensions in C to live in some of these sub-modules but I don’t understand how to get the Python extension to live in a submodule. What follows is the simplest example of what I’m looking for:

Here is my Python extension c_extension.c:

#include <Python.h>

static PyObject *
get_answer(PyObject *self, PyObject *args)
{
    return Py_BuildValue("i", 42);
}

static PyMethodDef Methods[] = {
    {"get_answer",  get_answer, METH_VARARGS, "The meaning of life."},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initc_extension(void) {
  (void) Py_InitModule("c_extension", Methods);
}

And here is a setup.py that works:

from distutils.core import setup
from distutils.extension import Extension

setup(name='c_extension_demo',
      ext_modules = [Extension('c_extension', sources = ['c_extension.c'])])

After installing in an virtualenv I can do this:

>>> import c_extension
>>> c_extension.get_answer()
42

But I would like to have c_extension live in a sub-module, say foo.bar. What do I need to change in this pipeline to be able to get the behavior in the Python shell to be like this:

>>> import foo.bar.c_extension
>>> foo.bar.c_extension.get_answer()
42
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:14:21+00:00Added an answer on June 10, 2026 at 4:14 am

    Just change

    Extension('c_extension', ...)
    

    to

    Extension('foo.bar.c_extension', ...)
    

    You will need __init__.py files in each of the foo and bar directories, as usual. To have these packaged with the module in your setup.py, you need to add

    packages = ['foo', 'foo.bar'],
    

    to your setup() call, and you will need the directory structure

    setup.py
    foo/
        __init__.py
        bar/
            __init__.py
    

    in your source directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I have a python project, I write my unit-testing code in many of
We have a python project that we want to start testing using buildbot. Its
I have a Python project with following directory structure: /(some files) /model/(python files) /tools/(more
We have a project that contains a library of Python and Scala packages, as
I need to have an at-home project now that I'm working on Python/Django at
I have a project in Python 2.6 and I'd like to write a utf-8
I have a python project with this directory structure and these files: /home/project_root |---__init__.py
I have a Python Facebook project hosted on Google App Engine and use the
I'm using SQLalchemy for a Python project, and I want to have a tidy
I say project file in the loosest sense. I have a few python projects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.