Reputation: 191
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?
Upvotes: 19
Views: 31175
Reputation: 29
Note: This answer assumes that you want to have the mypy python module installed globally. If you're installing the mypy module locally, for example in a virtual environment, the solution may be different.
First of all install the mypy module globally using pip install mypy
in any terminal
If the original mypy extension made by Matan Gover is still not working for you: Check does the mypy
command work when you use it manually.
Example command in PowerShell to test does mypy work when you're running it manually mypy path/to/any/python/file
.
If you haven't gotten any errors and the extension still does not work, sadly this guide won't help you.
If you've gotten an error when using the mypy command manually, you can read the below guide of how I solved my error
What my problem was is that the original mypy extension by Matan Gover on VSCode did not work for me when I installed it. There were no suggestions in the VSCode's "Problems" tab.
I had 2 problems:
I did not even have the mypy module installed. So I ran pip install mypy
What I found out later is that when I tried to run mypy manually via the mypy path/to/a/python/file
console command, I got this response:
Fatal error in launcher: Unable to create process using '"C:\Program Files\Python311\python.exe" "C:\Users\Jacob\AppData\Roaming\Python\Python11\Scripts\mypy.exe" main.py': The system cannot find the file specified'
What I realised after thoroughly reading the error text a few times is that it tried to use Python 3.11 for some reason. But I didn't have Python 3.11 installed!
I don't know why it wanted to use python 3.11. I just downloaded python 3.11 from the python website and now the extension works.
There were probably some other solution that did not involve installing a second python interpreter, but this works fine
Upvotes: 0
Reputation: 1
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:
Then, it works properly as shown below:
Upvotes: 2
Reputation: 67
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.
Upvotes: 1
Reputation: 5987
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.
Upvotes: 3
Reputation: 41
Adding on to previous answers, to enable Mypy, one also needs to
Upvotes: 0
Reputation: 880
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
Upvotes: 1
Reputation: 10372
In VS Code, "mypy" is one of the python code analysis tools, we usually install and use it as follows:
Install it. (pip install mypy
)
Check the installation: (pip show mypy
)
Select "mypy": (F1
, Python: Select Linter
, mypy
)
Run "mypy": (F1
, Python: Run Linting
)
Its effect:
Reference: Linting in VS Code.
Upvotes: 16
Reputation: 34873
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
Upvotes: 0