Getting pygmt running in Jupyter

Hi all,

I’m just having some trouble importing pygmt in jupyter. The pygmt kernel is running just fine - the issue is as follows:

Note that I can import pygmt in anaconda prompt just fine… Can anyone point me towards a solution here?

Thanks in advance!

Tom

Hi Tom, what’s the output of pygmt.show_versions()?

Here it is in Anaconda Prompt. Appreciate any help!

>>> pygmt.show_versions()
PyGMT information:
  version: v0.2.1
System information:
  python: 3.8.6 | packaged by conda-forge | (default, Jan 25 2021, 22:54:47) [MSC v.1916 64 bit (AMD64)]
  executable: C:\Users\taschaap\Anaconda3\envs\pygmt\python.exe
  machine: Windows-10-10.0.19041-SP0
Dependency information:
  numpy: 1.20.0
  pandas: 1.2.2
  xarray: 0.16.2
  netCDF4: 1.5.5.1
  packaging: 20.9
  ghostscript: 9.53.3
  gmt: 6.1.1
GMT library information:
  binary dir: C:/Users/taschaap/Anaconda3/envs/pygmt
  cores: 4
  grid layout: rows
  library path: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt.dll
  padding: 2
  plugin dir: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt_plugins
  share dir: c:/programs/gmt5/share
  version: 6.1.1

Ah, I didn’t see you were on Windows. Pinging @seisman or @weiji14 who may have more insight than me on this.

  library path: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt.dll
  plugin dir: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt_plugins
  share dir: c:/programs/gmt5/share
  version: 6.1.1

This is something that doesn’t make sense. GMT is in your pygmt environment, but the sharedir is c:/programs/gmt5/share. Do you have GMT5 installed? It seems in your Jupyter notebook, pygmt find the GMT5 library instead of the GMT6 library.

1 Like

It would seem I do have GMT5 sitting there in its own “programs” directory (weirdly not Program Files). I’m not sure but I might have installed that a long time ago so it could have been sitting there messing with things. Should I uninstall GMT5? Or is there some way of redirecting the share directory to GMT6?

It would seem I do have GMT5 sitting there in its own “programs” directory (weirdly not Program Files).

The GMT installer installs GMT to C:/programs/gmt5 or C:/programs/gmt6, not in C:/Programs Files.

Or is there some way of redirecting the share directory to GMT6?

See Installing — PyGMT. I think you can change your environmental variable GMT_LIBRARY_PATH to C:\Users\taschaap\Anaconda3\envs\pygmt\Library\bin\.

Yes

No, they are different.

Okay, so I have uninstalled GMT5 and installed GMT6, and set the GMT_LIBRARY_PATH as directed. Now on show_versions() I get:

`GMT library information:
  binary dir: C:/Users/taschaap/Anaconda3/envs/pygmt
  cores: 4
  grid layout: rows
  library path: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt.dll
  padding: 2
  plugin dir: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt_plugins
  share dir: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/share/gmt
  version: 6.1.1`

That looks alright to me…

Now on importing pygmt in a notebook:

---------------------------------------------------------------------------
GMTCLibNotFoundError                      Traceback (most recent call last)
<ipython-input-3-f58669fae37f> in <module>
----> 1 import pygmt

~\Anaconda3\envs\pygmt\lib\site-packages\pygmt\__init__.py in <module>
     30 
     31 # Start our global modern mode session
---> 32 _begin()
     33 # Tell Python to run _end when shutting down
     34 _atexit.register(_end)

~\Anaconda3\envs\pygmt\lib\site-packages\pygmt\session_management.py in begin()
     14     """
     15     prefix = "pygmt-session"
---> 16     with Session() as lib:
     17         lib.call_module("begin", prefix)
     18         # pygmt relies on GMT modern mode with GMT_COMPATIBILITY at version 6

~\Anaconda3\envs\pygmt\lib\site-packages\pygmt\clib\session.py in __enter__(self)
    181 
    182         """
--> 183         self.create("pygmt-session")
    184         # Need to store the version info because 'get_default' won't work after
    185         # the session is destroyed.

~\Anaconda3\envs\pygmt\lib\site-packages\pygmt\clib\session.py in create(self, name)
    331             pass
    332 
