0

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):
sejabs
  • 43
  • 5

1 Answers1

1

The fatal error is resolved by adding an environment variable "PYTHONPATH" and "PATHONHOME " which point to the installation location of Python, according to Py_Initialize fails - unable to load the file system codec,

PATHONPATH="C:\Users\My\Miniconda3\envs\sci_dev\DLLs;C:\Users\My\Miniconda3\envs\sci_dev\Lib;C:\Users\My\Miniconda3\envs\sci_dev\Lib\site-packages"

PATHONHOME="C:\Users\CRRCHZ\Miniconda3\envs\sci_dev"

Finally the .exe output expect result!

sejabs
  • 43
  • 5
  • 1
    The obvious problem with this is that the path is now hardcoded, so changing conda environement has no effect... Right? If so, this is not the correct answer. – helmesjo Nov 26 '19 at 13:40