14

I'm trying to install the mypy linter in Visual Studio Code version 1.53 on MacOS. I've never used a linter before, so I'm not sure what to expect, though I know it should be highlighting type errors and the such. I'm trying to get mypy working in the context of a Django app.

I followed these steps and restarted VS Code a few times, but the instructions don't seem to be working. I've also gone through the settings to ensure mypy linting is enabled and even tried changing the Python > Linting: Mypy Path setting to be the full path returned when doing which mypy; this also did not work.

Lastly, I installed this package from the VS Code marketplace: Mypy . I installed it through the marketplace in VS Code and followed the virtual environment instructions in the description.

django-stubs and mypy are both installed and I configured the mypy.ini file as suggested in the PyPI docs above.

I get the following error:

Error running mypy: mypy.ini:2:1: error: Error importing plugin 'mypy_django_plugin.main': No module named 'mypy_django_plugin'

How do I fix this error?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
nienna73
  • 141
  • 1
  • 1
  • 4

7 Answers7

14

In VS Code, "mypy" is one of the python code analysis tools, we usually install and use it as follows:

  1. Install it. (pip install mypy)

    Check the installation: (pip show mypy)

    enter image description here

  2. Select "mypy": (F1, Python: Select Linter, mypy)

    enter image description here

  3. Run "mypy": (F1, Python: Run Linting)

    enter image description here

Its effect:

enter image description here

Reference: Linting in VS Code.

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
2

VSCode asks you to enter the path to dmypy which is the mypy daemon. After installing mypy (ex. via conda install -c conda-forge mypy), you can find this path with which dmypy and then enter this in VSCode.

enter image description here

Eric Wiener
  • 4,929
  • 4
  • 31
  • 40
  • There's also an option further down `Run Using Active Interpreter` which allows you to create a per-project venv for example, install mypy in it and use this version (just set the executable to `dmypy`). – David Waterworth Apr 12 '23 at 02:08
1

I had the same issue loading mypy plugin. (Using conda environement). It was resolved when I started vscode from terminal with the right python environment activated

conda activate my-env
cd my/vs-code/project/dir
code .

hope this helps

asiera
  • 492
  • 5
  • 12
0

mypy is running through ~/.mypyls, a virtualenv that needs to have plugins installed in it for mypyls to find them.

To get mypy_django_plugin in there:

cd ~/.mypyls
. bin/activate
pip install django-stubs
andrewdotn
  • 32,721
  • 10
  • 101
  • 130
0

Adding on to previous answers, to enable Mypy, one also needs to

  • Open the "User Settings"
  • Search for Mypy Enabled
Ruotong Jia
  • 31
  • 1
  • 3
0

Go to settings, search for "mypy.runUsingActiveInterpreter" and check the box.

Or you install mypy "pip install mypy" globally, i.e. to your main Python environment.

Fhuad Balogun
  • 47
  • 2
  • 8
0

I just installed Mypy extension to VSCode as shown below and if Install Release Version doesn't work, click on Install Pre-Release Version. *I use Anaconda on Windows 11:

enter image description here

Then, it works properly as shown below:

enter image description here

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129