No module named 'pygmt'

Hello,

I am a beginner and I am looking to install PYGMT on windows 10. I first installed anaconda for windows10 (C:\Users\burlotr). Then I launched the Anaconda Powershell Prompt to start the installation of PYGMT.

I followed the installation guide on the site but I’m stuck at the 3rd step of the QuickStart.

(base) PS C:\Users\burlotr> conda activate pygmt
(pygmt) PS C:\Users\burlotr> jupyter notebook

when I try to import pygmt, the error message No module named ‘pygmt’ appears.

I know that other similar topics exist, but I was not able to solve my problem with their contents. Maybe I missed a clue?

Recently I installed the pygmt kernel (according to this topic), which was not present in jupyter notebook. But it didn’t change anything.

I hope you can enlighten me. Thank you.

Hi @Remy, thanks for trying out PyGMT! To help you with your problem, can you provide the output from the following commands (run it step by step in Anaconda prompt):

conda activate pygmt
conda list
jupyter kernelspec list --json
python -c "import pygmt; pygmt.show_versions()"

Just want to make sure that things are installed properly, sometimes the paths can get a bit messed up which results in the ModuleNotFoundError.

Hi @weiji14, Thank you for your follow-up!

Yes, here are the outputs

(base) PS C:\Users\burlotr> conda activate pygmt
(pygmt) PS C:\Users\burlotr> conda list
 packages in environment at C:\Users\burlotr\Anaconda3\envs\pygmt:
#
 Name                    Version                   Build  Channel
