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 :
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
I changed the build to "Release x64". Because it couldnot find the debug libraries
Related
I am trying to bind my CPP function and use it in python code using pybind11. I have created a visual studio project that includes the function (does not have the main function). I cloned pybind11 from git and added it and python38 /include folders to Configuration Properties/VC++ Directories/Include Directories and python38 /libs folder to Configuration Properties/VC++ Directories/Library Directories. Also, I added the python38.lib file to Linker/input/Additional Dependencies.
I added
#include <Python.h>
#include <pybind11/pybind11.h>
to the top of my code. And the lines
And I get build errors of the form "unresolved external symbol
"LNK2001 unresolved external symbol __imp_PyThread_tss_create"
PYBIND11_MODULE(my_kmeans, handle)
{
handle.doc() = "Kmeans Clustering";
handle.def("my_kmeans", &kmeans, "kmeans function");
}
Can you please help me fix them?
I tried looking for a .lib file in pybind11 folder but I did not find one.
From a related post, one installed package matplotlib-cpp through microsoft /
vcpkg . However, when running a simple example,
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
plt::plot({1,3,2,4});
plt::show();
}
an error returned stating that
Error C1083 Cannot open include file: 'Python.h': No such file or directory Examples
..\vcpkg\installed\x64-windows\include\matplotlibcpp.h 5
I have tried the methods in:
can't include Python.h in visual studio
psycopg: Python.h: No such file or directory and Cygwin gcc issue - cannot find Python.h (which didn't apply since vs managed python environment itself, and there wasn't python-dev option)
A very similiar post C++: matplotlibcpp.h and Python.h linker error also indicated the same issue in Ubuntu environment.
In matplotlibcpp.h file, it mentioned that
// Python headers must be included before any system headers, since
// they define _POSIX_C_SOURCE
#include <Python.h>
What did it mean? How to link python.h and a matplotlibcpp.h in Visual Studio 2019?
Updates:
I was able to locate "Python.h" and "numpy/arrayobject.h" in MS python folder. However, when I manually added those three ".h" files into the project header folder, the issue wasn't resolved. Especially, when I manually fixed the locations in "matplotlibcpp.h", the "Python.h" in ""numpy/arrayobject.h" still could not be located.
I am trying to use matplotlib-cpp for a C++ project compiled by Visual Studio using CMake.
The python distribution I use is Anaconda2 (latest 2.7 version downloaded today). I removed every other python distribution that was on my computer.
I added the paths to the Anaconda2 folder to the system AND user environment variables. (C:\Anaconda2...)
CMake is finding it correctly as when configuring the project with CMake, I have:
Found PythonInterp: C:/Anaconda2/python.exe (found suitable version "2.7.16", minimum required is "2.7")
Found PythonLibs: C:/Anaconda2/libs/python27.lib (found suitable version "2.7.16", minimum required is "2.7")
The project builds correctly with VS (no build or link error) but when I run a quick hello world, I have an error :
Hello World!
ImportError: No module named site
This is the main.cpp:
#include <iostream>
using namespace std;
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main(int argc, char **argv)
{
cout << "Hello World!" << endl;
plt::plot({ 1,3,2,4 });
plt::show();
return 0;
}
This is the CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(PLT)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
# main app
add_executable(
plt
src/main.cpp
)
# Matplotlib
find_package(PythonInterp 2.7 REQUIRED)
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(plt ${PYTHON_LIBRARIES})
I read on this forum that this might be caused by some Path issues, but the path to python directories is set:
I am using Microsoft Visual Studio 2017 to build with CMake's "Visual Studio 15 2017 Win64" generator and I run my program through "cmd" and I think the problem is configuring the default python used by "cmd".
I precise that I tried matplotlib on Anaconda Python's prompt and it is working.
Any idea on how to fix this?
This usually occurs when the PYTHONHOME path is invalid or not set
try:
set PYTHONHOME=C:\Python27
I basically did the same as the person here:
Building/including Boost.Python in VS2013
However, I used an empty cpp file with only the main function and the inclusion of <boost/python.hpp>
#include <boost/python.hpp>
int main() {
return 0;
}
Now I get the strange linker error (in Visual Studio):
1>LINK : fatal error LNK1104: cannot open file 'boost_python-vc140-mt-gd-1_60.lib'
Which is strange, because I have the lib file I think, however, it is called:
libboost_python3-vc140-mt-gd-1_60.lib
You need to configure your Visual C++ project setting.
The following case operates well.
[debug platform mode] x64
[include directory] (..\;;);C:\boost\boost_1_60_0\;C:\Python35\include\; # add your actual boost and python directory path
[library directory] (..\;;);C:\Python35\libs\;C:\boost\boost_1_60_0\stage\lib; # add your actual boost and python library path
My english is very poor, so I appreciate if you try to understand my question.
I have Python3.3 , Boost 1.50.0 and visual studio 2008 installed, and all of them work fine.
Now I want to embed python 2.7.6 in C++ by using Boost Python library,so I downloaded Portable Python 2.7.6.1 ( from portablepython.com/).
And I downloaded Boost lib. 1.50.0 again, extract them into another path, compiled them by following the building tutorial.
Everything seem alright, but when I tried to compile my code, I got a linker error.
Error LNK1104 cannot open file 'python32.lib'
I have no idea why it was not 'python27.lib'
I am confused, because I never install Python3.2, and I have modified user-config.jam to make sure that all paths has been redirect to my Portable Python(2.7) folders,and the new Boost library were successfully built without error.
I have been trying to solve the problem for a few weeks, using lots of ways , but it still doesn't work
All the things I have done are :
Download Boost C++ library (boost_1_50_0.7z from www.boost.org/users/history/version_1_50_0.html)
Extract them into my boost root path(D:\Proj\ThirdPartyLib\boost_1_50_0)
Download Portable Python 2.7.6.1 ( from portablepython.com/wiki/PortablePython2.7.6.1/ )
Extract them into D:\Py27Test
Modify user-config.jam, add the following text and save it into D:\Proj\ThirdPartyLib\boost_1_50_0\
using python : 2.7 : "D:\Py27Test\App" : "D:\Py27Test\App\include" : "D:\Py27Test\App\libs" : ;
Execute Visual Studio Command Prompt (x86)
cd /D D:\Proj\ThirdPartyLib\boost_1_50_0
bootstrap
bjam --toolset=msvc-9.0 --python=2.7 --user-config=D:\Proj\ThirdPartyLib\boost_1_50_0\user-config.jam --debug-configuration
the debug infomations are:
notice: [python-cfg] Configuring python...
notice: [python-cfg] user-specified version: "2.7"
notice: [python-cfg] Checking interpreter command "python"...
notice: [python-cfg] running command 'DIR /-C /A:S "D:\Py27Test\App\python.exe" 2>&1'
notice: [python-cfg] running command '"python" -c "from sys import *; print('version=%d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%s' % (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))" 2>&1'
notice: [python-cfg] ...requested configuration matched!
notice: [python-cfg] Details of this Python configuration:
notice: [python-cfg] interpreter command: "python"
notice: [python-cfg] include path: "D:\Py27Test\App\include"
notice: [python-cfg] library path: "D:\Py27Test\App\libs"
notice: [python-cfg] DLL search path: "D:\Py27Test\App"
Execute Visual Studio 2008
Add D:\Proj\ThirdPartyLib\boost_1_50_0 to compiler include paths
Add D:\Proj\ThirdPartyLib\boost_1_50_0\stage\lib to linker library paths
create a testing code:
#define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp>
#include <iostream>
int main(int argc, char** argv) {
return 0;
}
Complie the code and I would get a linker error (Error LNK1104 cannot open file 'python32.lib')
Did I miss something?
Or does anyone has any idea to solve this problem?
Thanks in advance
Finally, I solved this problem.
Add D:\Py27Test\App\include to compiler include paths
Add D:\Py27Test\App\libs to linker library paths
Seems like if I don't include Python.h, it will link to a default library file.
So after I added those paths, it works now.