--> 333         c_create_session = self.get_libgmt_func(
    334             "GMT_Create_Session",
    335             argtypes=[ctp.c_char_p, ctp.c_uint, ctp.c_uint, ctp.c_void_p],

~\Anaconda3\envs\pygmt\lib\site-packages\pygmt\clib\session.py in get_libgmt_func(self, name, argtypes, restype)
    282         """
    283         if not hasattr(self, "_libgmt"):
--> 284             self._libgmt = load_libgmt()
    285         function = getattr(self._libgmt, name)
    286         if argtypes is not None:

~\Anaconda3\envs\pygmt\lib\site-packages\pygmt\clib\loading.py in load_libgmt()
     44             error = err
     45     if error:
---> 46         raise GMTCLibNotFoundError(
     47             "Error loading the GMT shared library '{}':".format(
     48                 ", ".join(lib_fullnames)

GMTCLibNotFoundError: Error loading the GMT shared library 'gmt.dll, gmt_w64.dll, gmt_w32.dll':

So, there’s still a shared library problem. Can anyone help me identify the problem here?

Tom

Could you try the following codes in Jupyter:

import ctypes
ctypes.CDLL("C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt.dll")

or

import ctypes
ctypes.CDLL("C:\Users\taschaap\Anaconda3\envs\pygmt\Library\bin\gmt.dll")

and see what the error messages are.

No error message, just output:

<CDLL 'C:\Users\taschaap\Anaconda3\envs\pygmt\Library\bin\gmt.dll', handle 7ff8d46d0000 at 0x1d9d5e1e4f0>

Wow, so without changing anything after that step, import pygmt suddenly works in jupyter. I honestly haven’t changed a thing. Perhaps after the previous steps the kernel hadn’t updated. It looks to be working now.

Thank you all very much!

Okay that was premature - I must have misunderstood what ctypes does, because pygmt will only successfully import when I precede it with:

import ctypes
ctypes.CDLL(r"C:\Users\taschaap\Anaconda3\envs\pygmt\Library\bin\gmt.dll")
import pygmt

What has ctypes done to make pygmt work?

And another follow-up (sorry for the post spam) - pygmt seems happy to run functions right up until I want to plot something:

In: fig.show()
Out: ....
     GMTCLibError: Module 'psconvert' failed with status code 78:
     psconvert [ERROR]: Cannot execute Ghostscript (gswin64c).

Okay that was premature - I must have misunderstood what ctypes does, because pygmt will only successfully import when I precede it with:

import ctypes
ctypes.CDLL(r"C:\Users\taschaap\Anaconda3\envs\pygmt\Library\bin\gmt.dll")
import pygmt

What has ctypes done to make pygmt work?

pygmt calls ctypes.CDLL to load the shared library. If ctypes.CDLL can load the gmt library, pygmt should also be able to do that. As your pygmt now works, could your post the output of pygmt.show_versions()?

GMT or PyGMT needs Ghostscript (gswin64c) to convert PS format to other image formats. Perhaps you don’t have Ghostscript installed? You should already have installed it in your pygmt environment, but it’s unclear to me why GMT can’t find it.

Maybe you can download the Ghostscript installer from https://www.ghostscript.com?

Sorry, I don’t have a Windows machine, so can’t test it.

As your pygmt now works, could your post the output of pygmt.show_versions() ?

PyGMT information:
  version: v0.2.1
System information:
  python: 3.8.6 | packaged by conda-forge | (default, Jan 25 2021, 22:54:47) [MSC v.1916 64 bit (AMD64)]
  executable: C:\Users\taschaap\Anaconda3\envs\pygmt\python.exe
  machine: Windows-10-10.0.19041-SP0
Dependency information:
  numpy: 1.20.0
  pandas: 1.2.2
  xarray: 0.16.2
  netCDF4: 1.5.5.1
  packaging: 20.9
  ghostscript: None
  gmt: None
GMT library information:
  binary dir: C:/Users/taschaap/Anaconda3/envs/pygmt
  cores: 4
  grid layout: rows
  library path: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt.dll
  padding: 2
  plugin dir: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/gmt_plugins
  share dir: C:/Users/taschaap/Anaconda3/envs/pygmt/Library/share/gmt
  version: 6.1.1

You should already have installed it in your pygmt environment

Just to be clear, if I try to install Ghostscript now should I install it to the directory of my pygmt environment, or just keep to the default installation directory?

  ghostscript: None
  gmt: None

I think it means Ghostscript and GMT are not in your PATH, but GMT is in your C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/ directory.

It’s unclear to me what Anaconda or Jupyter does on Windows. Maybe you can try to add C:/Users/taschaap/Anaconda3/envs/pygmt/Library/bin/ to your PATH. It may work, but it’s not a good solution.

Well, I have installed ghostscript with the default installation settings and pygmt is now (apparently) operational! I have even plotted a map to prove it. I am hoping that will be the end of this saga, and I thank you all for your help.

1 Like

Great to hear! Hopefully this post will help other Windows users.