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

假如想象 提交于 2021-02-08 09:53:41

问题


cmake file

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 output:

/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);
}

I get the error message stated in the tile when I try running

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

Error:

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

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

回答1:


I am using macOS, and I also encounter this TypeError recently! It is probably caused by linking the built .so file to a different Python interpreter's lib/libpython2.7.dylib file.

1. First checkout your .so file by using otool -L command:

$ 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)

Note that this .so file is linked to the Python installed by MacPort, which is located at: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Python

2. But the Python interpreter I am running is a different one.

It's located at: /Users/name/anaconda3/envs/py27/ (which apparently is installed by Anaconda).

3. So the solution is to let cmake link to the right Python library by setting DPYTHON_LIBRARY varible:

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

4. Finally, check the result:

$ 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)

Note that the libpython2.7.dylib's linking path is changed.



来源:https://stackoverflow.com/questions/55238021/typeerror-init-should-return-none-not-nonetype-with-python-boost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!