Skip to content

Windows 10 pybind11 embed, Fatal Python error: initfsencoding: unable to load the file system codec #1930

@sejabs

Description

@sejabs

When I use PyBind11 to embed Python interpereter, I faced the error:
initfsencoding: unable to load the file system codec.

The details:
OS: Windows 10 1903 18362.356
Python 3.7.4 & Packages: Miniconda installed at C:\Users\My\Miniconda3\envs\sci_dev
MSVS: 2019 community 16.2.5
PyBind11: compiled with VS 2019 from git source with Boost, Eigen and Catch. Installed to C:\Users\My\Miniconda3\envs\sci_dev with only include and share

CMakeLists.txt

cmake_minimum_required( VERSION 3.15 )

project( "PyBind11_Test" )
enable_language(C)
enable_language(CXX)

find_package(pybind11 CONFIG REQUIRED PATHS 
    "C:/Users/My/Miniconda3/envs/sci_dev/share/cmake")`
include_directories( "C:/Users/My/Miniconda3/envs/sci_dev/include/pybind11" )
MESSAGE( [Main] " pybind11_INCLUDE_DIRS = ${pybind11_INCLUDE_DIRS}" )
MESSAGE( [Main] " pybind11_LIBRARIES = ${pybind11_LIBRARIES}" )



add_executable(PyBind11_Test main.cpp)

target_include_directories( PyBind11_Test PRIVATE "C:/Users/My/Miniconda3/envs/sci_dev/include   
        /pybind11" )                                                                    

target_link_libraries(PyBind11_Test PRIVATE pybind11::embed)

and main.cpp

#include <pybind11/embed.h>
#include <iostream>
namespace py = pybind11;
int main() {
    py::scoped_interpreter guard{};
    /*
    import sys
    print sys.path
    print "Hello,World!"
    */
    auto sys = py::module::import("sys");
    // sys.attr("path").attr("insert")(1, "C:/Users/My/Miniconda3/envs/sci_dev/Lib/site-packages");
    py::print(sys.attr("path"));
    py::print("Hello, World!"); // use the Python API

    //auto math = py::module::import("math");
    //double root_two = math.attr("sqrt")(2.0).cast<double>();

    //std::cout << "The square root of 2 is: " << root_two << "\n";

}

When VS configure is Release X64, running the build exe file in the cmd, it will report error:

Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00001628 (most recent call first):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions