Q&A
C++からPythonを呼び出して.pyファイルを実行する
C++でPython API(Python.h)を使用して.pyファイルを実行したいです。そして、その実行結果(戻り値)をC++の処理に使うまでが最終目標です。
ディレクトリ構成は、
./pyex/main.cpp
./pyex/python/
となっています。
python/というのは、pythonのembeddable pythonエディションをダウンロードしたものを展開し、「python」というフォルダのなかに入れたものです。例として、./pyex/python/python.exeのようになっています。
Pythonの実行環境が無い人でもソフトを実行できるように、ソフトに同梱しているembeddable Python 3.7.2を使って処理をしてほしいです。予定では、Pythonのhashlibを使います。環境では、Python.h
のはPython 3.6.8のものです。
発生している問題・エラーメッセージ
Fatal Python error: Py_Initialize: unable to load the file system codec ModuleNotFoundError: No module named 'encodings' Current thread 0x00001644 (most recent call first):
該当のソースコード
C++
1#include <Python.h> 2int main() 3{ 4 Py_SetPythonHome((wchar_t *)"python"); 5 Py_Initialize(); 6 PyRun_SimpleString("print(252)"); 7 Py_Finalize(); 8 9 return 0; 10}
試したこと
- embeddable pythonの中の
python37.zip
を展開して./pyex/python
に入れる。 Py_SetProgramName((wchar_t *)"python/python.exe");
- python3.6 embを使ってみる。
- Blenderのソースコードを読んで、ヒントがないか探す。
補足情報
Windows10 Home, Visual Studio 2017, Python 3.6.8, Python 3.7.2 embeddable edition
回答2件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
退会済みユーザー
2019/02/11 01:21
2019/02/11 01:45
2019/02/11 01:49