3 回答

TA贡献1772条经验 获得超5个赞
在此处为使用 Anaconda 或 Conda-Forge 发行版的人添加答案:
python 解释器在libpythonXY库中静态链接。这就是为什么它使 python 二进制文件与其他发行版不同的原因。
OP 报告的问题的解决方法是使用:
-undefined dynamic_lookup
代替:
-lpythonXY
您正在创建 Python C/C++ 扩展,而不是嵌入 Python 解释器。所以你不应该链接到 python 库。Pybind11 正确处理了这个问题。
有关更多信息,请参阅以下内容:
https://gitlab.kitware.com/cmake/cmake/issues/18100
https://github.com/ContinuumIO/anaconda-issues/issues/9078
一个旁注,python 3.8 添加了一个额外的标志:--embed然后才-lpythonXY在输出中添加:
$ python3.8-config --libs
-ldl -framework CoreFoundation
$ python3.8-config --libs --embed
-lpython3.8 -ldl -framework CoreFoundation

TA贡献1934条经验 获得超2个赞
我正在遵循一个类似的例子,我采用了这里的 Makefile 。我已经安装python 3.7.4,并boost-python通过brew上macOS。为了解决这个NoneType问题,我按照以下步骤操作:
1.检查Python路径
要检查python路径,请使用
which python
如果输出看起来不像下面的(brew的python安装路径)
/usr/local/opt/python/libexec/bin/python
将PATH变量设置为
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Python再次检查路径是否与上面的类似。
2. 检查编译标志
下面是采用的Makefile. 注意LIB变量。如果boost-python标志是-lboost_python,请将其更改为-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
重新编译C++代码并运行python脚本。这个NoneType问题应该消失了。
希望这可以帮助。
注意
如果您正在使用anaconda并希望PATH在上述更改后恢复变量,请尝试
export PATH="~/anaconda3/bin:$PATH"
你anaconda的路径可能不同。

TA贡献1851条经验 获得超3个赞
跟进Nehal对基于 Anaconda 的构建的回答。重新阅读 cmake 的 FindPython 文档显示Python::Module目标已添加到 cmake 3.15 中以创建模块。这意味着 CMakeLists.txt 应该是:
set(Python3_FIND_VIRTUALENV FIRST)
find_package(Python3 REQUIRED Development)
find_package(Boost REQUIRED python3)
add_library(classes MODULE classes.cpp)
target_link_libraries(classes PRIVATE Python3::Module Boost::python3)
显然,这Python3::Python是为了在另一个应用程序中嵌入 Python。
添加回答
举报