How to solve "AttributeError: module 'pygmt' has no attribute 'xyz2grd'"

Hi.
I am new to pygmt and followed the instruction on https://www.pygmt.org/latest/install.html. Here is my script for plotting my research result (i.e. Vp_model.xyzv). I want to plot velocity values (4th column in dat_grd) for each lat (1st column in dat_grd) and lon (0th column in dat_grd):

import pandas as pd
import pygmt

fig = pygmt.Figure()
region = [-105, -80, 5, 25]

with fig.subplot(
        nrows=3, ncols=2, 
        figsize=("15c", "20c"), 
        autolabel=True, 
        frame=["xa+lLongitude", "ya+lLatitude"], 
        sharex= "b", sharey="l"
        ):
    with fig.set_panel(panel = 0):
        fig.basemap(
        region = region , projection="X?", frame=['+t"Initial Model"']
    )
        dat = pd.read_fwf("../../Data/Vp_model.xyzv", index = False, header = None)
        dat_grd = dat.iloc[:, [0, 1, 4]]
        
        grd = pygmt.xyz2grd(data = dat_grd, region = region, spacing = "1d")
        fig.grdimage(
                grid = grd, 
                region = region, 
                )
    with fig.set_panel(panel = 1):
        fig.basemap(
        region = region, projection="X?", frame=['+t"TomoDD Result"']
    )

fig.show()

and I get this error:

Traceback (most recent call last):
  File "/Users/sonia/PhD/Tomography/Code/Vel/velocity_fig.py", line 23, in <module>
    grd = pygmt.xyz2grd(data = dat_grd, region = region, spacing = "1d")
AttributeError: module 'pygmt' has no attribute 'xyz2grd'

shell returned 1

I installed my python a year ago using homebrew if it is important. Also, I am using vim as my editor not jupyter.
Any help would be appreciated. Thanks.

Can you please post the output from the following?

import pygmt
pygmt.show_versions()

Thanks for your quick reply.
Here is the output:

PyGMT information:
  version: v0.4.1
System information:
  python: 3.9.9 (main, Nov 21 2021, 03:16:13)  [Clang 13.0.0 (clang-1300.0.29.3)]
  executable: /opt/homebrew/opt/python@3.9/bin/python3.9
  machine: macOS-12.1-arm64-arm-64bit
Dependency information:
  numpy: 1.22.0
  pandas: 1.3.0
  xarray: 0.19.0
  netCDF4: 1.2.9
  packaging: 21.0
  ghostscript: 9.55.0
  gmt: 6.3.0
GMT library information:
  binary dir: /opt/homebrew/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/Resources/Python.app/Contents/MacOS
  cores: 8
  grid layout: rows
  library path: /opt/homebrew/Cellar/gmt/6.3.0_1/lib/libgmt.dylib
  padding: 2
  plugin dir: /opt/homebrew/Cellar/gmt/6.3.0_1/lib/gmt/plugins
  share dir: /opt/homebrew/Cellar/gmt/6.3.0_1/share/gmt
  version: 6.3.0

The issue is that xyz2grd was released in v0.5.0 (https://www.pygmt.org/dev/changes.html#release-v0-5-0-2021-10-29), but your installation is v0.4.1. Did you install pygmt using conda or pip?

1 Like

I just installed it using Conda.
Should I install it again using pip?

No need to install it again, conda is fine. Did you create a virtual environment (e.g., conda create --name pygmt --channel conda-forge pygmt)? If so, did you activate the conda environment before running your Python script (e.g., conda activate pygmt?

yes. I both created the environment and activated it, exactly like the instruction.

This is the last step of installation:

The following NEW packages will be INSTALLED:

  pygmt              conda-forge/noarch::pygmt-0.6.0-pyhd8ed1ab_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
pygmt-0.6.0          | 191 KB    | ############################################################################################################### | 100% 
Preparing transaction: done
Verifying transaction: | WARNING conda.core.path_actions:verify(963): Unable to create environments file. Path not writable.
  environment location: /Users/sonia/.conda/environments.txt

done
Executing transaction: \ WARNING conda.core.envs_manager:register_env(52): Unable to register environment. Path not writable or missing.
  environment location: /Users/sonia/.conda/envs/pygmt
  registry file: /Users/sonia/.conda/environments.txt
done
(pygmt) ➜  ~ conda update pygmt 
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

Based on the python executable being from homebrew rather than conda, it seems likely that the issue relates to an error with the conda setup which could be related to the warnings about “Path not writable” in the last installation step. One option would be to specify a location for the environment rather than relying on the default installation environment inside ~/.conda/. For example, after navigating to a directory where you want to host the environment, you could use:

conda create --channel conda-forge --prefix ./envs pygmt
conda activate ./envs

Thanks for your suggestion. I did it and still get the warning “Unable to register environment. Path not writable or missing”.

What is the ‘conda version’ reported by running conda info?

it is

 conda version : 4.9.2

OK, two more things that you could try:

  1. Update conda (conda update conda)
  2. Create the directory to host the environment before creating the environment:
# Navigate to a folder where you want to host the environment
mkdir envs
conda create --channel conda-forge --prefix ./envs pygmt
conda activate ./envs