Problem with psconvert on pyGMT

Hey there, I’m trying to get pygmt to run on my pc, either through a Jupyter notebook or python command prompt, but had the same problem both times. It’s something to do with a psconvert error, but I have no idea how to read it and even less how to fix it. I’m just trying the simplest of codes, which is the example code in the pygmt website

fig = pygmt.Figure()
fig.coast(shorelines=True, region=[-90, -70, 0, 20], projection="M15c", frame=True)
fig.show()

It gives me this error

pygmt.exceptions.GMTCLibError: Module 'psconvert' failed with status code 79:
psconvert [ERROR]: System call [@gswin64c -q -dNOSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -DPSL_no_pagefill -dMaxBitmap=2147483647 -dUseFastColor=true "C:/Users/fedes/.gmt/sessions/gmt_session.11152/gmt_1.ps-" 2> "C:/Users/fedes/.gmt/sessions/gmt_session.11152/psconvert_6264c.bb"] returned error 1.

My only suspicion is that it might have something to do with a previous install of either ghostscript or gmt that I had on my pc and didn’t properly get rid of, but I’m not sure and can’t find anything like it.

Just for reference here’s what pygmt.show_versions() gives me

PyGMT information:
  version: v0.4.1
System information:
  python: 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]
  executable: C:\Users\fedes\AppData\Local\Programs\Python\Python39\python.exe
  machine: Windows-10-10.0.19041-SP0
Dependency information:
  numpy: 1.20.3
  pandas: 1.3.2
  xarray: 0.19.0
  netCDF4: 1.5.7
  packaging: 21.0
  ghostscript: 9.54.0
  gmt: 6.2.0
GMT library information:
  binary dir: C:/Users/fedes/AppData/Local/Programs/Python/Python39
  cores: 8
  grid layout: rows
  library path: C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/gmt_w64.dll
  padding: 2
  plugin dir: C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/gmt_plugins
  share dir: C:/Users/fedes/AppData/Local/Programs/Python/Python39/share
  version: 6.2.0

Thank you very much for your time!

I just typed your code, I noticed that is missing the next command at beginning:

import pygmt

fig = pygmt.Figure()
fig.coast(shorelines=True, region=[-90, -70, 0, 20], projection=“M15c”, frame=True)
fig.show()

and it works!

Thanks for the answer. I’m actually importing the module, just didn’t realize to add that line to the question. I also expected it to run for anyone with a working gmt and pygmt, it’s most definitely something about my installation process that is causing this. I guess the quickest solve would be to uninstall everything, python included, but I wanted to avoid that if I could.

import pygmt
fig = pygmt.Figure()
fig.coast(shorelines=True, region=[-90, -70, 0, 20], projection="M15c", frame=True)
pygmt.config(GMT_VERBOSE="d")
fig.show()

Can you run this command and post the verbose output? The pygmt.config(GMT_VERBOSE="d") setting will let the fig.show() call output more debugging messages, which may help us find why it doesn’t work for you.

Thanks, just tried it. This is what I got:

>>> pygmt.config(GMT_VERBOSE="d")
gmtset [DEBUG]: gmtlib_get_graphics_item: Fig: 1 Subplot: 2 Panel: () Inset: 0
<pygmt.src.config.config object at 0x000001DD3A933A30>
>>> fig.show()
pygmt-session [DEBUG]: Exit:  gmt_reload_settings
pygmt-session [DEBUG]: Enter: gmtlib_plot_C_format
pygmt-session [DEBUG]: Exit:  gmtlib_plot_C_format
pygmt-session [DEBUG]: Enter: gmtinit_get_history
pygmt-session [DEBUG]: gmtlib_get_graphics_item: Fig: 1 Subplot: 2 Panel: () Inset: 0
pygmt-session [DEBUG]: Enter: gmt_hash_init
pygmt-session [DEBUG]: Exit:  gmt_hash_init
Exception ignored on calling ctypes callback function: <function Session.create.<locals>.print_func at 0x000001DD3A95A820>
Traceback (most recent call last):
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\clib\session.py", line 351, in print_func
    message = message.decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 78: invalid continuation byte
pygmt-session [DEBUG]: Exit:  gmtinit_get_history
pygmt-session [DEBUG]: GMT_Create_Session initialized GMT structure
pygmt-session [DEBUG]: Shared Library # 0 (core). Path = C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/gmt_w64.dll
pygmt-session [DEBUG]: Loading GMT plugins from: C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/gmt_plugins
pygmt-session [DEBUG]: Shared Library # 1 (supplements). Path = C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/gmt_plugins/supplements_w64.dll
pygmt-session [DEBUG]: GMT now running in modern mode [Session ID = 5228]
pygmt-session [DEBUG]: GMT_Call_Command string: figure a4c0c76d972e42c7b3a0bc7955073eae -
pygmt-session [DEBUG]: Revised options: a4c0c76d972e42c7b3a0bc7955073eae -
pygmt-session [DEBUG]: Exit:  gmt_reload_settings
pygmt-session [DEBUG]: Enter: gmtlib_plot_C_format
pygmt-session [DEBUG]: Exit:  gmtlib_plot_C_format
pygmt-session [DEBUG]: Enter: gmtinit_get_history
pygmt-session [DEBUG]: gmtlib_get_graphics_item: Fig: 1 Subplot: 2 Panel: () Inset: 0
pygmt-session [DEBUG]: Enter: gmt_hash_init
pygmt-session [DEBUG]: Exit:  gmt_hash_init
Exception ignored on calling ctypes callback function: <function Session.create.<locals>.print_func at 0x000001DD3A95A940>
Traceback (most recent call last):
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\clib\session.py", line 351, in print_func
    message = message.decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 78: invalid continuation byte
