Is this Boost::Python (Python 3.7) error “__init__() should return None, not 'NoneType'” a linking problem?

后端 未结 2 510
终归单人心
终归单人心 2021-01-20 18:01

Update

I\'m not going to add this as an answer, since I still haven\'t technically solved the problem. But since I\'ve now spent 2.5 days trying to get things to w

2条回答
  •  情话喂你
    2021-01-20 18:11

    I am following a similar example and I adopt the Makefile from here. I have installed python 3.7.4 and boost-python via brew on macOS. To fix the NoneType issue, I follow the procedure below:

    1. Check the Python Path
    To check the python path, use

    which python
    

    If the output does not look like the following one (brew's python installation path)

    /usr/local/opt/python/libexec/bin/python
    

    set the PATH variable as

    export PATH="/usr/local/opt/python/libexec/bin:$PATH"
    

    Check if the Python path looks like the one above again.

    2. Check the Compilation Flag
    Below is the adopted Makefile. Note the LIB variable. If the boost-python flag is -lboost_python, change it to -lboost_python37.

    CPP = clang++
    PYLIBPATH = $(shell python-config --exec-prefix)/lib
    # LIB = -L$(PYLIBPATH) $(shell python-config --libs) -lboost_python
    LIB = -L$(PYLIBPATH) $(shell python-config --libs) -lboost_python37
    OPTS = $(shell python-config --include) -O2
    
    default: hello.so
    
    
    hello.so: hello.o
        $(CPP) $(LIB) -Wl,-rpath,$(PYLIBPATH) -shared $< -o $@
    
    hello.o: hello.cpp Makefile
        $(CPP) $(OPTS) -c $< -o $@
    
    clean:
        rm -rf *.so *.o
    
    .PHONY: default clean
    
    

    Recompile the C++ code and run the python script. The NoneType issue should disappear.

    Hope this helps.

    Note
    If you are using anaconda and want to restore the PATH variable after the above changes, try

    export PATH="~/anaconda3/bin:$PATH"
    

    Your anaconda's path may be different.

    Credit
    1. George's comment in How do I use brew installed Python as the default Python?
    2. leiyc's comment in ld: library not found for -lboost_python on MacOS

提交回复
热议问题