boost-cpp                 1.74.0               h5b4e17d_5    conda-forge
bzip2                     1.0.8                h8ffe710_4    conda-forge
ca-certificates           2021.10.8            h5b45459_0    conda-forge
cairo                     1.16.0            h15b3021_1009    conda-forge
cfitsio                   4.0.0                hd67004f_0    conda-forge
cftime                    1.5.1.1         py310h2873277_1    conda-forge
curl                      7.80.0               h789b8ee_0    conda-forge
expat                     2.4.1                h39d44d4_0    conda-forge
ffmpeg                    4.3.1                ha925a31_0    conda-forge
fftw                      3.3.10          nompi_hb424310_101    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.13.1            h1989441_1005    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
freetype                  2.10.4               h546665d_1    conda-forge
freexl                    1.0.6                ha8e266a_0    conda-forge
gdal                      3.3.3           py310h2a306c7_10    conda-forge
geos                      3.10.1               h39d44d4_1    conda-forge
geotiff                   1.7.0                hc8731e1_5    conda-forge
gettext                   0.19.8.1          ha2e2712_1008    conda-forge
ghostscript               9.54.0               h0e60522_1    conda-forge
gmt                       6.2.0                ha555b01_2    conda-forge
graphicsmagick            1.3.36               hd130993_2    conda-forge
hdf4                      4.2.15               h0e5069d_3    conda-forge
hdf5                      1.12.1          nompi_h57737ce_102    conda-forge
icu                       69.1                 h0e60522_0    conda-forge
importlib-metadata        4.8.2           py310h5588dad_0    conda-forge
importlib_metadata        4.8.2                hd8ed1ab_0    conda-forge
intel-openmp              2021.4.0          h57928b3_3556    conda-forge
jbig                      2.1               h8d14728_2003    conda-forge
jpeg                      9d                   h8ffe710_0    conda-forge
kealib                    1.4.14               h8995ca9_3    conda-forge
krb5                      1.19.2               h6da9e4a_3    conda-forge
lcms2                     2.12                 h2a16943_0    conda-forge
lerc                      3.0                  h0e60522_0    conda-forge
libblas                   3.9.0              12_win64_mkl    conda-forge
libcblas                  3.9.0              12_win64_mkl    conda-forge
libcurl                   7.80.0               h789b8ee_0    conda-forge
libdeflate                1.8                  h8ffe710_0    conda-forge
libffi                    3.4.2                h8ffe710_5    conda-forge
libgdal                   3.3.3               h58f6a35_10    conda-forge
libglib                   2.70.1               h3be07f2_0    conda-forge
libiconv                  1.16                 he774522_0    conda-forge
libkml                    1.3.0             h9859afa_1014    conda-forge
liblapack                 3.9.0              12_win64_mkl    conda-forge
libnetcdf                 4.8.1           nompi_h1cc8e9d_101    conda-forge
libpng                    1.6.37               h1d00b33_2    conda-forge
libpq                     14.1                 h1ea2d34_1    conda-forge
librttopo                 1.1.0                he35e8ac_8    conda-forge
libspatialite             5.0.1               hf126459_12    conda-forge
libssh2                   1.10.0               h9a1e1f7_2    conda-forge
libtiff                   4.3.0                hd413186_2    conda-forge
libwebp-base              1.2.1                h8ffe710_0    conda-forge
libxml2                   2.9.12               hf5bbc77_1    conda-forge
libzip                    1.8.0                h519de47_1    conda-forge
libzlib                   1.2.11            h8ffe710_1013    conda-forge
lz4-c                     1.9.3                h8ffe710_1    conda-forge
mkl                       2021.4.0           h0e2418a_729    conda-forge
netcdf4                   1.5.8           nompi_py310h5489b47_101    conda-forge
numpy                     1.21.4          py310h5ebf175_0    conda-forge
openjpeg                  2.4.0                hb211442_1    conda-forge
openssl                   3.0.0                h8ffe710_2    conda-forge
packaging                 21.3               pyhd8ed1ab_0    conda-forge
pandas                    1.3.4           py310hf5e1058_1    conda-forge
pcre                      8.45                 h0e60522_0    conda-forge
pip                       21.3.1             pyhd8ed1ab_0    conda-forge
pixman                    0.40.0               h8ffe710_0    conda-forge
poppler                   21.11.0              h24fffdf_0    conda-forge
poppler-data              0.4.11               hd8ed1ab_0    conda-forge
postgresql                14.1                 he353ca9_1    conda-forge
proj                      8.2.0                h1cfcee9_0    conda-forge
pygmt                     0.5.0              pyhd8ed1ab_1    conda-forge
pyparsing                 3.0.6              pyhd8ed1ab_0    conda-forge
python                    3.10.0          hcf16a7b_3_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python_abi                3.10                    2_cp310    conda-forge
pytz                      2021.3             pyhd8ed1ab_0    conda-forge
setuptools                59.2.0          py310h5588dad_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
sqlite                    3.36.0               h8ffe710_2    conda-forge
tbb                       2021.4.0             h2d74725_1    conda-forge
tiledb                    2.5.1                h47404fa_1    conda-forge
tk                        8.6.11               h8ffe710_1    conda-forge
typing_extensions         4.0.0              pyha770c72_0    conda-forge
tzdata                    2021e                he74cb21_0    conda-forge
ucrt                      10.0.20348.0         h57928b3_0    conda-forge
vc                        14.2                 hb210afc_5    conda-forge
vs2015_runtime            14.29.30037          h902a5da_5    conda-forge
wheel                     0.37.0             pyhd8ed1ab_1    conda-forge
xarray                    0.20.1             pyhd8ed1ab_0    conda-forge
xerces-c                  3.2.3                h0e60522_4    conda-forge
xz                        5.2.5                h62dcd97_1    conda-forge
zipp                      3.6.0              pyhd8ed1ab_0    conda-forge
zlib                      1.2.11            h8ffe710_1013    conda-forge
zstd                      1.5.0                h6255e5f_0    conda-forge
>(pygmt) PS C:\Users\burlotr> jupyter kernelspec list --json
{
  "kernelspecs": {
    "pygmt": {
      "resource_dir": "C:\\Users\\burlotr\\AppData\\Roaming\\jupyter\\kernels\\pygmt",
      "spec": {
        "argv": [
          "C:\\Users\\burlotr\\Anaconda3\\python.exe",
          "-m",
          "ipykernel_launcher",
          "-f",
          "{connection_file}"
        ],
        "env": {},
        "display_name": "pygmt",
        "language": "python",
        "interrupt_mode": "signal",
        "metadata": {
          "debugger": true
        }
      }
    },
    "python3": {
      "resource_dir": "C:\\Users\\burlotr\\Anaconda3\\share\\jupyter\\kernels\\python3",
      "spec": {
        "argv": [
          "C:/Users/burlotr/Anaconda3\\python.exe",
          "-m",
          "ipykernel_launcher",
          "-f",
          "{connection_file}"
        ],
        "env": {},
        "display_name": "Python 3 (ipykernel)",
        "language": "python",
        "interrupt_mode": "signal",
        "metadata": {
          "debugger": true
        }
      }
    }
  }
}
(pygmt) PS C:\Users\burlotr> python -c "import pygmt; pygmt.show_versions()"
PyGMT information:
  version: v0.5.0
System information:
  python: 3.10.0 | packaged by conda-forge | (default, Nov 20 2021, 02:18:13) [MSC v.1916 64 bit (AMD64)]
  executable: C:\Users\burlotr\Anaconda3\envs\pygmt\python.exe
  machine: Windows-10-10.0.19044-SP0
Dependency information:
  numpy: 1.21.4
  pandas: 1.3.4
  xarray: 0.20.1
  netCDF4: 1.5.8
  packaging: 21.3
  ghostscript: 9.54.0
  gmt: 6.2.0
GMT library information:
  binary dir: C:/Users/burlotr/Anaconda3/envs/pygmt
  cores: 8
  grid layout: rows
  library path: C:/Users/burlotr/Anaconda3/envs/pygmt/Library/bin/gmt.dll
  padding: 2
  plugin dir: C:/Users/burlotr/Anaconda3/envs/pygmt/Library/bin/gmt_plugins
  share dir: C:/Users/burlotr/Anaconda3/envs/pygmt/Library/share/gmt
  version: 6.2.0

