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.
You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name.
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'])],
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With