问题
I tried to use Homebrew to install graph-tool, but the python3 cannot find it.
brew tap homebrew/science
brew install graph-tool
It is said that the package is installed in homebrew/science/graph-tool-2.22_1
, where I only found /usr/local/Homebrew/Library/Taps/homebrew/homebrew-science/graph-tool.rb
.
When I tried to import graph-tool in python3, it shows that
from graph_tool.all import *
ImportError: No module named 'graph_tool'
I am using python3.
which python3
/usr/local/bin/python3
Is there a way I can use the graph_tool package installed in Homebrew?
Any help would be appreciated.
回答1:
Another solution that worked for me is simply creating a symlink from graph-tool to python side packages
ln -s /usr/local/Cellar/graph-tool/2.26_2/lib/python3.6/site-packages/graph_tool /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
回答2:
A quick solution.
To Check brew installed package path in the console,
brew --prefix graph-tool
Then at the beginning of the code, append the path.
import sys
sys.path.append('/usr/local/Cellar/graph-tool/2.22_1/lib/python2.7/site-packages/')
from graph_tool.all import *
回答3:
The way it supposed to work is with passing --with-python3 to brew:
brew install graph-tool --with-python3
But it causes rebuild of graph-tool and need of matplotlib, numpy and scipy from brew instead pip which is not very appealing specially as the recent stable version networkx is not yet compatible with matplotlib (fixed on master) so I'd rather to go with this:
brew install graph-tool --with-python3 --without-matplotlib --without-numpy --without-scipy
which tells the formula to not seek for brew installed matplotlib/numpy/scipy.
And for some reasons I couldn't use the formula with python2 support, which I didn't care, so I ended up with:
brew install graph-tool --without-python --with-python3 --without-matplotlib --without-numpy --without-scipy
回答4:
I was in a virtualenv when this happened to me. Make sure homebrew installs graph-tool with the same python version you want to use for your virtualenv, then recreating the virtualenv with --system-site-packages
will allow it to access system packages, including graph-tool installed through homebrew.
See: https://github.com/Homebrew/homebrew-science/issues/5741
来源:https://stackoverflow.com/questions/43521774/how-to-let-python3-import-graph-tool-installed-by-homebrew