python - Matplotlib-cpp : ImportError: No module named site

标签 python c++ matplotlib cmake

我正在尝试使用 matplotlib-cpp对于由 Visual Studio 使用 CMake 编译的 C++ 项目。

我使用的 python 发行版是 Anaconda2(今天下载了最新的 2.7 版本)。我删除了计算机上的所有其他 Python 发行版。

我将 Anaconda2 文件夹的路径添加到系统和用户环境变量中。 (C:\Anaconda2...)

CMake 正确地找到了它,因为在使用 CMake 配置项目时,我有:

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") 

该项目使用 VS 正确构建(没有构建或链接错误)但是当我快速运行 hello world 时,出现错误:

Hello World!
ImportError: No module named site

这是 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;
}

这是 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})

我在这个论坛上看到这可能是由一些路径问题引起的,但是设置了python目录的路径:

enter image description here

我正在使用 Microsoft Visual Studio 2017 通过 CMake 的“Visual Studio 15 2017 Win64”生成器进行构建,我通过“cmd”运行我的程序,我认为问题在于配置“cmd”使用的默认 python。

我准确地说,我在 Anaconda Python 的提示符下尝试了 matplotlib,它正在运行。

知道如何解决这个问题吗?

最佳答案

这通常发生在 PYTHONHOME 路径无效或未设置时 尝试:

set PYTHONHOME=C:\Python27

关于python - Matplotlib-cpp : ImportError: No module named site,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831751/

相关文章:

python - 根据分组和条件更新数据框列

c++ - Collision c++ 2d 游戏,防止玩家进入瓷砖

C++ 性能问题

python - 如何注释 seaborn pairplots

python - 生成所有可能的有序字符串排列

python - 用于集成测试的斜纹布替代品

python - python中按距离排序点的奇怪结果

c++ - C 结构中的段错误。使用 Matlab Coder 从 Matlab->C 翻译的代码

python - Matplotlib:如何 "cut"颜色条不需要的部分?

python - 如何在 hexbin 图上绘制等高线?