問題詳情
我正在嘗試在我的 C++ dll 庫中使用嵌入式 python。 該庫是使用 GCC 編譯器、CMake 和 Ninja 在 MSYS2 中構(gòu)建和編譯的。 Python 3.10 也使用 pacman 安裝在 MSYS2 上。 Windows 10 env 在路徑中包含 C:\msys64\mingw64\bin (python 也位于此處)。 Python 不安裝在 Windows 上,僅安裝在 MSYS2 上。
cmake_minimum_required(VERSION 3.26) project(python_test) set(CMAKE_CXX_STANDARD 17) find_package(Python REQUIRED Development) add_executable(python_test main.cpp) target_link_libraries(python_test PRIVATE Python::Python)
簡(jiǎn)單測(cè)試代碼:
int main() { Py_Initialize(); PyRun_SimpleString("from time import time,ctime\n" "import numpy as np\n" "print('Today is', ctime(time()))\n"); if (Py_FinalizeEx() < 0) exit(120); return 0; }
當(dāng)我運(yùn)行此代碼時(shí),我收到此錯(cuò)誤:
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 = [ 'D:\\a\\msys64\\mingw64\\lib\\python310.zip', 'D:\\a\\msys64\\mingw64\\lib\\python3.10', 'D:\\a\\msys64\\mingw64\\lib\\lib-dynload', '', ] 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>
但是 python 命令在 MSYS2/MinGW 控制臺(tái)和 Windows cmd 中都可以工作。 當(dāng)我在 Windows 10 環(huán)境中將 PYTHONHOME 指定為 C:\msys64\mingw64\bin 時(shí),我在 Clion 中再次遇到相同的錯(cuò)誤,現(xiàn)在在 Windows 命令行中也是如此。 如何解決這個(gè)問題?
解決方法
比較簡(jiǎn)單,我設(shè)置 PYTHONHOME=C:\msys64\mingw64\bin 和更重要的 PYTHONPATH=C:\msys64\mingw64\lib\python3.10;C:\msys64\mingw64\lib\python3.10\site-packages;C:\ msys64\mingw64\lib\lib-dynload
就把這個(gè)問題解決了文章來源:http://www.zghlxwxcb.cn/article/391.html
文章來源地址http://www.zghlxwxcb.cn/article/391.html
到此這篇關(guān)于MSYS2 嵌入Python發(fā)生報(bào)錯(cuò) No module named encodings,沒有名為“encodings”的模塊的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!