pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: os.getcwd() returns error from embedded python code in c++

Open azzeddinetiba opened this issue 3 years ago • 1 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [X] Consider asking first in the Gitter chat room or in a Discussion.

Problem description

Hello, I have a code that uses an embedded python code called from c++ :

    py::exec(R"(
		import my_pack
		import numpy as np
		import os
		train_data_disp = np.load("../References/discrete/train_data_disp.npy")	
                ...

with CMakeLists.txt :

cmake_minimum_required(VERSION 3.16.0)
project(prjc)

#Include libraries
include_directories(include)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
find_package(pybind11 REQUIRED)

#Add source files
file(GLOB SOURCES "src/*.cpp")

add_executable(prjc ${SOURCES})
target_link_libraries(prjc PRIVATE Eigen3::Eigen pybind11::pybind11)

Now this works perfectly in Ubuntu 20. In Windows 10, (With Python 3.8 from anaconda3, and Viual studio 2019 to compile c++ code) the code works when I :

  • included pybind11 as a subdirectory instead of find_package
  • Created the path PYTHONHOME='anacondadir/'
  • Added the link_directories("anacondadir/libs") to avoid the error [LINK : fatal error LNK1104: cannot open file 'python38.lib' with CMake finding library]

Now with CMakeLists.txt :

cmake_minimum_required(VERSION 3.16.0)
project(prjc)

#Include libraries
include_directories(include)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    find_package(pybind11 REQUIRED)
endif ()
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
    add_subdirectory(pybind11)
    link_directories("C:/mydir/anaconda3/libs")
endif ()


#Add source files
file(GLOB SOURCES "src/*.cpp")

add_executable(prjc ${SOURCES})
target_link_libraries(prjc PRIVATE Eigen3::Eigen pybind11::pybind11)

Now the problem is: Apparently (When I run the executable I only get a window with "Abort() has been called" so I can't figure out exactly the error message) I can't do np.load("../References/discrete/train_data_disp.npy") . After digging I found that

import os
os.getcwd()

returns an error and sys.executable is empty .

So I have to add

import os
os.chdir('projectdir/prjc/src/')

for the code to work. Why do I get this ? and why is it only on Windows ?

Thank you !

Reproducible example code

py::dict globals = py::globals();
py::exec(R"(
	import my_pack
	import numpy as np
	import os
        os.getcwd()
	)", globals, globals);

azzeddinetiba avatar Apr 30 '22 11:04 azzeddinetiba

How do you start the interpreter on the Python C++ side? You should provide a complete example, because this:

#include <iostream>

#include <pybind11/pybind11.h>
#include <pybind11/embed.h>

int main(){
    try {
        py::scoped_interpreter guard{};
        py::dict globals = py::globals();
        py::exec(R"(
        import os
        print(os.getcwd())
        )",
                globals, globals);
    }
    catch (py::error_already_set const& ex) {
        std::cout << ex.what() << "\n";
    }
}

works fine.

BTW, you are not linking against pybind11::embed so I guess that can be source of issues.

Holt59 avatar May 17 '22 13:05 Holt59