Skip to content Skip to sidebar Skip to footer

How To Use Setuptools Packages And Ext_modules With The Same Name?

I got the following file structure for my Python C Extension project: . ├── setup.py ├── source    ├── cppimplementation    │   └── fastfile.cpp

Solution 1:

You cannot. The first one imported wins. You cannot have scripts/modules/packages/extensions with the same name — one overrides all others.

But you can to have one inside another. Make your extension named fastfilepackage.fastfilepackage and you can import fastfilepackage to import the Python package and import fastfilepackage.fastfilepackage to import the extension; or from fastfilepackage import fastfilepackage.

Solution 2:

As a final solution, I completely removed all Python *.py code because they caused the C Extensions code to become 30% slower. Now my setup.py become like this:

from setuptools import setup, Extension

setup(
        name = 'fastfilepackage',
        version = '0.1.1',

        ext_modules = [
            Extension(
                name = 'fastfilepackage',
                sources = [
                    'source/fastfile.cpp',
                ],
                include_dirs = ['source'],
            )
        ],
    )

File structure:

.
├── setup.py
├── MANIFEST.in
├── README.md
├── LICENSE.txt
└── source
    ├── fastfile.cpp
    └── version.h

MANIFEST.in

include README.md
include LICENSE.txt

recursive-include source *.h

This is the installed file structure: (No *.py files anywhere = 100% performance)

.
└── dist-packages
    ├── fastfilepackage-0.1.1.dist-info
    │   ├── INSTALLER
    │   ├── LICENSE.txt
    │   ├── METADATA
    │   ├── RECORD
    │   ├── top_level.txt
    │   └── WHEEL
    └── fastfilepackage.cpython-36m-x86_64-linux-gnu.so

I just replaced the version.py directly by a C Extensions module attribute:

// https://docs.python.org/3/c-api/arg.html#c.Py_BuildValueconstchar* __version__ = "0.1.1";
PyObject_SetAttrString( thismodule, "__version__", Py_BuildValue( "s", __version__ ) );

References:

  1. Define a global in a Python module from a C API
  2. Original commit: Deprecated the Python implementation *.py

Post a Comment for "How To Use Setuptools Packages And Ext_modules With The Same Name?"