Introduction to the correct calling method of the Python Interpreter

Source: Internet
Author: User

What should we do if we want to call the Python interpreter in C? In fact, this operation is relatively simple. We can use the sample code below to fully grasp the operation steps to help us get some help in practical applications.

Python interpreter call code example:

 
 
  1. #include < iostream> 
  2. #include < string> 
  3. #include "Python.h"  
  4. int main()  
  5. {  
  6. std::cout < <  "Welcome to SIMPLEPYTHON!!" < <  std::endl;  
  7. std::cout < <  "Chapter 17, Demo 01 - MUD Game Programming" < <  std::endl;  
  8. Py_Initialize(); // initialize python  
  9. std::string str;  
  10. std::getline( std::cin, str );  
  11. while( str != "end" )  
  12. {  
  13. PyRun_SimpleString( const_cast< char*>( str.c_str() ) );  
  14. std::getline( std::cin, str );  
  15. }  
  16. Py_Finalize(); // shut down python  
  17. return 0;  

The above is the call method for the Python interpreter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.