Right, so from your jupyter kernelspec list --json output, it appears that your ‘pygmt’ kernel was not installed correctly, it is using the system python (C:\Users\burlotr\Anaconda3\python.exe) instead of the ‘pygmt’ virtual environment’s python. What you will need to do is re-run these commands:

conda activate pygmt
python -m ipykernel install --user --name pygmt
jupyter kernelspec list --json

Note that the conda activate pygmt step is the critical part here, you want to make sure that you are inside the ‘pygmt’ virtual environment before doing the ipykernel install. If all goes well, the output from jupyter kernelspec list --json should say the ‘pygmt’ environment uses Python from C:\Users\burlotr\Anaconda3\envs\pygmt\bin\python.exe (or something like that).

(base) C:\Users\burlotr>conda activate pygmt

(pygmt) C:\Users\burlotr>python -m ipykernel install --user --name pygmt
C:\Users\burlotr\Anaconda3\envs\pygmt\python.exe: No module named ipykernel

I guess I have to install the ipykernel module in the pygmt environment =)

Ah yes, you’ll have to do conda install ipykernel first, or if you’re using jupyter, you might as well do conda install jupyterlab (which will pull in ipykernel too).

Note that you’ll have to run these after conda activate pygmt (i.e. make sure there is (pygmt) on the left which indicates you are in the ‘pygmt’ virtual environment.

(pygmt) PS C:\Users\burlotr> conda install ipykernel
Collecting package metadata (current_repodata.json): done
Solving environment: done

Package Plan

environment location: C:\Users\burlotr\Anaconda3\envs\pygmt

added / updated specs:
- ipykernel

(…)

(pygmt) PS C:\Users\burlotr> python -m ipykernel install --user --name pygmt
Installed kernelspec pygmt in C:\Users\burlotr\AppData\Roaming\jupyter\kernels\pygmt

(pygmt) PS C:\Users\burlotr> jupyter kernelspec list --json
{
“kernelspecs”: {
“pygmt”: {
“resource_dir”: “C:\Users\burlotr\AppData\Roaming\jupyter\kernels\pygmt”,
“spec”: {
“argv”: [
“C:\Users\burlotr\Anaconda3\envs\pygmt\python.exe”,
“-m”,
“ipykernel_launcher”,
“-f”,
“{connection_file}”
],
“env”: {},
“display_name”: “pygmt”,
“language”: “python”,
“interrupt_mode”: “signal”,
“metadata”: {
“debugger”: true
}
}
},
“python3”: {
“resource_dir”: “C:\Users\burlotr\Anaconda3\envs\pygmt\share\jupyter\kernels\python3”,
“spec”: {
“argv”: [
“C:/Users/burlotr/Anaconda3/envs/pygmt\python.exe”,
“-m”,
“ipykernel_launcher”,
“-f”,
“{connection_file}”
],
“env”: {},
“display_name”: “Python 3 (ipykernel)”,
“language”: “python”,
“interrupt_mode”: “signal”,
“metadata”: {
“debugger”: true
}
}
}
}
}

Yes, the changes have been taken into account !
But the same error, from my first message, appeared on jupyter notebook.
And yes, I am careful to run the command “conda activate pygmt”. it is indicated just to the left of my requests -->(pygmt)

Ok, last thing to try, could you launch jupyter from within the ‘pygmt’ virtual environment? Try this:

conda activate pygmt
jupyter lab clean --all
jupyter lab

If you get a ‘jupyter’ not found error, try doing conda install jupyterlab first. Oh, and make sure you have the ‘pygmt’ kernel selected on the top right corner. Sorry for all the extra steps, just that Python virtual environments can be tricky sometimes…

No problem !

(pygmt) C:\Users\burlotr>conda install jupyterlab
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.

ResolvePackageNotFound:

  • python=3.1

I tried to reinstall jupyter lab, but it doesn’t work in the pygmt environment.

Oo ok, that python=3.1 error is because you have Python 3.10 installed (there’s a silly conda bug where 3.10 gets converted to 3.1), could you do conda install python=3.9 jupyterlab instead?

(pygmt) C:\Users\burlotr>conda install python=3.9

Done

(pygmt) C:\Users\burlotr>conda install jupyterlab

Done

(pygmt) C:\Users\burlotr>jupyterlab

And …

It’s working !!
Thank you very much @weiji14

1 Like

Excellent! Enjoy using PyGMT, and let us know in the forum if you have any specific questions :partying_face:

not so fast… :sweat_smile:
When I run the last example sequence, it says “name ‘pygmt’ is not defined”.

Maybe a path problem?
I tried to add the path "GMT_LIBRARY_PATH=C:\Users\USERNAME\Anaconda3\envs\pygmt\Library\bin" in windows, but nothing changes.

Huh, that’s strange. If you did import pygmt and pygmt.show_versions(), then things should be working. Could you restart the jupyter kernel and retry? Maybe do a help(pygmt) and see if you get any output.

Oh my bad ! That’s because I ran the script later, without redoing the import…
That’s working now ! Thank you @weiji14