pygmt-session [DEBUG]: Exit:  gmtinit_get_history
pygmt-session [DEBUG]: GMT_Create_Session initialized GMT structure
pygmt-session [DEBUG]: Shared Library # 0 (core). Path = C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/gmt_w64.dll
pygmt-session [DEBUG]: Loading GMT plugins from: C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/gmt_plugins
pygmt-session [DEBUG]: Shared Library # 1 (supplements). Path = C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/gmt_plugins/supplements_w64.dll
pygmt-session [DEBUG]: GMT now running in modern mode [Session ID = 5228]
pygmt-session [DEBUG]: GMT_Call_Command string: psconvert -A -E300 -F"C:\Users\fedes\AppData\Local\Temp\a4c0c76d972e42c7b3a0bc7955073eae-preview-cg6e2ha4\a4c0c76d972e42c7b3a0bc7955073eae" -Tf
pygmt-session [DEBUG]: Revised options: -A -E300 -FC:\Users\fedes\AppData\Local\Temp\a4c0c76d972e42c7b3a0bc7955073eae-preview-cg6e2ha4\a4c0c76d972e42c7b3a0bc7955073eae -Tf
psconvert [DEBUG]: Ghostscript not found in registry. Fallback to PATH.
psconvert [DEBUG]: gmt_check_executable: Pass to popen: [gswin64c --version 2> NUL]
El sistema no puede encontrar la ruta especificada.
psconvert [DEBUG]: gswin64c --version 2> NUL was successful
psconvert [DEBUG]: Ghostscript version: 9.54.0
psconvert [DEBUG]: Use PS filename C:/Users/fedes/.gmt/sessions/gmt_session.5228/gmt_1.ps-
psconvert [DEBUG]: Hidden PS file C:/Users/fedes/.gmt/sessions/gmt_session.5228/gmt_1.ps- found
psconvert [DEBUG]: Complete partial PS file C:/Users/fedes/.gmt/sessions/gmt_session.5228/gmt_1.ps-
psconvert [DEBUG]: Size of half-baked PS file = 73379.
psconvert [DEBUG]: Fattened up PS file C:/Users/fedes/.gmt/sessions/gmt_session.5228/gmt_1.ps-
psconvert [INFORMATION]: Processing C:/Users/fedes/.gmt/sessions/gmt_session.5228/gmt_1.ps-...
psconvert [INFORMATION]: Find HiResBoundingBox ...
psconvert [DEBUG]: Running: @gswin64c -q -dNOSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -DPSL_no_pagefill -dMaxBitmap=2147483647 -dUseFastColor=true "C:/Users/fedes/.gmt/sessions/gmt_session.5228/gmt_1.ps-" 2> "C:/Users/fedes/.gmt/sessions/gmt_session.5228/psconvert_6256c.bb"
El sistema no puede encontrar la ruta especificada.
psconvert [ERROR]: System call [@gswin64c -q -dNOSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -DPSL_no_pagefill -dMaxBitmap=2147483647 -dUseFastColor=true "C:/Users/fedes/.gmt/sessions/gmt_session.5228/gmt_1.ps-" 2> "C:/Users/fedes/.gmt/sessions/gmt_session.5228/psconvert_6256c.bb"] returned error 1.
psconvert [DEBUG]: Delete C:/Users/fedes/.gmt/sessions/gmt_session.5228/psconvert_6256c.bb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\figure.py", line 331, in show
    pdf = self._preview(fmt="pdf", dpi=dpi, anti_alias=False, as_bytes=False)
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\figure.py", line 390, in _preview
    self.savefig(fname, dpi=dpi, **kwargs)
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\figure.py", line 263, in savefig
    self.psconvert(prefix=prefix, fmt=fmt, crop=crop, **kwargs)
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\helpers\decorators.py", line 582, in new_module
    return module_func(*args, **kwargs)
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\helpers\decorators.py", line 725, in new_module
    return module_func(*args, **kwargs)
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\figure.py", line 198, in psconvert
    lib.call_module("psconvert", build_arg_string(kwargs))
  File "C:\Users\fedes\AppData\Local\Programs\Python\Python39\lib\site-packages\pygmt\clib\session.py", line 505, in call_module
    raise GMTCLibError(
pygmt.exceptions.GMTCLibError: Module 'psconvert' failed with status code 79:
psconvert [ERROR]: System call [@gswin64c -q -dNOSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -DPSL_no_pagefill -dMaxBitmap=2147483647 -dUseFastColor=true "C:/Users/fedes/.gmt/sessions/gmt_session.5228/gmt_1.ps-" 2> "C:/Users/fedes/.gmt/sessions/gmt_session.5228/psconvert_6256c.bb"] returned error 1.

Just in case, “El sistema no puede encontrar la ruta especificada.” translates to The system cannot find the path specified.

It seems that GMT cannot find the command gswin64c, but you already have Ghostscript installed.

Can you check if the gswin64c.exe binary file exists in your C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/ directory?

I do in fact have it in that folder, and it works when running it there

Open a CMD, type echo %PATH% and see if you have the C:/Users/fedes/AppData/Local/Programs/Python/Python39/bin/ path in your PATH variable.

I do, and calling programs through cmd that are in that folder works perfectly too

Isn’t it strange? Shouldn’t it be gmt_1.ps instead of gmt_1.ps-?
makes think the PS is not finished.

Try run gmt clear sessions in CMD

Thanks for the help, running that just gives me this error

C:\Users\fedes>gmt clear sessions
El sistema no puede encontrar la ruta especificada.
clear [ERROR]: Failed to remove session directory C:/Users/fedes/.gmt/sessions [error = 1]

I guess there’s no session to be cleared, and C:/Users/fedes/.gmt/ is empty too.

You should not get that error either. Running gmt clear sessions on an empty .gmt is a perfectly valid move.
When the py code errors does your .gmt is still empty? No .gmt\sessions\...?
What is the gmt version?

It’s still empty indeed, no folder is created when I run the code. When I run gmt it says the version is 6.2.0.

Could it be some sort of permission problem, as if gmt wouldn’t have permission to make sessions in that folder? I would not know how to allow it (or, for that matter, change the folder either).

By the way, I tried a completely clean install of everything, this time in an anaconda environment, and keep getting the exact same error. All folders have changed except for this temp folder, so maybe that has something to do?

Thanks a lot

Small update: I realized that python does indeed create a sessions folder, but it’s gone as soon as I close the terminal. If I don’t close it and run gmt clear sessions on another cmd, it works just fine. And as you said, one of the files there has extension .ps-

Can you open CMD and run where gswin64c and gswin64c --version?

I’m now working on an anaconda environment, and everything seems to be in the right place

(env1) C:\Users\fedes>where gswin64c
C:\Users\fedes\anaconda3\envs\env1\Library\bin\gswin64c.exe

(env1) C:\Users\fedes>gmt --version
6.2.0

Everything looks normal to me. I have no idea why it doesn’t work for you.

Thanks for the tons of help. I began to suspect it might be an environment variable that’s set incorrectly, but I don’t even know which variables even exist for gmt and gs, and where I should set them to.

I’ll go cry for a bit, maybe come back to it after that.

Try running a pure GMT command like gmt basemap -R0/1/0/1 -JX1 -Baf -Vi -png map (and provide the output). Will be easier to troubleshoot, and allow us to see if this is a GMT problem or PyGMT problem.

This reminds me of a previous issue at https://github.com/GenericMappingTools/pygmt/issues/829 which was closed because we couldn’t reproduce it reliably.

It actually doesn’t work either, here’s the output:

(cin2020) C:\Users\fedes>gmt basemap -R0/1/0/1 -JX1 -Baf -Vi -png map
begin [INFORMATION]: Creating a workflow directory C:/Users/fedes/.gmt/sessions/gmt_session.5956
basemap [INFORMATION]: Constructing the basemap
basemap [INFORMATION]: Auto-frame interval for x-axis (item 0): a0.2f0.1
basemap [INFORMATION]: Auto-frame interval for y-axis (item 0): a0.2f0.1
basemap [INFORMATION]: Map scale is 0.001 km per cm or 1:100.
end [INFORMATION]: Process GMT figure queue: 1 figures found
end [INFORMATION]: Processing GMT figure #0 [map png ]
El sistema no puede encontrar la ruta especificada.
psconvert [INFORMATION]: Processing C:/Users/fedes/.gmt/sessions/gmt_session.5956/gmt_0.ps-...
psconvert [INFORMATION]: Find HiResBoundingBox ...
El sistema no puede encontrar la ruta especificada.
psconvert [ERROR]: System call [@"C:/Users/fedes/anaconda3/envs/cin2020/Library/bin/gswin64c.exe" -q -dNOSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -DPSL_no_pagefill -dMaxBitmap=2147483647 -dUseFastColor=true "C:/Users/fedes/.gmt/sessions/gmt_session.5956/gmt_0.ps-" 2> "C:/Users/fedes/.gmt/sessions/gmt_session.5956/psconvert_7176c.bb"] returned error 1.
end [ERROR]: Failed to call psconvert
end [ERROR]: gmtinit_process_figures returned error 79
end [INFORMATION]: Destroying the current workflow directory C:/Users/fedes/.gmt/sessions/gmt_session.5956
basemap [ERROR]: Unable to call module end for a one-liner plot.

It keeps telling me it can’t find the specified path :confused: