How do I install openbabel for Python 3.6 in Windows 10?

3.8k views Asked by At

I am using Python for making a script to work with some chemical structures in my PhD. I want to install openbabel libraries for python. I tried my best but I could not install it. It always gives error: Error: SWIG failed. Is Open Babel installed? Here is the Python Path on my PC: C:\Users\malih\AppData\Local\Programs\Python\Python36-32. Openbabel is installed at the following path C:\Program Files (x86)\OpenBabel-2.3.1.

When I run python -m pip install openbabel, it gives the following error:

C:\Users\malih>python -m pip install openbabel
Collecting openbabel
Using cached openbabel-2.4.1.tar.gz
Installing collected packages: openbabel
Running setup.py install for openbabel ... error
Complete output from command C:\Users\malih\AppData\Local\Programs\Python\Python36-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\malih\\AppData\\Local\\Temp\\pip-build-glr82a7x\\openbabel\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\malih\AppData\Local\Temp\pip-02z3p_a9-record\install-record.txt --single-version-externally-managed --compile:
running install
running build_ext
Warning: pkg-config could not be found.
Guessing Open Babel location:
- include_dirs: ['C:\\Users\\malih\\AppData\\Local\\Programs\\Python\\Python36-32\\include', 'C:\\Users\\malih\\AppData\\Local\\Programs\\Python\\Python36-32\\include', '/usr/local/include/openbabel-2.0']
- library_dirs: ['C:\\Users\\malih\\AppData\\Local\\Programs\\Python\\Python36-32\\libs', 'C:\\Users\\malih\\AppData\\Local\\Programs\\Python\\Python36-32\\PCbuild\\win32', '/usr/local/lib']
building '_openbabel' extension
swigging openbabel-python.i to openbabel-python_wrap.cpp
swig.exe -python -c++ -small -O -templatereduce -naturalvar -IC:\Users\malih\AppData\Local\Programs\Python\Python36-32\include -IC:\Users\malih\AppData\Local\Programs\Python\Python36-32\include -I/usr/local/include/openbabel-2.0 -o openbabel-python_wrap.cpp openbabel-python.i

Error: SWIG failed. Is Open Babel installed?
You may need to manually specify the location of Open Babel include and library directories. For example:
  python setup.py build_ext -I/usr/local/include/openbabel-2.0 -L/usr/local/lib
  python setup.py install

----------------------------------------
Command "C:\Users\malih\AppData\Local\Programs\Python\Python36-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\malih\\AppData\\Local\\Temp\\pip-build-glr82a7x\\openbabel\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\malih\AppData\Local\Temp\pip-02z3p_a9-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\malih\AppData\Local\Temp\pip-build-glr82a7x\openbabel\

I tried to follow the instructions on this page but they are purely for linux and I could not follow up them for Windows: How do I use python-openbabel in Travis CI?

I also tried to install pkg-config from the following guide but even after that, it gives the same error: How to install pkg config in windows?

Please help me on this as my work is based on this and I am stuck at this point. Thanks in advance for your time.

2

There are 2 answers

2
pandamakes On

pip install is notoriously non-windows friendly. Either go to openbabel website and download + install windows binary, then download + install python binding, or use the unoffical official openbabel python windows build, then cd into the downloaded directory, and perform pip install OPENBABEL_FILE_WINDOWS.whl

I recently had do some quite a bit of work on 2D mol file -> 3D mol file translation using openbabel. Let me know how it works out for you.

--

edit: I should also add, python3.x is breakingly different to python2.x. So if anything breaks, try it with python2.x

0
mimminou On

This is somewhat old, but the problems still persist so i'll answer nonetheless.

The precompiled Windows binary (most likely) doesn't come with SWIG built-in, which in turn doesn't offer language bindings and gives you the error when installing from pip. A workaround to this is to install a wheel from the unofficial Windows binaries website just like pandamakes answered.

However, the wheels already comes bundled with a precompiled obabel, so you really don't need to install the windows binaries version (the one that comes with the setup and all), but you have to know that if you're willing to ship a python script that uses this library, know that it will interfere with systems that do have BABEL_DIR in the environment variables, to fix this you have to include the whole openbabel directory from YourPythonDirectory/lib/site-packages/. in your script's folder.

Another important note is that some babel builds from the unofficial binaries website have a bug where they do not detect all file formats, especially the most common ones like sdf/pdb/mol2 or even smiles. The only versions i have confirmed working somewhat flawlessly so far are the openbabel‑3.1.1‑cp36‑cp36m‑win32.whl and openbabel‑3.1.1‑cp38‑cp38‑win32.whl.