pybind / pybind/pybind11

[BUG]: Home .local libraries missing from sys.path with embed

Open
#4,654 2 comments 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
What version (or hash if on master) of pybind11 are you using?

tags/v2.10.4

Problem description

the standard procedure to embed python in c++ using pybind lead to missing home libraries from sys.path.

Reproducible example code

CMakeLists.txt :

cmake_minimum_required(VERSION 3.1)

include(FetchContent)

add_executable(main ./main.cpp)

FetchContent_Declare(
  pybind11
  GIT_REPOSITORY https://github.com/pybind/pybind11.git
  GIT_TAG        v2.10.4
)

FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
  FetchContent_Populate(pybind11)
  add_subdirectory(./build/_deps/pybind11-src)
endif()

target_link_libraries(main pybind11::embed)

main.cpp

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

namespace py = pybind11;

int main()
{
    py::scoped_interpreter guard{};

    py::exec("import sys\nprint(sys.path)");
}

running gives :

> cmake -B build .
> cd build
> make
> ./main 
['', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/usr/lib/python3.10/site-packages']

in normal python :

In [1]: import sys

In [2]: print(sys.path)
['/usr/bin', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '', '/home/xxx/.local/lib/python3.10/site-packages', '/usr/lib/python3.10/site-packages']
Is this a regression? Put the last known working version here if it is.

Not a regression

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

Reproduce the issue with CMakeLists.txt and main.cpp, starting at py::scoped_interpreter and the embedded Python setup. Compare the printed sys.path with normal Python behavior, then verify that the user's home .local site-packages path is included for the embedded interpreter.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.