【问题标题】:Cannot install nlopt python module无法安装 nlopt python 模块
【发布时间】:2020-10-23 13:14:58
【问题描述】:

我正在尝试将 nlopt 安装到 macOS 10.15.5 上。我从NLopt docs 下载了nlopt-2.6.2.tar.gz file 并从nlopt-2.6.2 目录运行以下内容:

mkdir build
cd build
cmake -DNLOPT_OCTAVE=Off -DNLOPT_MATLAB=Off -DNLOPT_GUILE=Off ..
make
sudo make install

我得到以下输出:cmake.txt

头文件 (nlopt.h) 正确安装到 /usr/local/include,动态库 (libnlopt.dylib) 正确安装到 /usr/local/lib/,但 dist-info 文件和 nlopt 模块本身都没有安装。

我也尝试过通过 pip、brew 和 conda 进行安装,但都没有奏效。我也试过从这个 Github 克隆,但也没有用。

感谢您对此提供的任何帮助,因为我完全迷路了。我对这类东西比较陌生,我在网上找不到任何好的答案。

【问题讨论】:

  • 查看您的输出,它看起来确实像 libnlopt.dylib 安装。您收到的错误消息是否有帮助?
  • @C.Nivs:感谢您的回复。你是对的,我能够在/usr/local/lib/ 下找到libnlopt.dylib,我将相应地编辑我的原始问题。不幸的是,我没有收到任何错误消息。

标签: python macos nlopt


【解决方案1】:

官方文档对使用 Python 绑定构建 nlopt 所需的确切步骤有些简洁。首先,您需要安装 SWIG:

$ brew install swig

然后,您需要numpy 才能用于目标 Python 解释器。它已经为系统 Python 预安装,否则通过 Homebrew 或pip 安装,具体取决于您的 Python 安装。

现在运行cmake:

$ cmake -DNLOPT_GUILE=OFF -DNLOPT_MATLAB=OFF -DNLOPT_OCTAVE=OFF -DNLOPT_TESTS=OFF

这将针对 MacOS 上预装的默认 Python 2.7 安装构建绑定。如果您需要针对自定义 Python 安装进行构建(例如,当您通过 Homebrew 或 https://www.python.org/downloads 的 PKG 安装程序安装 Python 3 时),请通过 PYTHON_EXECUTABLE arg 传递它:

$ cmake -DNLOPT_GUILE=OFF -DNLOPT_MATLAB=OFF -DNLOPT_OCTAVE=OFF -DNLOPT_TESTS=OFF -DPYTHON_EXECUTABLE=/usr/local/bin/python3

现在检查日志 - Python、SWIG 和 numpy 标头应该已成功定位。示例输出 sn-p(您可能打印了不同的路径/版本):

-- Found PythonInterp: /usr/local/bin/python3.8 (found version "3.8.3")
-- Found PythonLibs: /Library/Frameworks/Python.framework/Versions/3.8/lib/libpython3.8.dylib (found suitable exact version "3.8.3")
-- Found NumPy: /Users/hoefling/Library/Python/3.8/lib/python/site-packages/numpy/core/include (found version "1.19")
-- Found SWIG: /usr/local/bin/swig (found version "4.0.2")

如果其中任何一个条件不满足(例如,您看到 Could NOT find NumPyCould NOT find PythonLibsCould NOT find SWIG),则停止并确保配置成功,然后再继续下一步。

现在编译:

$ make
...
Scanning dependencies of target nlopt_python_swig_compilation
[ 96%] Swig compile nlopt.i for python
[ 96%] Built target nlopt_python_swig_compilation
Scanning dependencies of target nlopt_python
[ 98%] Building CXX object src/swig/CMakeFiles/nlopt_python.dir/CMakeFiles/nlopt_python.dir/nloptPYTHON_wrap.cxx.o
[100%] Linking CXX shared module _nlopt.so
[100%] Built target nlopt_python

安装:

$ make install
...
-- Installing: /usr/local/lib/python3.8/site-packages/nlopt.py
-- Installing: /usr/local/lib/python3.8/site-packages/_nlopt.so

测试 Python 绑定是否可导入:

$ python -c "import nlopt; print(nlopt.__version__)"
2.6.2

【讨论】:

  • 非常感谢您的回复!结果是swig 4.0.2 is already installed, it's just not linked。我现在已经链接了 swig,但 nlopt 仍然无法安装。给你一个问题:运行make install 返回一个错误,上面写着CMake Error at cmake_install.cmake:40 (file): file INSTALL cannot set permissions on "/usr/local/include/nlopt.h": Operation not permitted.,所以我正在运行sudo make install。这有什么不同吗/为什么make install 返回错误?
猜你喜欢
  • 2018-10-28
  • 2019-07-07
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 2017-05-30
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多