No module named pygmt - VS code

An error occurred after installing pygmt in VisualStudio code.
When import pygmt is executed after installing pygmt, ModuleNotFoundError: No module named’pygmt’ appears.
How to do it?

Welcome @Hyunyoung! Just want to double check that VS Code has loaded the correct pygmt conda environment and that you had followed the step by step instructions at https://www.pygmt.org/v0.2.1/install.html? The bottom left corner of your screenshot suggests that you are using the pygmt virtual environment, but could you try typing conda list into the terminal console and see if pygmt is in the installed list of packages.

These related links may be helpful:

Feel free to post more error messages or screenshots if you still have problems.

1

Still, the problem continues.

The error seems to appear because the virtual environment is set to pygmt. It was solved by changing the virtual environment.

However, another problem arose.

When I run the first example of pygmt, it doesn’t recognize the figure.

Hmm, that AttributeError: module 'pygmt' has no attribute 'Figure' is strange. Can you try to provide the output of print(pygmt.show_versions())? For example, mine looks like:

PyGMT information:
  version: v0.2.1
System information:
  python: 3.8.6 | packaged by conda-forge | (default, Oct  7 2020, 19:08:05)  [GCC 7.5.0]
  executable: /home/user/miniconda3/envs/pygmt/bin/python
  machine: Linux-4.19.0-8-amd64-x86_64-with-glibc2.10
Dependency information:
  numpy: 1.19.4
  pandas: 1.1.4
  xarray: 0.16.2
  netCDF4: 1.5.4
  packaging: 20.4
  ghostscript: 9.53.3
  gmt: 6.1.1
GMT library information:
  binary dir: /home/user/miniconda3/envs/pygmt/bin
  cores: 2
  grid layout: rows
  library path: /home/user/miniconda3/envs/pygmt/lib/libgmt.so
  padding: 2
  plugin dir: /home/user/miniconda3/envs/pygmt/lib/gmt/plugins
  share dir: /home/user/miniconda3/envs/pygmt/share/gmt
  version: 6.1.1

Also, just making sure that you don’t have a folder called ‘pygmt’ or a ‘pygmt.py’ file in your working directory?

There was a pygmt folder, so I kept getting errors. Thank you very much for your help.

1 Like

Hello. I’m new to GMT and trying to install it. i followed this link Installing — PyGMT to setup conda environment and other dependencies libraries using anaconda prompt…i did so but when importing pygmt on my jupyter notebook it says "No module named ‘pygmt’ ". why is that? please help!

Hi @marihatabassum, just checking that you have set jupyter to use the pygmt kernel? See my answer at https://flint.soest.hawaii.edu/t/no-module-named-pygmt-in-spyder/702/3:

1 Like

Hi @weiji14 My kernel only shows python 3 when about to change…no pygmt in jupyter notebook! but i have run all the command for installation on anaconda prompt. And the python version is 3.7.X. should i change it? as you have mentioned for spyder!

Right, you’ll need to install the pygmt kernel then. Run these commands in your anaconda prompt:

conda activate pygmt
python -m ipykernel install --user --name pygmt  #to install conda env properly
jupyter kernelspec list --json                        #see if kernel is installed

The output should of jupyter kernelspec list --json should include a section that looks like this:

    "pygmt": {
      "resource_dir": "/home/username/.local/share/jupyter/kernels/pygmt",
      "spec": {
        "argv": [
          "/home/username/miniconda3/envs/pygmt/bin/python",
          "-m",
          "ipykernel_launcher",
          "-f",
          "{connection_file}"
        ],
        "env": {},
        "display_name": "pygmt",
        "language": "python",
        "interrupt_mode": "signal",
        "metadata": {}
      }
    },

After this, if you re-launch jupyter, there should be a pygmt kernel you can connect to. Let me know if that works.

1 Like

Thank you so much @weiji14 for your kind response. I have installed pygmt sucessfully. yes i had to run those commands below…after using Installing — PyGMT this insatallation process…to set up kernel environment into pygmt.

python -m ipykernel install --user --name pygmt
conda install jupyter
jupyter notebook

Note: relaunching doesn’t help so i had to install jupyter again in the environment and then relaunch!



1 Like