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
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
Adding an answer here for those using the Anaconda or Conda-Forge Distribution:
The python interpreter statically links in the libpythonXY
library. Which is why it makes the python binary different compared to other distributions.
The fix for the problem reported by OP is to use:
-undefined dynamic_lookup
Instead of:
-lpythonXY
You are creating a Python C/C++ extension, and not embedding the python interpreter. So you shouldn't be linking to the python library. Pybind11 handles this correctly.
See the following for more information:
One a side note, python 3.8 has added an additional flag: --embed
and only then it adds -lpythonXY
in the output:
$ python3.8-config --libs
-ldl -framework CoreFoundation
$ python3.8-config --libs --embed
-lpython3.8 -ldl -framework CoreFoundation