# How to fix 'pygmt-session \[ERROR\]: Not available in classic mode'

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573>\
**Category:** PyGMT Q&A\
**Created:** [January 8, 2024, 2:13pm UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573 "2024-01-08T14:13:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 8, 2024, 2:13pm UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/1 "2024-01-08T14:13:48Z")

</div>

Hello everyone!

I encountered some difficulties while using PyGMT.

When I use ‘fig = gmt.Figure()’ in a function, an error is returned:

‘pygmt-session [ERROR]: Not available in classic mode’

I tried solution given by:[https://github.com/GenericMappingTools/pygmt/issues/217](https://github.com/GenericMappingTools/pygmt/issues/217)

After I added ‘reload(pygmt)’, a new error comes out:

‘free(): invalid next size (fast)’ or 'double free or corruption (out)

How to fix this problem? I hope some of you could help me. Thanks in advance 😀

---

<div class="post-metadata">

**Author:** ![weiji14](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/weiji14/32/33_2.png) [@weiji14](https://forum.generic-mapping-tools.org/u/weiji14)\
**Post date:** [January 8, 2024, 10:00pm UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/2 "2024-01-08T22:00:11Z")

</div>

Hi @sherfy, thanks for reporting the issue. Could you provide the output of:

```auto
import pygmt

pygmt.show_versions()

```

Just want to see what version of GMT/PyGMT you have installed.

> [@sherfy](#):
>
> ‘free(): invalid next size (fast)’ or 'double free or corruption (out)

Is this error happening on `reload(pygmt)`, or another line in your Python script? If it is another function (e.g. `fig.plot()`), could you turn on debugging mode using e.g. `fig.plot(..., verbose=True)`, and post the output from that? That would make it easier to know where the issue is.

---

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 9, 2024, 4:26am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/3 "2024-01-09T04:26:30Z")

</div>

Thank you for responding to my question.

The version of my PyGMT is: 6.4.0. And my full error is:

> pygmt-session [ERROR]: Not available in classic mode  
> Module ‘figure’ failed with status code 30

I tried to add ‘verbose=True’, and here is my output:

> plot [INFORMATION]: Processing input table data  
> plot [INFORMATION]: Central meridian not given, default to 127.95  
> plot [INFORMATION]: Map scale is 7.79236 km per cm or 1:779236.  
> plot [INFORMATION]: Reading Data Table from Input memory location via vector  
> double free or corruption (out)  
> Aborted (core dumped)

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [January 9, 2024, 7:37am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/4 "2024-01-09T07:37:45Z")

</div>

Open a terminal, run the command `gmt clear sessions`, and then try your Python script again.

---

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 9, 2024, 7:47am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/5 "2024-01-09T07:47:05Z")

</div>

I tried it and it only worked once and then I got the same error again.

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [January 9, 2024, 8:00am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/6 "2024-01-09T08:00:09Z")

</div>

OK, now I know the reason. Since your Python script crashes, the GMT session is not correctly ended. Could you please try run `gmt clear sessions` again, and then run the following commands in your Python interpreter:

```auto
import pygmt

pygmt.show_versions()

```

---

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 9, 2024, 8:05am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/7 "2024-01-09T08:05:46Z")

</div>

ok, the output is:

> version: 6.4.0

I just tried:

> sudo apt install libtcmalloc-minimal4  
> export LD\_PRELOAD=“/usr/lib/x86\_64-linux-gnu/libtcmalloc\_minimal.so.4”

After I used this method, the problem was solved. But I still don’t understand why this problem occurs.

---

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 9, 2024, 9:18am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/8 "2024-01-09T09:18:10Z")

</div>

> [@seisman](#):
>
> ```auto
> import pygmt
> 
> pygmt.show_versions()
> 
> ```

Sorry, here is my full output:

> > > > pygmt.show\_versions()  
> > > > PyGMT information:  
> > > > version: v0.10.0  
> > > > System information:  
> > > > python: 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0]  
> > > > executable: /home/user/anaconda3/envs/SWS/bin/python  
> > > > machine: Linux-5.15.0-82-generic-x86\_64-with-glibc2.31  
> > > > Dependency information:  
> > > > numpy: 1.26.2  
> > > > pandas: 2.1.4  
> > > > xarray: 2023.6.0  
> > > > netCDF4: 1.6.2  
> > > > packaging: 23.1  
> > > > contextily: None  
> > > > geopandas: None  
> > > > IPython: 8.19.0  
> > > > rioxarray: None  
> > > > ghostscript: 10.02.1  
> > > > GMT library information:  
> > > > binary version: 6.4.0  
> > > > cores: 192  
> > > > grid layout: rows  
> > > > image layout:  
> > > > library path: /home/user/anaconda3/envs/SWS/lib/libgmt.so  
> > > > padding: 2  
> > > > plugin dir: /home/user/anaconda3/envs/SWS/lib/gmt/plugins  
> > > > share dir: /home/user/anaconda3/envs/SWS/share/gmt  
> > > > version: 6.4.0

---

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 9, 2024, 9:19am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/9 "2024-01-09T09:19:43Z")

</div>

I use this method solved the problem:double free or corruption (out). But a new one has arisen, I will have Segmentation fault (core dumped) when I run my code.

