C and Python are mixed. c calls functions in the python module.

Source: Internet
Author: User

Tool ---> option ---> projectc and solutions ---> VC ++ Directories
Path of the include file that is added to the python installation directory in include files
Path of the libs folder where libraray files is added to the python installation directory

Go to the include directory of Python and find pyconfig.
Ifdef _ debug
# Pragma comment (Lib, "python27_d.lib ")
# Else
# Pragma comment (Lib, "python27.lib ")
# Endif/* _ debug */
Change python27_d.lib to python27.lib.
And save it.

# Include <python. h> using namespace STD; void main !! // System ("test. PY "); // initialize the python environment. In order to later use the C apipy_initialize () provided by Python; pyobject * pname, * pmodule, * pfunc, * pargs, * pvalue; If (! Py_isinitialized () {printf ("initialized error! \ N ") ;}// the simplest Python c api // pyrun_simplestring (" Print 'Hello world' "); // obtain the name of the Python script, reference this module. At this time, the main function of this module is executed !! Pname = pystring_fromstring ("hello"); pmodule = pyimport_import (pname); If (! Pmodule) {printf ("can not open the script! \ N ") ;}// find the defined function pfunc = pyobject_getattrstring (pmodule," hello ") in the referenced Python module "); // Method for assigning values to function parameters/* pargs = pytuple_new (1); pvalue = pystring_fromstring ("10000"); pytuple_setitem (pargs, 0, pvalue ); * // The second method for assigning values to function parameters: pargs = py_buildvalue ("(I)", 10); // call this function. The first parameter is the returned function, the second parameter is the function parameter list pyobject_callobject (pfunc, pargs); // pyobject_callobject (pfunc, null); // clear the python environment py_finalize (); // The problem encountered during this write process is: at the beginning, the C ++ project name was test_proj. ECT, // The Python script name is test. so when I reference the module, I mistakenly use other files. // I guess this is related to searching for files in the Python application module. After I change the name of the script, I can use it all, it took nearly two hours. It was very sweaty!
}

import osimport sysdef hello(a):    #print "hello world!"    print "execute the func"+str(a)    os.mkdir("test")   # system("mkdir test")

print "hello world"print "+++++++++++++++++++"

The above is hello. py

Some basic usage of Objective C API

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.