pybind / pybind/pybind11

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

Open
#3,918 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
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);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducible py::exec snippet and the Windows CMakeLists.txt configuration, then reproduce the os.getcwd() and sys.executable behavior using Python 3.8 and Anaconda. Compare the Windows embedded-interpreter setup with the working Ubuntu setup; done means the platform-specific cause is identified and the required configuration or a confirmed pybind11 defect is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp, numpy, python
Domain
operating-systems, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.