---

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 9, 2024, 9:20am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/10 "2024-01-09T09:20:23Z")

</div>

> [@sherfy](#):
>
> sudo apt install libtcmalloc-minimal4  
> export LD\_PRELOAD=“/usr/lib/x86\_64-linux-gnu/libtcmalloc\_minimal.so.4”

I use this method solved the problem:double free or corruption (out). But a new one has arisen, I will have Segmentation fault (core dumped) when I run my code.

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [January 9, 2024, 10:27am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/11 "2024-01-09T10:27:22Z")

</div>

Can you share the Python script you’re running?

---

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 9, 2024, 10:40am UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/12 "2024-01-09T10:40:06Z")

</div>

> [@seisman](#):
>
> Python script

Sure.

```
def plot_2d_map(region,lon,lat,station_lat,station_long,depth,grd_path,data_path):
    mindepth = float(min(depth))/1000
    fig = pygmt.Figure()
    pygmt.config(MAP_FRAME_TYPE="plain", FONT_ANNOT_PRIMARY="14p")
    fig.basemap(region=region, projection="M10c",frame="a")
    fig.coast(
        projection="M10c",
        region=region,
        frame="a",
        land="white",
        water="lightblue",
        borders="1/1p,black",
    )
    pygmt.makecpt(
        cmap="gray",
        series="0.95/1/0.01",
        continuous=True
    )
    fig.grdimage(
        grid=os.path.join(grd_path,'earth_relief_15s.grd'),
        region=region,
        projection="M10c",
        shading="+a-20+nt0.5+m0",
        cmap=True,
        frame=True
    )

    fig.plot(x=lon,y=lat,style="c0.15c",fill='black')
    fig.plot(x=station_long,y=station_lat,style="t0.3c",fill='black')
    fig.savefig(os.path.join(data_path,'map.png'))
    return 1

def plot_3d(region1,region2,lon,lat,depth,timelist,grd_path,data_path):
    # PyGMT Figure
    fig = pygmt.Figure()
    # Set the azimuth and elevation angle
    perspective = [220, 30]
    # Plot 3D scatter points
    fig.plot3d(
        region=region2,
        x=lon,
        y=lat,
        z=depth,
        projection="M5c",
        style="g0.2c",
        pen="black",
        fill='black',
        frame=[
            "WSZ4",
            'xag+l"latitude"',
            'yag+l"longitude"',
            "z+ldepth(m)",
        ],
        perspective=perspective,
    )
    # Plot basemap
    fig.shift_origin(yshift=8)
    fig.coast(region=region1,
        projection='M5c',
        shorelines='0.25p,black',
        land='grey90',
        perspective=perspective,)

    pygmt.makecpt(
        cmap="gray",
        series="0.95/1/0.01", 
        continuous=True
    )

    fig.grdimage(
        grid=os.path.join(grd_path,'earth_relief_15s.grd'),
        region=region1,
        projection="M5c",
        shading="+a-20+nt0.5+m0", 
        cmap=True, 
        frame=True,  
        perspective=perspective
    )
    fig.savefig(os.path.join(data_path,'3d.png'))
    return

def plot_map(data_path,station_path,grd_path):
    
    station_list = np.loadtxt(station_path,dtype=str)
    station_long = station_list[:,4]
    station_lat = station_list[:,3]

    data = np.loadtxt(os.path.join(data_path,'location.txt'),dtype='str',delimiter=' ')
    time = data[:,0]
    lon = [float(x) for x in data[:,2]]
    lat = [float(x) for x in data[:,3]]
    depth = [float(x) for x in data[:,4]]

    timelist = pd.DataFrame({
        'time': time,
        'value': np.random.rand(len(time))
    })

    timelist['time'] = timelist['time'].astype("datetime64[ns]")

    minlon, maxlon = round(float(min(station_long))-0.1,1),round(float(max(station_long))+0.1,1)
    minlat, maxlat = round(float(min(station_lat))-0.1,1),round(float(max(station_lat))+0.1,1)
    maxdepth = float(max(depth))/1000
    region1 = [minlon, maxlon, minlat, maxlat]
    region2 = [minlon, maxlon, minlat, maxlat,-maxdepth, 0]

    plot_2d_map(region1,lon,lat,station_lat,station_long,depth,grd_path,data_path)
    plot_3d(region1,region2,lon,lat,depth,timelist,grd_path,data_path)

if __name__ == ' __main__':
    data_path = config.get_Location_save_path()
    station_path = config.get_station_file()
    grd_path = config.get_ppt_file_path()
    plot_map(data_path,station_path,grd_path)

```

---

<div class="post-metadata">

**Author:** ![sherfy](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@sherfy](https://forum.generic-mapping-tools.org/u/sherfy)\
**Post date:** [January 9, 2024, 4:00pm UTC](https://forum.generic-mapping-tools.org/t/how-to-fix-pygmt-session-error-not-available-in-classic-mode/4573/13 "2024-01-09T16:00:39Z")

</div>

Thank you for your answers. I really appreciate you helping me with this. 😃  
The problem should be caused by the bash GMT code with os.system before running this code. 😂  
But how to close the GMT correctly? By using the “gmt clear sessions”?
