How Python is embedded in C/D + +

Source: Internet
Author: User
If you want to embed Python in C + +, you need to add Python's include file directory and Lib file directory to the VC. Let's take a look at how to embed Python in C + +.

VC6.0, open tools->options->directories->show directories for, add the Inlude directory under the Python installation directory to the Inlude files item, Add the Libs directory to the library files item.

VC2005, open the Tools->options-> project and Solution->vc++ directory, and do the same work.

The code is as follows:

Error in debug execution, "Unable to find Python31_d.lib file", after the reason is: in debug generated must have Python31_d.lib file, otherwise can only be generated under release

#include 
 
  
   
   int main ()  {  py_initialize ();  Pyrun_simplestring ("Print ' Hi, python! '");  Py_finalize ();  return 0;  }
 
  

Py_initialize function prototype is: void Py_initialize ()

This function must be used when Python is embedded in C + + +, which initializes the Python interpreter and must be called before other PYTHON/C APIs can be used. You can use the Py_isinitialized function to determine if the initialization succeeded and return true successfully.

The Pyrun_simplestring function prototype is an int pyrun_simplestring (const char *command) used to execute a python code.

Note: Is it necessary to maintain indentation between statements?

The Py_finalize function prototype is void Py_finalize (), which closes the Python interpreter and frees up the resources that the interpreter occupies.

The Pyrun_simplefile function can be used to run the ". Py" script file, the function prototype is as follows:

int Pyrun_simplefile (FILE *fp, const char *filename);

Where FP is an open file pointer, filename is the Python script file name to run. However, because the function is officially published by Visual Studio 2003.NET, if you use a different version of the compiler, the file definition may cause a crash due to version reasons. Also, for brevity, you can replace the function with the following:

Pyrun_simplestring ("execfile (' file.py ')"); Using execfile to run Python files

Py_buildvalue () is used to convert numbers and strings into the corresponding data types in Python (all Python types are declared as Pyobject types in the C language), and the function prototypes are as follows:

Pyobject *py_buildvalue (const char *format, ...);

Pystring_string () is used to convert a variable of type pyobject* into a char* type that can be processed by the C language, with the following prototype:

char* pystring_string (Pyobject *p);

The above is an introduction to how Python is embedded in C/s + + content

  • 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.