【问题标题】:TypeError: __init__() should return None, not 'NoneType' with Python BoostTypeError: __init__() 应该返回 None,而不是 Python Boost 的“NoneType”
【发布时间】:2019-08-09 19:18:35
【问题描述】:

cmake 文件

cmake_minimum_required(VERSION 3.13)
project(p1)

set(CMAKE_CXX_STANDARD 11)

FIND_PACKAGE(PythonInterp)

if (PYTHONINTERP_FOUND)
if (UNIX AND NOT APPLE)
    if (PYTHON_VERSION_MAJOR EQUAL 3)
        FIND_PACKAGE(Boost COMPONENTS python${PYTHON_VERSION_SUFFIX})
        FIND_PACKAGE(PythonInterp 3)
        FIND_PACKAGE(PythonLibs 3 REQUIRED)
    else()
        FIND_PACKAGE(Boost COMPONENTS python)
        FIND_PACKAGE(PythonInterp)
        FIND_PACKAGE(PythonLibs REQUIRED)
    endif()
 else()
    if (PYTHON_VERSION_MAJOR EQUAL 3)
        FIND_PACKAGE(Boost COMPONENTS 
  python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})
        FIND_PACKAGE(PythonInterp 3)
        FIND_PACKAGE(PythonLibs 3 REQUIRED)
    else()
        FIND_PACKAGE(Boost COMPONENTS 
  python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR})
        FIND_PACKAGE(PythonInterp)
        FIND_PACKAGE(PythonLibs REQUIRED)
    endif()
endif()
else()
  message("Python not found")
 endif()

 message(STATUS "PYTHON_LIBRARIES = ${PYTHON_LIBRARIES}")
 message(STATUS "PYTHON_EXECUTABLE = ${PYTHON_EXECUTABLE}")
 message(STATUS "PYTHON_INCLUDE_DIRS = ${PYTHON_INCLUDE_DIRS}")
 message(STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}")

 #ENABLE_TESTING()
 INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})


  add_library(pylib SHARED pylib.cpp)
 target_link_libraries(pylib ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
 #
 # Tweaks the name of the library to match what Python expects
set_target_properties(pylib PROPERTIES SUFFIX .so)
set_target_properties(pylib PROPERTIES PREFIX "")

cmake 输出:

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake - 
DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" 
/Users/studentuser/CLionProjects/sbmlPythonAPI
-- Found PythonInterp: /usr/local/bin/python (found version "2.7.16") 
-- Boost version: 1.68.0
-- Found the following Boost libraries:
--   python27
-- PYTHON_LIBRARIES = /usr/lib/libpython2.7.dylib
-- PYTHON_EXECUTABLE = /usr/local/bin/python
-- PYTHON_INCLUDE_DIRS = 
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr
/include/python2.7
-- Boost_LIBRARIES = /usr/local/lib/libboost_python27-mt.dylib
-- Configuring done
-- Generating done
-- Build files have been written to: 
/Users/studentuser/CLionProjects/sbmlPythonAPI/cmake-build-debug

Bonjour.hpp

#include <iostream>
#include <string>
using namespace std;

class Bonjour
{
// Private attribute
string m_msg;

public:
// Constructor
Bonjour(string msg):m_msg(msg) { }

// Methods
void greet() { std::cout << m_msg << std::endl; }

void check_func() {cout<<"Hello! I am working"; }

// Getter/Setter functions for the attribute
void set_msg(std::string msg) { this->m_msg = msg; }
std::string get_msg() const { return m_msg; }
};

pylib.cpp

#include <boost/python.hpp>
#include "Bonjour.hpp"

using namespace boost::python;

BOOST_PYTHON_MODULE(pylib)
{
class_< Bonjour >("Bonjour", init<std::string>())
  .def("greet", &Bonjour::greet)
  .add_property("msg", &Bonjour::get_msg, &Bonjour::set_msg);
}

我在尝试运行时收到磁贴中所述的错误消息

  from pylib import Bonjour
  b = Bonjour("He")

错误:

 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
 <ipython-input-2-a019b42ef03f> in <module>()
 ----> 1 b = Bonjour("He")

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

【问题讨论】:

  • 您能否edit 向我们展示产生此错误消息的源文件,以及您能否向我们展示确切的编译器错误(您的标题很有帮助,但不清楚是否“ with Python Boost”是否是错误消息的一部分)。
  • 谢谢。以及错误消息的确切文本(包括文件名和行号)?
  • @MartinBonner 更新

标签: c++ python-3.x python-2.7 boost boost-python


【解决方案1】:

我用的是macOS,最近也遇到这个TypeError!这可能是由于将构建的 .so 文件链接到不同的 Python 解释器 lib/libpython2.7.dylib 文件引起的。

1。 首先使用otool -L 命令检查您的.so 文件:

$ otool -L libh264decoder.so 
libh264decoder.so:
    /somepath/build/libh264decoder.so (compatibility version 0.0.0, current version 0.0.0)
    /usr/local/opt/ffmpeg/lib/libavcodec.58.dylib (compatibility version 58.0.0, current version 58.35.100)
    /usr/local/opt/ffmpeg/lib/libswscale.5.dylib (compatibility version 5.0.0, current version 5.3.100)
    /usr/local/opt/ffmpeg/lib/libavutil.56.dylib (compatibility version 56.0.0, current version 56.22.100)
    /usr/local/opt/boost-python/lib/libboost_python27-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
    /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)

注意这个.so文件链接到MacPort安装的Python,位于:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/Python

2。 但我正在运行的 Python 解释器是不同的。

它位于:/Users/name/anaconda3/envs/py27/(显然是由 Anaconda 安装的)。

3。 所以解决方案是通过设置DPYTHON_LIBRARY 变量让cmake 链接到正确的Python 库:

$ cd build/
$ cmake -DPYTHON_LIBRARY="/Users/name/anaconda3/envs/py27/lib/libpython2.7.dylib" ..
$ make

4。最后,查看结果:

$ otool -L libh264decoder.so 
libh264decoder.so:
    /somepath/build/libh264decoder.so (compatibility version 0.0.0, current version 0.0.0)
    /usr/local/opt/ffmpeg/lib/libavcodec.58.dylib (compatibility version 58.0.0, current version 58.35.100)
    /usr/local/opt/ffmpeg/lib/libswscale.5.dylib (compatibility version 5.0.0, current version 5.3.100)
    /usr/local/opt/ffmpeg/lib/libavutil.56.dylib (compatibility version 56.0.0, current version 56.22.100)
    /usr/local/opt/boost-python/lib/libboost_python27-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libpython2.7.dylib (compatibility version 2.7.0, current version 2.7.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)

请注意libpython2.7.dylib的链接路径已更改。

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2018-06-25
    • 1970-01-01
    相关资源
    最近更新 更多