1

I am trying to follow the tutorial for creating python packages from shared objects compiled from C++ via boost::python, but I am running into some problems I need clarification about.

Assume I have a local $install_dir into which I install the compiled shared objects in the form of a python package via CMake. Parallel to the tutorial liked above, my structure is:

$installdir/
  my_package/
    __init__.py
    module/
      __init__.py
      _module.so

I have added $installdir to my $PYTHONPATH.

$installdir/my_package/__init__.py is empty.

$installdir/my_package/module/__init__.py contains:

from _module import *

When I then import my_package.module I get ModuleNotFoundError: No module named '_module' raised from $installdir/my_package/module/__init__.py.

The issue seems to be that _module.so is not found from $installdir/my_package/module/__init__.py.

Why is the approach from the tutorial not working?

If I add $installdir/my_package/module to $PYTHONPATH directly everything works fine, but it feels like that should not be neccessary, as $installdir/my_package/module/__init__.py should find _module.so locally.

I implemented the following portable workaround for now within $installdir/my_package/module/__init__.py:

import sys, pathlib
sys.path.insert(0,str(pathlib.Path(__file__).parent.absolute()))
from _module import *

Bonus Question: Changing the file name extension from .so to .pyd breaks the import (ModuleNotFoundError) even without any packaging and .pyd being accessible directly via $PYTHONPATH. I define the extension via CMake's SUFFIX target property. This is obviously mostly cosmetic, but I would still like to understand the reason and how to fix it.

Edit: This is Ubuntu 20.04 with python 3.8 and boost 1.71

fabmene
  • 55
  • 6
  • AFAIK the pyd extension is a Windows-only thing. Since you have an .so that your Python can import, I guess you're not on Windows. – Dan Mašek May 21 '21 at 15:13
  • The other problem might be due to this? https://stackoverflow.com/questions/12172791/changes-in-import-statement-python3 | It would be really useful if you could mention at least the platform and Python version. – Dan Mašek May 21 '21 at 15:15

0 Answers0