Import vtk with brew python

In the situation using python on homebrew

$ brew install --python --qt vtk

Then

import vtk
# => Fatal Python error: PyThreadState_Get: no current thread. Abort trap: 6

Will be.

This is as mentioned in the homebrew-science issue OSX in * .dylib and * .so that comes down with brew install This is because the Python Path on the system side is embedded.

Try otool -L (Xcode Command Line Tools are required to use otool and install_name_tool)

$ otool -L /usr/local/Cellar/vtk/7.0.0_3/lib/libvtkCommonCorePython27D-7.0.dylib
/usr/local/Cellar/vtk/7.0.0_3/lib/libvtkCommonCorePython27D-7.0.dylib:
	/usr/local/opt/vtk/lib/libvtkCommonCorePython27D-7.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/local/Cellar/vtk/7.0.0_3/lib/libvtkWrappingPython27Core-7.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/local/Cellar/vtk/7.0.0_3/lib/libvtkCommonCore-7.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.10)
	/usr/local/Cellar/vtk/7.0.0_3/lib/libvtksys-7.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

As you can see, the link to /System/Library/Frameworks/Python.framework/Versions/2.7/Python is embedded. To fix this, use install_name_tool

$ sudo install_name_tool -change /System/Library/Frameworks/Python.framework/Versions/2.7/Python /usr/local/Cellar/python/2.7.11/Frameworks/Python /usr/local/Cellar/vtk/7.0.0_3/lib/libvtkCommonCorePython27D-7.0.dylib

Just run. (Sudo is required for rewriting) When I check it,

$ otool -L /usr/local/Cellar/vtk/7.0.0_3/lib/libvtkCommonCorePython27D-7.0.dylib
/usr/local/Cellar/vtk/7.0.0_3/lib/libvtkCommonCorePython27D-7.0.dylib:
	/usr/local/opt/vtk/lib/libvtkCommonCorePython27D-7.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/local/Cellar/vtk/7.0.0_3/lib/libvtkWrappingPython27Core-7.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/local/Cellar/vtk/7.0.0_3/lib/libvtkCommonCore-7.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/local/Cellar/python/2.7.11/Frameworks/Python (compatibility version 2.7.0, current version 2.7.10)
	/usr/local/Cellar/vtk/7.0.0_3/lib/libvtksys-7.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

You can see that the link can be modified to the path of brew python. Not this one needs to be changed, but __ / usr / local / lib / python2.7 / site-in addition to __ / usr / local / Cellar / vtk / {version} /lib/*.dylib__ Since packages / vtk / vtk * .so__ also needs to be modified, I wrote the following script.

vtk_fix_pythonpath.sh


#!/bin/sh

SystemPythonPath=/System/Library/Frameworks/Python.framework/Versions/2.7/Python #Change according to the version of python you are using
BrewPythonPath=/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Python #Change according to the version of python you are using

function replace_system_python() {
    echo " -- before -- "
    otool -L $1
    install_name_tool -change ${SystemPythonPath} ${BrewPythonPath} $1
    echo " -- after -- "
    otool -L $1
}

for file in `find /usr/local/lib/python2.7/site-packages/vtk/vtk*.so`;do
    replace_system_python ${file}
done

for file in `find /usr/local/Cellar/vtk/7.0.0_3/lib/*.dylib`;do
    replace_system_python ${file}
done

It's bad manners to run install_name_tool -change on all dylib, so files (even those that don't include the Python path), but it shouldn't be a problem as it won't be replaced unless it matches.

$ sudo ./vtk_fix_pythonpath.sh

After that, if you check if you can import,

import vtk
# => No error!!!!!!

I was able to import without problems. If you read the home-science issue, it seems that some people have written a patch that can build vtk without embedding links, but do you still want to fix the main body side?

Recommended Posts

Import vtk with brew python
Import tsv with Python
Working with LibreOffice in Python: import
Statistics with python
Python module import
Python with Go
Twilio with Python
Integrate with Python
Import python script
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Summarize Python import
Cast with python
Python> Run with run-time arguments> Use import argparse
Investigation when import cannot be done with python
Download and import files with Splunk external python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Serial communication with python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Python from or import
python original module import
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python