Ta-lib インストール解決法【MacOS Catalina】

【問題点】

pip3 install Ta-lib を実行すると以下のようなエラーが出ました。

WARNING: The directory '/Users/hogehoge/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/Users/hogehoge/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Ta-lib
  Downloading https://files.pythonhosted.org/packages/90/05/d4c6a778d7a7de0be366bc4a850b4ffaeac2abad927f95fa8ba6f355a082/TA-Lib-0.4.17.tar.gz (717kB)
     |████████████████████████████████| 727kB 1.4MB/s 
Requirement already satisfied: numpy in /usr/local/lib/python3.7/site-packages (from Ta-lib) (1.15.1)
Building wheels for collected packages: Ta-lib
  Building wheel for Ta-lib (setup.py) ... error
  ERROR: Complete output from command /usr/local/opt/python/bin/python3.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/tmp/pip-install-hj6txy4c/Ta-lib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/tmp/pip-wheel-qimpa1sv --python-tag cp37:
  ERROR: /private/tmp/pip-install-hj6txy4c/Ta-lib/setup.py:79: UserWarning: Cannot find ta-lib library, installation may fail.
    warnings.warn('Cannot find ta-lib library, installation may fail.')
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.14-x86_64-3.7
  creating build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/abstract.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/test_abstract.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/deprecated.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/stream.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/test_pandas.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/test_data.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/test_func.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  copying talib/test_stream.py -> build/lib.macosx-10.14-x86_64-3.7/talib
  running build_ext
  building 'talib._ta_lib' extension
  creating build/temp.macosx-10.14-x86_64-3.7
  creating build/temp.macosx-10.14-x86_64-3.7/talib
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/lib/python3.7/site-packages/numpy/core/include -I/usr/include -I/usr/local/include -I/opt/include -I/opt/local/include -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c talib/_ta_lib.c -o build/temp.macosx-10.14-x86_64-3.7/talib/_ta_lib.o
  talib/_ta_lib.c:526:10: fatal error: 'ta-lib/ta_defs.h' file not found
  #include "ta-lib/ta_defs.h"
           ^~~~~~~~~~~~~~~~~~
  1 error generated.
  error: command 'clang' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for Ta-lib
  Running setup.py clean for Ta-lib
Failed to build Ta-lib
Installing collected packages: Ta-lib
  Running setup.py install for Ta-lib ... error
    ERROR: Complete output from command /usr/local/opt/python/bin/python3.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/tmp/pip-install-hj6txy4c/Ta-lib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/tmp/pip-record-1gz1p7rd/install-record.txt --single-version-externally-managed --compile:
    ERROR: /private/tmp/pip-install-hj6txy4c/Ta-lib/setup.py:79: UserWarning: Cannot find ta-lib library, installation may fail.
      warnings.warn('Cannot find ta-lib library, installation may fail.')
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.14-x86_64-3.7
    creating build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/abstract.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/test_abstract.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/deprecated.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/stream.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/test_pandas.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/test_data.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/test_func.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    copying talib/test_stream.py -> build/lib.macosx-10.14-x86_64-3.7/talib
    running build_ext
    building 'talib._ta_lib' extension
    creating build/temp.macosx-10.14-x86_64-3.7
    creating build/temp.macosx-10.14-x86_64-3.7/talib
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -I/usr/local/lib/python3.7/site-packages/numpy/core/include -I/usr/include -I/usr/local/include -I/opt/include -I/opt/local/include -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c talib/_ta_lib.c -o build/temp.macosx-10.14-x86_64-3.7/talib/_ta_lib.o
    talib/_ta_lib.c:526:10: fatal error: 'ta-lib/ta_defs.h' file not found
    #include "ta-lib/ta_defs.h"
             ^~~~~~~~~~~~~~~~~~
    1 error generated.
    error: command 'clang' failed with exit status 1
    ----------------------------------------
ERROR: Command "/usr/local/opt/python/bin/python3.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/tmp/pip-install-hj6txy4c/Ta-lib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/tmp/pip-record-1gz1p7rd/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-install-hj6txy4c/Ta-lib/

【解決方法】



まずは、homebrewTa-lib をインストールします。

brew install TA-Lib

以下のようなメッセージが出て、正常にインストールが成長します。

==> Downloading https://homebrew.bintray.com/bottles/ta-lib-0.4.0.catalina.bottle.1.tar.gz

しかし、この時点で python3import TA-Lib とやってもエラーがおきます。

ここから更に、pip3 でインストールしてやる必要があります。

sudo pip3 install Ta-lib

すると、以下のようにインストールがうまくいきます。

WARNING: The directory '/Users/hogehoge/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/Users/hogehoge/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Ta-lib
  Downloading https://files.pythonhosted.org/packages/90/05/d4c6a778d7a7de0be366bc4a850b4ffaeac2abad927f95fa8ba6f355a082/TA-Lib-0.4.17.tar.gz (717kB)
     |████████████████████████████████| 727kB 834kB/s 
Requirement already satisfied: numpy in /usr/local/lib/python3.7/site-packages (from Ta-lib) (1.15.1)
Building wheels for collected packages: Ta-lib
  Building wheel for Ta-lib (setup.py) ... done
  Stored in directory: /Users/hogehoge/Library/Caches/pip/wheels/2a/2e/ec/71c565b2e0091e03a2b56abfbfd062f14a01a8d7b20ffe8bd5
Successfully built Ta-lib
Installing collected packages: Ta-lib
Successfully installed Ta-lib-0.4.17

まとめ

macTA-Lib のインストールがうまくいかない時は、homebrew でインストールし、そのあとで pip3TA-Lib をインストールしましょう。