pythonc++pipswig

Failing to build a python package with SWIG


I want to create a local Python package with SWIG C++ bindings. However, I have been unsuccessful with the last step of the process, in which I need to call pip install -e .So far, my setup.py has the following setup:

from distutils.core import setup, Extension

import os
import sys
import shutil
from sys import platform

if platform == "darwin":
    IRDLIB_PATH = "/Users/john/Documents/GitHub/IRDLIB/IRDLIB/time"
    IRDLIB_SWIG_PATH = "/Users/john/Documents/GitHub/IRDLIB/IRDLIB-SWIG/SWIG"

    # Clean build
    for file in os.listdir(IRDLIB_SWIG_PATH + "/date"):
        if file in ["__init__.py", "test.ipynb", "__pycache__"]:
            continue
        else:
            os.remove(IRDLIB_SWIG_PATH + "/date/" + file)
    os.system(
        "swig -c++ -python -I" + IRDLIB_PATH + " -outdir " + IRDLIB_SWIG_PATH + "/date"
        " -o "
        + IRDLIB_SWIG_PATH
        + "/date/date_wrap.cpp "
        + IRDLIB_SWIG_PATH
        + "/date.i"
    )
    name = "date"  # name of the module
    version = "1.0"  # the module's version number
    setup(
        name=name,
        version=version,
        py_modules=["SWIG.date.__init__", "SWIG.date.date"],
        include_dirs=[IRDLIB_PATH],
        ext_modules=[
            Extension(
                "SWIG.date._date",
                ["SWIG/date/date_wrap.cpp", IRDLIB_PATH + "/date.cpp"],
                include_dirs=[IRDLIB_PATH],
                depends=[IRDLIB_PATH + "/date.hpp"],
            )
        ],
    )

My folder structure is as seen in the uploaded image. When I execute pip install -e ., I get no issues, and the bindings work only if I am in the date folder, as expected. However, it does not work outside of that folder, i.e., if I would use

import date 

in for example IRDLIB/time then I would get module error. What steps do I need to take for it to work together with pip globally?

I tried to investigate similar posts on this website but I have found no success. Thanks in advance.


Solution

  • I must have missed it myself when I created the directory but the setup.py file was in the wrong path. Thus leading to the issue. If I move it into SWIG it compiles successfully.