编辑

编辑

化学 Stack Exchange我问了一个关于 Open Babel 和 Python 一起使用的问题。

问题在于我得到的答案(由 Geoff Hutchinson 提供)要求 Python 能够导入模块 Pybel。

我通过两种方式在这台电脑上安装了 Open Babel,APT 和源。当我意识到 pybel 不适用于 Python 时,我删除了 APT 安装,并决定使用 Python 绑定从源安装。

为此我遵循本指南最终命令被定制为(从 运行~/build

cmake ../openbabel-2.3.2 -DBUILD_GUI=ON -DPYTHON_BINDINGS=ON

输出结果为:

-- Using included inchi library.
-- Found wxWidgets: TRUE  
-- Cairo found. PNG output will be supported.
-- Attempting to build the GUI
--    wxWidgets found => GUI will be built
CMake Warning (dev) at test/CMakeLists.txt:171 (include):
  Syntax Warning in cmake code at

    /home/fusion809/Downloads/openbabel-2.3.2/cmake/modules/UsePythonTest.cmake:54:14

  Argument not separated from preceding token by whitespace.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at test/CMakeLists.txt:171 (include):
  Syntax Warning in cmake code at

    /home/fusion809/Downloads/openbabel-2.3.2/cmake/modules/UsePythonTest.cmake:54:31

  Argument not separated from preceding token by whitespace.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at test/CMakeLists.txt:171 (include):
  Syntax Warning in cmake code at

    /home/fusion809/Downloads/openbabel-2.3.2/cmake/modules/UsePythonTest.cmake:57:25

  Argument not separated from preceding token by whitespace.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) at test/CMakeLists.txt:171 (include):
  Syntax Warning in cmake code at

    /home/fusion809/Downloads/openbabel-2.3.2/cmake/modules/UsePythonTest.cmake:57:39

  Argument not separated from preceding token by whitespace.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Python bindings will be compiled
-- Could NOT find Ruby (missing:  RUBY_INCLUDE_DIR RUBY_LIBRARY RUBY_CONFIG_INCLUDE_DIR) (found version "2.1.0")
-- Ruby library files NOT found. Ruby bindings will NOT be compiled.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fusion809/build

但是,我应该提到,我第一次编译 Open Babel 时忘记将-DBUILD_GUI&-DPYTHON_BINDINGS命令添加到 cmake 行,因此我不得不在最初编译软件后运行这个新的 cmake 命令。这有什么区别吗?我应该删除 Open Babel 并重新编译吗?如果是这样,我是否必须删除目录中的某些文件/usr/(如果是,请提及它们,因为我不知道是哪些)?如果相关,我使用的是 32 位 15.04。

编辑

我删除了构建目录的内容并重新启动,然后在 cmake 命令之后运行:

make
sudo make install
export PYTHONPATH=/usr/local/lib:$PYTHONPATH

我收到的输出最后是以下两行:

-- Up-to-date: /usr/local/lib/openbabel.py
-- Up-to-date: /usr/local/lib/pybel.py

在 Python 终端中,我运行了import openbabelimport pybel给出了输出: openbabel 或 pybelImportError: No module named ...在哪里...,取决于执行了哪个命令,所以我怀疑这是我的 Python 安装端的问题。

答案1

没有python-openbabel

% python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import openbabel
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named openbabel
>>>

% python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pybel
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pybel
>>>

安装python-openbabel

sudo apt-get install python-openbabel

查看:

% python  
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import openbabel
>>>

% python                     
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pybel
>>>

答案2

我遇到了同样的问题,并发现我使用的 Python 发行版 (Anaconda) 不是原始版本 (Ubuntu 提供的版本)。以下是一种可能的解决方法 (如果是这种情况)。

使用 apt-get 安装 python-openbabel 后,使用系统提供的 python 检查它(在我的情况下/usr/bin/python)。您应该能够导入 openbabel:

% /usr/bin/python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import openbabel
>>>

我尝试使用easy_installAnaconda 提供的安装 openbabel 和 pybel,但没有成功。然后我将*openbabel**pybel*文件复制/usr/lib/python2.7/dist-packages/到一个文件夹中PYTHONPATH,它工作正常。如果有人有更好(更干净)的方法让 Anaconda 使用为系统提供的 python 安装的包,我将不胜感激。

(我想对答案进行评论,但由于声誉不佳,我无法评论)

相关内容