MSYS2 and Embedding Python. No module named 'encodings'

I'm trying to use embedded python in my C++ dll library. The library is built and compiled in MSYS2 using GCC compiler, CMake and Ninja. Python 3.10 is also installed on MSYS2 using pacman. Windows 10 env contains C:\msys64\mingw64\bin in Path (python is also located there). Python doesn't installed on Windows, only on MSYS2.

This is what CMakeLists.txt contains:

cmake_minimum_required(VERSION 3.26)


find_package(Python REQUIRED Development)
add_executable(python_test main.cpp)
target_link_libraries(python_test PRIVATE Python::Python)

Simple test code:

int main() {
    PyRun_SimpleString("from time import time,ctime\n"
                       "import numpy as np\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0)
    return 0;

When I run this code I got this error:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'C:\\Users\\someUsername\\CLionProjects\\python_test\\cmake-build-debug\\python_test.exe'
  sys.base_prefix = 'D:\\a\\msys64\\mingw64'
  sys.base_exec_prefix = 'D:\\a\\msys64\\mingw64'
  sys.platlibdir = 'lib'
  sys.executable = 'C:\\Users\\someUsername\\CLionProjects\\python_test\\cmake-build-debug\\python_test.exe'
  sys.prefix = 'D:\\a\\msys64\\mingw64'
  sys.exec_prefix = 'D:\\a\\msys64\\mingw64'
  sys.path = [
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00004564 (most recent call first):
  <no Python frame>

But python command work both in MSYS2/MinGW console and Windows cmd. When I specify PYTHONHOME to C:\msys64\mingw64\bin in Windows 10 environment, I have got the same error again in Clion, and now in Windows command line too. How to resolve this problem?


  • I set PYTHONHOME=C:\msys64\mingw64\bin and more important PYTHONPATH=C:\msys64\mingw64\lib\python3.10;C:\msys64\mingw64\lib\python3.10\site-packages;C:\msys64\mingw64\lib\lib-dynload and it solved my problem