Python解釋器正確調用方式簡介
作者:佚名
Python解釋器在C語言中進行調用是實際編程中一個比較常用到的應用技巧。在這里我們將會針對這方面的內容進行一個詳細介紹。
如果我們想要在C語言中對Python解釋器進行調用的話,應該如何操作呢?其實這一操作步驟還是比較簡單的。我們可以通過一下給出的代碼示例來對著一操作步驟進行充分的掌握,以幫助我們在實際應用中獲得些幫助。
Python解釋器調用代碼示例:
- #include < iostream>
- #include < string>
- #include "Python.h"
- int main()
- {
- std::cout < < "Welcome to SIMPLEPYTHON!!" < < std::endl;
- std::cout < < "Chapter 17, Demo 01 - MUD Game Programming" < < std::endl;
- Py_Initialize(); // initialize python
- std::string str;
- std::getline( std::cin, str );
- while( str != "end" )
- {
- PyRun_SimpleString( const_cast< char*>( str.c_str() ) );
- std::getline( std::cin, str );
- }
- Py_Finalize(); // shut down python
- return 0;
- }
以上就是我們為大家介紹的有關Python解釋器的調用方法。
【編輯推薦】
責任編輯:曹凱
來源:
博客園