TypeError: __init__() should return None, not 'NoneType'

2018/2/8 posted in  Python comments

After linking a .so with boost-python:

-L/usr/local/lib -lboost_serialization -lboost_python -lpython2.7 -O2

When I import it to a .py file, I got

TypeError: __init__() should return None, not 'NoneType'

This is due to the mismatch of libpython in .so and current interpreter:

$ otool -L model.so 
    model.so (compatibility version 0.0.0, current version 0.0.0)
    /System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.10)


Use -L to inform the linker where the correct libpython is:

-L`python -c "import distutils.sysconfig as c; print(c.PREFIX)"`/lib -L/usr/local/lib -lboost_serialization -lboost_python -lpython2.7 -O2


  1. https://stackoverflow.com/questions/20437769/boost-python-init-should-return-none-not-nonetype
  2. https://github.com/Homebrew/legacy-homebrew/issues/10541#issuecomment-4229171