7

(I do find a lot of similar questions, but so far non of these fits me...)

===========Updated error message, images, Command Line===========

I am trying to #include <Python.h>(that's nearly all the code, the main function is almost empty yet) in Visual Studio, but it keeps reminding me cannot open source file "Python.h", if I run the program, it will raise an error:

fatal error C1083: Cannot open include file: 'Python.h': No such file or directory.

I added the include and library directories in project Property Pages > VC++ Directories, not working, tried to add the path to C/C++ > Additional Include Directories, not working, and I tried to change it to release mode, still not working...

VC++ C/C++

=================Update 2.0================

I add %(AdditionalIncludeDirectories); to C/C++ > Additional Include Directories but seems not to work.

Then I did something really stupid: I copied the headers and .dll to the header include folder... Now it doesn't remind me can't find Python.h any longer, I can code:

Py_Initialize();
PyRun_SimpleString("print('Hello Python!')");
Py_Finalize();

but it won't compile... I got a new error message:

'"C:\Amarth\Programing\CPlusPlusLearning\Release\CPlusPlusLearning.exe"' is not recognized as an internal or external command,
operable program or batch file.

And in Output, it's:

1>------ Build started: Project: CPlusPlusLearning, Configuration: Release Win32 ------
1>  PartOne.cpp
1>PartOne.cpp(34): warning C4244: 'argument': conversion from 'double' to 'float', possible loss of data
1>PartOne.obj : error LNK2001: unresolved external symbol __imp__Py_Finalize
1>PartOne.obj : error LNK2001: unresolved external symbol __imp__Py_Initialize
1>PartOne.obj : error LNK2001: unresolved external symbol __imp__PyRun_SimpleStringFlags
1>C:\Amarth\Computer_Graphics\Programing\CPlusPlusLearning\Release\CPlusPlusLearning.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

That seems obvious as the compiler can only find the function declaration but can't find the definition... Besides, according to the similar questions I've seen, most people solved the problem after adding the include directories. So if I'm really facing a fancy problem, will it be possible to find and copy all the function definitions then make it work in some way ?

==============OTHER MESSAGES===============

I'm using python 3.5, installed by Anaconda. The include and libs folder is under C:\Users\Amarthgul\Anaconda3, which is also added to system variable > path. There's also a Python 3.6 in my computer, but normally I only use its Manuals, and yet it haven't cause any trouble in python environment. Command Line:

/GS /GL /W3 /Gy /Zc:wchar_t /I"C:\Users\Amarthgul\Anaconda3\include" /Zi /Gm- /O2 /sdl /Fd"x64\Release\vc140.pdb" /Zc:inline /fp:precise /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\CPlusPlusLearning.pch"  
AJM
  • 1,317
  • 2
  • 15
  • 30
Amarth Gûl
  • 1,040
  • 2
  • 14
  • 33
  • To aid people answering, I suggest you write a very minimal [mcve], and also include the build line (Project properties > C++ > Command Line) – Tas Jun 14 '17 at 01:02
  • Also copy+paste the actual error message (omit any personal information like project folder etc) – Tas Jun 14 '17 at 01:03
  • 1
    I see you've added in the project directories for _Release_. Are you building in _Release_ or some other configuration? – Tas Jun 14 '17 at 01:13
  • @Tas I set `Active solution configuration` to `Release` either – Amarth Gûl Jun 14 '17 at 01:15
  • I'm guessing you've checked C:\Users\Amarthgul\Anaconda3\include to ensure there is indeed a file called python.h – Tas Jun 14 '17 at 01:18
  • @Tas surely I did, there is `Python.h` under `C:\Users\Amarthgul\Anaconda3\include` ... – Amarth Gûl Jun 14 '17 at 01:21
  • Try retyping the #include line - maybe one of the characters is not an ascii character – The Dark Jun 14 '17 at 02:27
  • @TheDark Retyped, still not working... Does my parameters seems to be fine? I found that may people fixed the problem after adding the directories... – Amarth Gûl Jun 14 '17 at 02:46
  • in `C++>Additional ...` `C:\Python34\include;%(AdditionalIncludeDirectories)` you may need add the latter part – Shihe Zhang Jun 14 '17 at 05:36
  • @ShiheZhang still neither work nor run... I'm thinking will it work if I simply copy these headers and .dll to the original include and library folder? – Amarth Gûl Jun 14 '17 at 06:04
  • Did the error message changed? – Shihe Zhang Jun 14 '17 at 07:36
  • @ShiheZhang adding `%(AdditionalIncludeDirectories);` did not change the message... – Amarth Gûl Jun 14 '17 at 08:15

