簡體   English   中英

無法安裝 nlopt python 模塊

[英]Cannot install nlopt python module

我正在嘗試將 nlopt 安裝到 macOS 10.15.5 上。 我從NLopt 文檔下載了nlopt-2.6.2.tar.gz 文件,並從 nlopt-2.6.2 目錄運行以下內容:

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

我得到了以下 output: cmake.txt

header 文件 ( nlopt.h ) 正確安裝到/usr/local/include並且動態庫 ( libnlopt.dylib ) 正確安裝到/usr/local/lib/ ,但是 dist-info 文件和 nlopt 模塊本身都沒有安裝.

我還嘗試通過 pip、brew 和 conda 進行安裝,但都沒有成功。 我也試過從這個 Github 克隆,它也不起作用。

我很感激這方面的任何幫助,因為我完全迷路了。 我對這種東西比較陌生,我在網上找不到任何好的答案。

官方文檔對使用 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 安裝構建綁定。 If you need to build against custom Python installation (eg when you've installed Python 3 via Homebrew or PKG installer from https://www.python.org/downloads ), pass it via the 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標頭應該已成功定位。 示例 output 片段(您可能打印了不同的路徑/版本):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM