1

I am trying to execute a python interpreter from a python environment, called env_tensorflow, using pybind11 library.

Even though, I include and link the cpp file to the pybind11 library which is included from that environment (env_tensorflow), the interpreter prints its binary path as:

/usr/bin/python3

However, I expect the output to be:

/home/user/miniconda3/envs/env_tensorflow/bin/python3

What am I doing wrong? How can I run the python interpreter from a specific environment?

main.cpp

#include "/home/user/miniconda3/envs/env_tensorflow/include/python3.6m/Python.h"
#include "/home/user/miniconda3/envs/env_tensorflow/include/python3.6m/pybind11/pybind11.h"
#include "/home/user/miniconda3/envs/env_tensorflow/include/python3.6m/pybind11/embed.h"

namespace py = pybind11;
int main() {
    py::scoped_interpreter guard{};
    py::module sys = py::module::import("sys");
    py::print(sys.attr("executable"));
    //prints: /usr/bin/python3
    //expected print: /home/user/miniconda3/envs/env_tensorflow/bin/python3
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(my_proj)
set(CMAKE_CXX_STANDARD 11)

add_library(my_python3.6m SHARED IMPORTED)
set_target_properties(my_python3.6m PROPERTIES
        IMPORTED_LOCATION "/home/user/miniconda3/envs/env_tensorflow/lib/libpython3.6m.so"
        INTERFACE_INCLUDE_DIRECTORIES "/home/user/miniconda3/envs/env_tensorflow/include/python3.6m/"
        )

add_executable(my_proj main.cpp)
target_link_libraries(my_proj my_python3.6m)
2
  • 1
    Do you have your env_tensorflow environment activated during this entire process (cmake, make, run)? Commented Sep 24, 2018 at 12:46
  • It was not, activating the environment solved the issue, thank you very much! Commented Sep 24, 2018 at 13:03

1 Answer 1

2

You need to activate the Conda environment:

source activate env_tensorflow

Once you've done that, you should be able to run cmake, make, and your application with the correct Python interpreter. You probably don't need the IMPORTED target in CMake, you can just link against python3.6m as normal, so long as the environment is active during the build process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.