leinardi / mypy-pycharm

A plugin providing both real-time and on-demand scanning of Python files with Mypy from within PyCharm/IDEA.
Apache License 2.0
187 stars 30 forks source link

Unable to configure mypy binary on Linux #24

Open MarcinOrlowski opened 5 years ago

MarcinOrlowski commented 5 years ago

Step 1: Are you in the right place?

Step 2: Describe your environment

Step 3: Describe the problem:

Steps to reproduce:

  1. I am using Ubuntu/Debian based distro
  2. I have both python2 and python3 installed
  3. Installed python3-pip
  4. installed mypy using pip3 install mypy
  5. Properly installed in /usr/local/bin/mypy
  6. above path is in $PATH
  7. mypy runs when started from shell
  8. Attempt to configure your plugin with either mypy or /usr/local/bin/mypy as mypy binary does not work (test fails) with Mypy Plugin: Failure: executable "mypy" not found.

This looks similar to #12

flowersw commented 5 years ago

Similar issue here, with pycharm generated stacktrace:

2018-11-10T13:11:22.487
java.lang.Throwable: Error while checking Mypy path:   File "/home/will/.pyenv/shims/mypy", line 3
    [ -n "$PYENV_DEBUG" ] && set -x
                      ^
SyntaxError: invalid syntax
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:123)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.isMypyPathValid(MypyRunner.java:102)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.checkMypyAvailable(MypyRunner.java:176)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.checkMypyAvailable(MypyRunner.java:152)
    at com.leinardi.pycharm.mypy.MypyInspection.inspectFile(MypyInspection.java:76)
    at com.leinardi.pycharm.mypy.MypyInspection.lambda$checkFile$0(MypyInspection.java:65)
    at com.intellij.openapi.application.impl.ApplicationImpl$2.call(ApplicationImpl.java:337)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
leinardi commented 5 years ago

@MarcinOrlowski, @flowersw could you please attach the idea.log file (https://intellij-support.jetbrains.com/hc/en-us/articles/207241085-Locating-IDE-log-files) after you get the Exception?

@MarcinOrlowski which interpreter are you using in PyCharm? The system one or a venv?

MarcinOrlowski commented 5 years ago

Here's my stacktrace

java.lang.Throwable: Error while checking Mypy path: Traceback (most recent call last):
  File "/usr/local/google/home/orlowskim/mypy", line 7, in <module>
    from mypy.__main__ import console_entry
ImportError: No module named mypy.__main__
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:123)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.isMypyPathValid(MypyRunner.java:102)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.checkMypyAvailable(MypyRunner.java:176)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.checkMypyAvailable(MypyRunner.java:152)
    at com.leinardi.pycharm.mypy.MypyInspection.inspectFile(MypyInspection.java:76)
    at com.leinardi.pycharm.mypy.MypyInspection.lambda$checkFile$0(MypyInspection.java:65)
    at com.intellij.openapi.application.impl.ApplicationImpl$2.call(ApplicationImpl.java:337)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

I am using 2.7 via venv

hackalyze commented 5 years ago

I ran into a similar issue, it was because it was using the python binary in my venv to execute the global mypy installed with pip3. I had to install mypy in my venv and then I had to provide the path to that mypy in the "Path to Mypy executable" setting.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had activity in the last 60 days.

chadrik commented 5 years ago

I ran into the same problem on MacOS 10.13. In my case the error is:

2019-04-24 23:07:29,098 [ 586314]   INFO - .pycharm.mypy.mpapi.MypyRunner - Command Line string: /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /usr/local/bin/mypy -V 

Why is the plugin using a python interpreter instead of just invoking mypy using the provided path and deferring to the python executable it was installed with? Trying to guess at another interpreter is just asking for trouble. In my case it's using python2, which obviously isn't going to work.

chadrik commented 5 years ago

It seems like a lot of people are running into this same basic problem. Issues #18 and #26 are basically the same.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had activity in the last 60 days.

paranoidi commented 5 years ago

Yeah, not working on Mint either ... Installed mypy using pipx (so just a virtualenv)

