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 8660055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:09:38+00:00 2026-06-12T16:09:38+00:00

I would like to gather multiple Python modules under one package, so they do

  • 0

I would like to gather multiple Python modules under one package, so they do not reserve too many names from global set of python packages and modules. But I have problems with modules that are written in C.

Here is a very simple example, straight from the official Python documentation. You can find it at the bottom of the page from here: http://docs.python.org/distutils/examples.html

from distutils.core import setup
from distutils.extension import Extension
setup(name='foobar',
      version='1.0',
      ext_modules=[Extension('foopkg.foo', ['foo.c'])],
      )

My foo.c file looks like this

#include <Python.h>

static PyObject *
foo_bar(PyObject *self, PyObject *args);

static PyMethodDef FooMethods[] = {
    {
        "bar",
        foo_bar,
        METH_VARARGS,
        ""
    },
    {NULL, NULL, 0, NULL}
};

static PyObject *
foo_bar(PyObject *self, PyObject *args)
{
    return Py_BuildValue("s", "foobar");
}

PyMODINIT_FUNC
initfoo(void)
{
    (void)Py_InitModule("foo", FooMethods);
}

int
main(int argc, char *argv[])
{
    // Pass argv[0] to the Python interpreter
    Py_SetProgramName(argv[0]);

    // Initialize the Python interpreter.  Required.
    Py_Initialize();

    // Add a static module
    initfoo();

    return 0;
}

It builds and installs fine, but I cannot import foopkg.foo! If I rename it to just “foo” it works perfectly.

Any ideas how I can make the “foopkg.foo” work? For example changing “foo” from Py_InitModule() in C code to “foopkg.foo” does not help.

  • 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-12T16:09:39+00:00Added an answer on June 12, 2026 at 4:09 pm

    There must be an __init__.py file in the foopkg folder, otherwise Python does not recognize as a package.

    Create a foopkg folder where setup.py is, and put an empty file __init__.py there, and add a packages line to setup.py:

    from distutils.core import setup
    from distutils.extension import Extension
    setup(name='foobar',
          version='1.0',
          packages=['foopkg'],
          ext_modules=[Extension('foopkg.foo', ['foo.c'])],
          )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to gather usage statistics for my iPhone app using Google Analytics
Would like to make anapplication in Java that will not automatically parse parameters used
I have a large matrix from which I would like to gather a collection
I would like to be able to gather all records in a table where
i have three tables i would like to link in this one query. The
I am writing a program in C#.NET I would like to gather the total
I would like to be able to spawn a process in python and have
I would like to gather some ideas how you can implement Javascript in a
I have an iterable of entries on which I would like to gather some
I have a list of players that I would like to gather stats in

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.