2 Answers2

6

In Visual Studio Community 2015 I changed the "Active solution configuration" in Build \ Configuration Manager from 'Debug' to 'Release. That solved this problem for me.

I got my following example code from: Tutorial Python embedded in C++

#include <python.h>
#include <stdio.h>
#include <conio.h>


int main()
{
    CPyInstance pyInstance;

    PyRun_SimpleString("print('Hello World from Embedded Python!!!')");

    printf("\nPress any key to exit...\n");
    if (!_getch()) _getch();
    return 0;
}

class CPyInstance
{
    public:
    CPyInstance()
    {
        Py_Initialize();
    }

    ~CPyInstance()
    {
        Py_Finalize();
    }
};

class CPyObject
{
    private:
        PyObject* p;
    public:

        CPyObject() : p(NULL)
        { }

        CPyObject(PyObject* _p) : p(_p)
        { }


        ~CPyObject()
        {
            Release();
        }

        PyObject* getObject()
        {
            return p;
        }

        PyObject* setObject(PyObject* _p)
        {
            return (p = _p);
        }

        PyObject* AddRef()
        {
            if (p)
            {
                Py_INCREF(p);
            }
            return p;
        }   

        void Release()
        {
             if (p)
             {
                  Py_DECREF(p);
             }

             p = NULL;
        }

        PyObject* operator ->()
             {
                  return p;
             }

             bool is()
             {
                  return p? true : false;
             }


             operator PyObject* ()
             {
                  return p;
             }

             PyObject* operator = (PyObject* pp)
             {
                  p = pp;
                  return p;
             }


             operator bool()
             {
                 return p ? true : false;
             }
};
CarpeDiemKopi
  • 316
  • 3
  • 13
  • 1
    I tried this approach, also didn't work... Thanks anyway. – Amarth Gûl Jul 28 '17 at 14:06
  • Check your settings under Tools \ Options \ Python Tools \ Environment Options. – CarpeDiemKopi Jul 29 '17 at 01:02
  • Do you have a Default Environment for Python? Does it fit - 64-bit or 32-bit - to the rest of your installation? – CarpeDiemKopi Jul 29 '17 at 01:06
  • I have Python 3.5 (CPython) and in the Library Directories from the VC++ Directories configuration in my installation exists the file: libpython35.a ...\pyt – CarpeDiemKopi Jul 29 '17 at 01:25
  • Do you have this a-File in your installation too? My directory is: C:\Program Files\Python35\libs – CarpeDiemKopi Jul 29 '17 at 01:27
  • In my last but one comment the ending characters "...\pyt" where a mistake. – CarpeDiemKopi Jul 29 '17 at 01:30
  • I checked, I'm running on win64, python 3.5.2 installed by Anaconda 4.2.0 (64-bit), and the platform is CPython, seems fine... – Amarth Gûl Jul 29 '17 at 06:02
  • Is your "Library Path" setting for the Linker is correct? – CarpeDiemKopi Jul 29 '17 at 10:55
  • Check under Project Property Pages \ VC++ Directories \ Library Directories. In my case this contains: C:\Program Files\Python35\libs and there (must) - in my case of installed Python 3.5 - exist the file libpython35.a size 1045 KB. I had problems to build my example with python.h in the IDE Eclipse Oxygen CDT / PyDev with die MinGW Tools. The MinGW Compiler und Linker weren'be able to process this libpython35.a file. Because of that I changed to Visual Community 2015. – CarpeDiemKopi Jul 29 '17 at 11:12
  • In your case the next question is: With wich compiler is Anaconda3 compiled? Is it compatible with Visual Studio and your version of Visual Studio? – CarpeDiemKopi Jul 29 '17 at 12:20
  • Oh my! I checked all the configurations you mentioned and reset them, not sure which setting's change contributes to that but now it works! thank you! – Amarth Gûl Jul 29 '17 at 13:18
4

Strange, I have the same problems. But I solved it by changing the platform to 'x64'.

Sikai Yao
  • 227
  • 3
  • 10
  • Only if you are running x64 bit Python, most installations are x32 bit in which case this will not work. – Xantium Mar 11 '19 at 12:17
  • 1
    This actually solved my issue, thanks a lot. @Simon, when it comes to data-science, its the exact opposite only x64s are supported (in main libraries so people use x64 version of python) – Hossein Mar 24 '20 at 05:22