Pygmt not running in Jupyter on MAC

Hi all,

I’m unsuccessfully trying to import pygmt on a Jupyter Notebook on macOS Monterey 12.5.

However, pygmt works if I launch python through my terminal and I get the following:

`~ python
Python 3.9.10 (main, Aug 23 2022, 09:58:58)
[Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

import pygmt
pygmt.show_versions()
PyGMT information:
version: v0.7.0
System information:
python: 3.9.10 (main, Aug 23 2022, 09:58:58) [Clang 13.1.6 (clang-1316.0.21.2.5)]
executable: /Users/roberta/.pyenv/versions/3.9.10/bin/python
machine: macOS-12.5-arm64-arm-64bit
Dependency information:
numpy: 1.23.2
pandas: 1.5.0
xarray: 2022.9.0
netCDF4: 1.6.1
packaging: 21.3
geopandas: 0.11.1
ghostscript: 10.00.0
gmt: 6.4.0
GMT library information:
binary dir: /Users/roberta/.pyenv/versions/3.9.10/bin
cores: 10
grid layout: rows
library path: /opt/homebrew/Cellar/gmt/6.4.0_1/lib/libgmt.dylib
padding: 2
plugin dir: /opt/homebrew/Cellar/gmt/6.4.0_1/lib/gmt/plugins
share dir: /opt/homebrew/Cellar/gmt/6.4.0_1/share/gmt
version: 6.4.0`

Most of my python modules were installed within pyenv, but homebrew installs GMT in a different location. I’ve tried to remove and reinstall GMT with homebrew but it gets installed in the same place by default. Would you have any tips on how to fix this?

I’ve included the following env variable to my .zshrc but it’s not helping:

#Path to GMT shared lib
export GMT_LIBRARY_PATH="/opt/homebrew/Cellar/gmt/6.4.0_1/lib"

I’ve tried this on Jupyter but it also doesn’t help

import ctypes
ctypes.CDLL("/opt/homebrew/Cellar/gmt/6.4.0_1/lib/libgmt.dylib")

thank you in advance for your help and time!
Cheers,
R.

Hello again,

I copied some of the /opt/homebrew/Cellar/gmt/6.4.0_1 info into the /Users/roberta/.pyenv/versions/3.9.10/lib/python3.9/site-packages/ and updated my env variable to:

export GMT_LIBRARY_PATH="/Users/roberta/.pyenv/versions/3.9.10/lib/python3.9/site-packages/pygmt/lib/bin"

now pygmt.show_versions() looks like this:

PyGMT information:
  version: v0.7.0
System information:
  python: 3.9.10 (main, Aug 23 2022, 09:58:58)  [Clang 13.1.6 (clang-1316.0.21.2.5)]
  executable: /Users/roberta/.pyenv/versions/3.9.10/bin/python
  machine: macOS-12.5-arm64-arm-64bit
Dependency information:
  numpy: 1.23.2
  pandas: 1.5.0
  xarray: 2022.9.0
  netCDF4: 1.6.1
  packaging: 21.3

  geopandas: 0.11.1
  ghostscript: 10.00.0
  gmt: 6.4.0
GMT library information:
  binary dir: /Users/roberta/.pyenv/versions/3.9.10/bin
  cores: 10
  grid layout: rows
  library path: /Users/roberta/.pyenv/versions/3.9.10/lib/python3.9/site-packages/pygmt/lib/bin/libgmt.dylib
  padding: 2
  plugin dir: /Users/roberta/.pyenv/versions/3.9.10/lib/python3.9/site-packages/pygmt/lib/bin/gmt/plugins
  share dir: /opt/homebrew/Cellar/gmt/6.4.0_1/share/gmt
  version: 6.4.0

Hello,

In order to run pygmt on Mac, you can follow some steps:

  1. At terminal create a director by mkdir evn for example
  2. Call evn by cd evn
  3. Install pygmt by pip install pygmt
  4. After that install jupyter by pip install jupyter-lab
  5. Turn off terminal and run again by cd env; conda activate pygmt ; jupyter-lab

Hi Roberta,

You did not mention how you installed the Jupyter software. It is not easy to get different Python installations to work together, especially when using Jupyter. I found it is easier to install everything for PyGMT using Anaconda including a version of GMT, even though I have a separate GMT that I use from the command line. This way all the components are in the came “conda” environment.

Hi everyone,

thank you for your feedback. I don’t use conda, I’ve installed python through pyenv similarly to what have below.

brew install pyenv
pyenv install 3.8.11
pyenv global 3.8.11

My pygmt is in here
/Users/roberta/.pyenv/versions/3.9.10/lib/python3.9/site-packages/pygmt/

but GMT is here
/opt/homebrew/Cellar/gmt/6.4.0_1/share/gmt

Hi there,

I’ve managed to fix this.

I’ve updated my OS, and uninstalled and reinstalled Brew and Xcode. I’ve installed python without pyenv, so I’ve everything installed with brew. As from below:

PyGMT information:
  version: v0.7.0
System information:
  python: 3.10.8 (main, Oct 13 2022, 09:48:40) [Clang 14.0.0 (clang-1400.0.29.102)]
  executable: /opt/homebrew/opt/python@3.10/bin/python3.10
  machine: macOS-12.6-arm64-arm-64bit
Dependency information:
  numpy: 1.23.4
  pandas: 1.5.0
  xarray: 2022.10.0
  netCDF4: 1.6.1
  packaging: 21.3
  geopandas: None
  ghostscript: 10.00.0
  gmt: 6.4.0
GMT library information:
  binary dir: /opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/Resources/Python.app/Contents/MacOS
  cores: 10
  grid layout: rows
  library path: /opt/homebrew/Cellar/gmt/6.4.0_1/lib/libgmt.dylib
  padding: 2
  plugin dir: /opt/homebrew/Cellar/gmt/6.4.0_1/lib/gmt/plugins
  share dir: /opt/homebrew/Cellar/gmt/6.4.0_1/share/gmt
  version: 6.4.0

I’m on Apple Silica here, but I didn’t have this problem before with Intel.

And what’s the problem ? You have everything in the same place no?