TypeError:__init __()应该使用Python Boost返回None,而不是'NoneType' [英] TypeError: __init__() should return None, not 'NoneType' with Python Boost

查看:99
本文介绍了TypeError:__init __()应该使用Python Boost返回None,而不是'NoneType'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

错误:

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

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

推荐答案

我正在使用macOS,最近我也遇到了这个 TypeError !可能是由于将生成的 .so 文件链接到另一个Python解释器的 lib/libpython2.7.dylib 文件引起的.

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.

$ 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

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

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

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

$ 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 的链接路径已更改.

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

这篇关于TypeError:__init __()应该使用Python Boost返回None,而不是'NoneType'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