$ /home/myusernamehere/.local/pipx/venvs/mypy/bin/mypy
usage: mypy [-h] [-v] [-V] [more options; see below]
            [-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
mypy: error: Missing target module, package, files, or command.

Direct copy & paste /home/myusernamehere/.local/pipx/venvs/mypy/bin/mypy does not work.

Edit:

Looks like it's trying to use the projects virtualenv, not the one created by pipx where mypy is installed in ..

path: /home/usernamehere/.virtualenvs/projectnamehere/bin/python: can't find '__main__' module in '/home/usernamehere/projects/projectnamehere/' 
java.lang.Throwable: Error while checking Mypy path: /home/usernamehere/.virtualenvs/projectnamehere/bin/python: can't find '__main__' module in '/home/usernamehere/projects/projectnamehere/'
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:145)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.isMypyPathValid(MypyRunner.java:103)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.checkMypyAvailable(MypyRunner.java:177)
    at com.leinardi.pycharm.mypy.mpapi.MypyRunner.checkMypyAvailable(MypyRunner.java:153)
    at com.leinardi.pycharm.mypy.MypyInspection.inspectFile(MypyInspection.java:76)
    at com.leinardi.pycharm.mypy.MypyInspection.lambda$checkFile$0(MypyInspection.java:65)
    at com.intellij.openapi.application.impl.ApplicationImpl$2.call(ApplicationImpl.java:339)
    at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)

Edit2:

Installing mypy in project venv resolved the issue, but it should work from elsewhere too ..

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had activity in the last 60 days.

bandophahita commented 4 years ago

I had similar issue, but in reverse order. I installed mypy via pip in my virtual environment and pycharm gave the error "executable not found". I then installed mypy in my actual environment and then the plugin started working in pycharm.

In short, I had to install mypy in both the venv and the regular environment to get the plugin working.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had activity in the last 60 days.

sitnarf commented 4 years ago

This managed to fix the issue for me. I belive my pipenv enviroment appears twice in PyCharm. When I select the second one (see screenshot), it even automatically detected the package. image

phoenixeliot commented 3 years ago

For some reason I had to do @sitnarf's suggestion as well, and not just set the project interpreter in PyCharm's project settings. This might be because I had to destroy and update my venv recently, and PyCharm didn't seem to fully propagate that through in a few places. This might have just been a fallout from the same not-propagating.

aggo commented 2 years ago

in my case, i tried running mypy from the terminal which had the env activated and it wasn't working there either, saying I must install typed-ast for it to work. this is the output:


❯❯❯ mypy setup.py
The typed_ast package is not installed.
You can install it with `python3 -m pip install typed-ast`.
❯❯❯ python3 -m pip install typed-ast                                                                                                                                                                                                                ✘ 1 
Collecting typed-ast
  Downloading typed_ast-1.5.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (843 kB)
     |████████████████████████████████| 843 kB 1.5 MB/s            
Installing collected packages: typed-ast
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
mypy 0.910 requires typed-ast<1.5.0,>=1.4.0; python_version < "3.8", but you have typed-ast 1.5.1 which is incompatible.
astroid 2.6.2 requires typed-ast<1.5,>=1.4.0; implementation_name == "cpython" and python_version < "3.8", but you have typed-ast 1.5.1 which is incompatible.
Successfully installed typed-ast-1.5.1
❯❯❯ mypy setup.py
mypy: can't read file 'setup.py': No such file or directory```
5cat commented 2 years ago

Looking into the logfile of pycharm i found out that the plugin is using another pipenv environment that doesnt have mypy instead of using the one pycharm is set on.

2022-01-22 17:43:57,883 [ 574664]   INFO - .pycharm.mypy.mpapi.MypyRunner - Command Line string: /home/CCC/.local/share/virtualenvs/XXX-I8024MuH/bin/python /home/XXX/.local/share/virtualenvs/YYY-LWz7hkGd/bin/mypy -V 

so i simply deleted the XXX environment to force it to use the YYY environment and it solved the issue for me. this page helped me find the log file.

derula commented 1 year ago

Is there any reason why MyPy has to be installed in the project's virtualenv? Why can't it just be called as a binary, without using the project interpreter?

In my case, using the project interpreter makes no practical sense. I have 10+ projects opened at once (in the same window), each has its own virtualenv and project interpreter.

  1. I don't want to install MyPy 10+ times (what for?)
  2. I want to run checks across all open projects, not once for each project by changing the project interpreter every time

This reliance on the project interpreter makes this plugin (as well as the PyLint plugin by the same author) effectively useless for me, unfortunately.

leinardi commented 1 year ago

If I recall correctly, it was done to be sure that the installation of mypy/pylint has access to the same dependencies of the project, otherwise it may lead to false positive.

If someone wants to try to improve this, PRs are welcome.

derula commented 1 year ago

Hmm, that reasoning actually makes sense assuming that mypy, like pylint, doesn't support separating its own dependencies from the project dependencies. (I know I tried before to convince pylint to do that, with extremely limited success.)

In lieu of that, the only other solution I can think of is to improve this plugin's multi-project support, e.g.:

I know literally nothing about IntelliJ plugin development, but if others are interested in improving multi-project support, I could create a feature request ticket with my UI/UX ideas.