这是Boost::Python(Python 3.7)错误吗__init__()应返回None,而不是';非类型'&引用;链接问题? 更新
我不打算添加这个作为答案,因为我还没有从技术上解决这个问题。但由于我现在花了2.5天的时间试图让boost-python3发挥作用,我已经失去了接受它的意愿 我刚刚遇到(我之前对python绑定工具的冗长搜索没有找到它,我不知道),我正在使用它。2.5天的痛苦与相比,我遵循了一个标准,我采用了来自的Makefile。我已经通过这是Boost::Python(Python 3.7)错误吗__init__()应返回None,而不是';非类型'&引用;链接问题? 更新,python,c++,python-3.x,boost,boost-python,Python,C++,Python 3.x,Boost,Boost Python,我不打算添加这个作为答案,因为我还没有从技术上解决这个问题。但由于我现在花了2.5天的时间试图让boost-python3发挥作用,我已经失去了接受它的意愿 我刚刚遇到(我之前对python绑定工具的冗长搜索没有找到它,我不知道),我正在使用它。2.5天的痛苦与相比,我遵循了一个标准,我采用了来自的Makefile。我已经通过brew在macOS上安装了python3.7.4和boostpython。要解决NoneType问题,我遵循以下步骤: 1。检查Python路径 要检查python路径,
brew
在macOS
上安装了python3.7.4
和boostpython
。要解决NoneType
问题,我遵循以下步骤:
1。检查Python
路径要检查
python
路径,请使用
which python
如果输出与下面的不一样(brew
的python
安装路径)
将路径
变量设置为
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
检查Python
路径是否与上面的路径相同
2。检查编译标志下面是采用的
Makefile
。注意LIB
变量。如果boost python
标志是-lboost\u python
,则将其更改为-lboost\u 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
您的anaconda
路径可能不同
信贷1. 乔治在
2. leiyc在中的评论为使用Anaconda或Conda Forge发行版的用户添加了一个答案: python解释器静态链接在
libpythonXY
库中。这就是为什么它使python二进制文件与其他发行版不同的原因
OP报告的问题的修复方法是使用:
-undefined dynamic_lookup
而不是:
-lpythonXY
您正在创建一个Python C/C++扩展,而不是嵌入Python解释器。因此,您不应该链接到python库。Pybind11正确地处理了这个问题
有关更多信息,请参见以下内容:
--embed
,然后才在输出中添加-lpythonXY
:
$ python3.8-config --libs
-ldl -framework CoreFoundation
$ python3.8-config --libs --embed
-lpython3.8 -ldl -framework CoreFoundation
谢谢Nick,这看起来可以解决这个问题,所以我会接受信任,即使我无法测试(根据问题中的更新,很早就移动到pybind了)。谢谢你给我一个很好的解释和回答。非常感谢你,这已经解决了我的相关问题。
which python
/usr/local/opt/python/libexec/bin/python
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
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
export PATH="~/anaconda3/bin:$PATH"
-undefined dynamic_lookup
-lpythonXY
$ python3.8-config --libs
-ldl -framework CoreFoundation
$ python3.8-config --libs --embed
-lpython3.8 -ldl -framework CoreFoundation