简体   繁体   中英

python from c++ in visual studio

I used the visual studio installer to install python and could run the basic python program. Now I want to make a new c++ console program and call the python script from there.

When googling I saw I need to include the "Python.h" in my c++ function. I tried to include and i get the message "cannot open the source file". Is it because I used visual studio to install the python. And most of googling have installed python separately.

Older versions of visual studio, just installed python interpretter. So installed Visual studio 2019 and had a option to select Python3

With that option, it created a new directory, C:\Python27amd64\

Now I created a new console application: Code:

#include <iostream>
#include "C:\Python27amd64\include\Python.h"

    int main()
    {
        Py_Initialize();
        PyRun_SimpleString("from time import time,ctime\n"
         "print('Today is',ctime(time()))\n");
        //std::cout << "Hello World!\n";
    }

And also followed the following steps:

  1. Properties > C/C++ > General > Additional Include Directories. I added the "C:\Python27amd64\include" 2.Properties > Linker > General > Additional Library Directories. : I added C:\Python27amd64\libs 3.Properties > Linker > Input > Additional Dependencies: I added python27.lib
  2. I changed the build to "Release x64". Because it couldnot find the debug libraries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM