【问题标题】:CMake Error "NumPy import failure" when compiling Boost.Numpy编译 Boost.Numpy 时出现 CMake 错误“NumPy 导入失败”
【发布时间】:2016-05-06 10:34:36
【问题描述】:

这是我按照here 所述安装的:

1. Python 3.5 (Anaconda3 2.4.3)
    Chainer 1.5.0.2
    Cython 0.23.4
    NumPy 1.10.1
    tqdm
2. OpenCV 3.0.0
3. lmdb 0.87
4. Boost 1.59.0

接下来我要编译和安装 Boost.NumPy。一开始,找不到 NumPy 模块。经过一番搜索,我在~/anaconda3/lib/python3.5/site-packages/numpy/core/include/numpy 中找到了与NumPy 相关的文件,而不是/usr/lib/usr/local/lib 等。因此,在/Boost.NumPy/CMakeList.txt 中,我添加了这一行:

set(NUMPY_INCLUDE_DIRS, /home/graphics/anaconda3/lib/python3.5/site-packages)

但仍然找不到 NumPy。当我运行 cmake -DPYTHON_LIBRARY=$HOME/anaconda3/lib/libpython3.5m.so ../ 为 Boost.NumPy 生成 makefile 时发生错误。这是错误:

graphics@gubuntu:~/usr/Boost.NumPy/build$ sudo cmake -DPYTHON_LIBRARY=$HOME/anaconda3/lib/libpython3.5m.so ../
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found PythonInterp: /usr/bin/python3.5 (found suitable version "3.5.1", minimum required is "3.5") 
-- Found PythonInterp: /usr/bin/python3.5 (found version "3.5.1") 
-- Found PythonLibs: /home/graphics/anaconda3/lib/libpython3.5m.so
CMake Error at libs/numpy/cmake/FindNumPy.cmake:61 (message):
  NumPy import failure:

  Traceback (most recent call last):

    File "<string>", line 1, in <module>

  ImportError: No module named 'numpy'

Call Stack (most recent call first):
  CMakeLists.txt:30 (find_package)


-- Configuring incomplete, errors occurred!

我尝试将NUMPY_INCLUDE_DIRS 替换为其他一些目录,但没有任何效果。我应该写什么到CMakelists.txt 告诉 cmake 在哪里可以找到 NumPy 模块并导入它?

提前致谢!


可能需要找出问题所在的其他文件:

  1. CMakeLists.txt 的 Boost.NumPy。

【问题讨论】:

  • set() 命令中不需要逗号 (,)。此外,最好在cmake 调用:cmake -DPYTHON_LIBRARY=&lt;...&gt; -DNUMPY_INCLUDE_DIRS=&lt;...&gt; &lt;source-dir&gt; 中设置变量、描述的环境,而不是修改 3d 方 CMake 脚本。
  • @Tsyvarev 谢谢,但在我这样运行后它不起作用:sudo cmake -DPYTHON_LIBRARY=$HOME/anaconda3/lib/libpython3.5m.so -DNUMPY_INCLUDE_DIRS=$HOME/anaconda3/lib/python3.5/site-packages/numpy/core/include/numpy ../。我应该写什么?
  • 变量NUMPY_INCLUDE_DIRS 为搜索头文件设置目录(在C/C++源代码中)。它不影响 python 模块搜索。您需要设置 PYTHONPATH environment 变量以使事情正常进行。

标签: python numpy cmake anaconda


【解决方案1】:

终于成功了!但我不知道为什么...:(

我做了什么:

1.  I reinstalled numpy to /usr/lib/python3.5/site-packages (previously, I installed it to ~/anaconda3/lib/python3.4/site-packages)
1.1 I also added ~/anaconda3/lib/python3.4/site-packages/numpy/include to $PYTHONPATH and $PATH
2.  I ran these commands in Python:
   >>>import numpy 
   And I found it returns no error!
3.  I removed previously compiled files in directory build, and rebuilt. Finally it worked

希望这些对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 2020-03-02
    • 2015-06-29
    • 2019-05-19
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    相关资源
    最近更新 更多