0

I have a shared library hello_ext.so. I need to import this shared library in a python script. The directory is:

- project
| - py_binding
  | - hello_ext.cpp
  | - CMakeLists.txt
| - python_script.py
| - CMakeLists.txt

The binding is correct and works.

After cmake .. and make , the library can be called from the build folder.

$ python3
$ from hello_ext import greet
$ greet()
'hello, world'

Therefore the pipeline works. However, if I run the python script using python3 /<path_to_sscript>/python_script.py I get this error:

Traceback (most recent call last):
  File "/home/sharad/git_repository/longitudinal_control_pid/py_bindings/arduino_connection_usb.py", line 1, in <module>
    from hello_ext import greet
ModuleNotFoundError: No module named 'hello_ext'

I believe the .so is not visible from python script. I am using cmake as for the project.

CMakeLists.txt under py_binding dir:

cmake_minimum_required(VERSION 3.0.0)

set(ThisBinding hello_ext)

find_package(PythonLibs 3.6 REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)

set(CMAKE_SHARED_MODULE_PREFIX "")

add_library(${ThisBinding} MODULE hello_ext.cpp)
target_link_libraries(${ThisBinding} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(${ThisBinding} PRIVATE ${PYTHON_INCLUDE_DIRS})

CMakeLists.txt in the major directory:


cmake_minimum_required(VERSION 3.0.0)

# set the project name
project(longitudinal_control_pid CXX)

# Set some global variables
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

enable_testing()
add_subdirectory(googletest)

file(GLOB SOURCES 
    src/*.cpp
)

file(GLOB HEADERS 
    include/*.h
)

add_library(longitudinal_control_pid ${SOURCES})

target_include_directories(longitudinal_control_pid PUBLIC ./)

add_subdirectory(test)
add_subdirectory(py_bindings)

I am using Ubuntu 20.04, python3. I want to make this script import the built shared library and use the bindings.

I have googled this and have not found a solution which worked for me.

Does anyone know how to make it work?

Thanks

shrdrwt01
  • 1
  • 2
  • It is unclear what exactly doesn't work. – n. m. could be an AI Nov 08 '20 at 21:58
  • I edited the questions. I hope it is more clear now. – shrdrwt01 Nov 09 '20 at 07:57
  • The shared library needs to be in the module search path of python. – n. m. could be an AI Nov 10 '20 at 07:19
  • I am trying to avoid putting .so file in this directory because I would have to do so everytime I change something on the C++ side. Is there a way to include the .so file from the build? This is possible in `bazel` where I can just make the python target depend on `.so` target. Is it possible with cmake? – shrdrwt01 Nov 10 '20 at 21:09
  • Python has a thing called search path. https://stackoverflow.com/questions/3144089/expand-python-search-path-to-other-source How cmake or bazel could possibly influence that is not something I understand. – n. m. could be an AI Nov 11 '20 at 07:29

0 Answers0