python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.27k stars 2.79k forks source link

mypy cannot start in a Windows venv #12192

Closed reinderien closed 2 years ago

reinderien commented 2 years ago

Bug Report

On Windows, running Python 3.8 with mypy 0.931 installed to a virtualenv, it cannot start with any of these methods:

In all cases, I get this stack trace:

Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\me\src\myproject\.venv\Scripts\mypy.exe\__main__.py", line 7, in <module>
  File "c:\users\me\src\myproject\.venv\lib\site-packages\mypy\__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy\main.py", line 96, in main
  File "mypy\main.py", line 173, in run_build
  File "mypy\build.py", line 180, in build
  File "mypy\build.py", line 212, in _build
  File "mypy\modulefinder.py", line 781, in compute_search_paths
  File "mypy\modulefinder.py", line 646, in get_site_packages_dirs
    debug = 0
  File "C:\Program Files\Python38\lib\subprocess.py", line 415, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "C:\Program Files\Python38\lib\subprocess.py", line 493, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Program Files\Python38\lib\subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files\Python38\lib\subprocess.py", line 1311, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

In this context, args is ./.venv/bin/python C:\\Users\\me\\src\\myproject\\.venv\\lib\\site-packages\\mypy\\pyinfo.py getsitepackages'.

mypy.modulefinder.get_site_packages_dirs is passing in an incorrect path to Python, because on Windows .venv/bin/python is the wrong path to a venv Python binary, and should instead be ./.venv/Scripts/python.exe. Doing the really bad thing and editing Python38/Lib/subprocess.py so that check_output contains

    first = popenargs[0][0]
    if first == './.venv/bin/python':
        popenargs = (['./.venv/Scripts/python.exe'] + popenargs[0][1:],) + popenargs[1:]

"fixes" the problem.

hauntsaninja commented 2 years ago

Thanks for the report!

@AlexWaygood if you're up for it, want to make another "improve mypy on Windows" PR? :-)

(For context, mypy does a sneaky amount of "actual runtime" work by executing mypy/pyinfo.py. In this case, we're figuring out site packages in mypy/modulefinder.py)

AlexWaygood commented 2 years ago

@AlexWaygood if you're up for it, want to make another "improve mypy on Windows" PR? :-)

Might be difficult for me to find the time, I'm afraid — I've already got quite a lot on ://

reinderien commented 2 years ago

Egg meet face: this is entirely my fault, and was due to an explicit - and incorrect - python_executable setting in a surprise .ini that was being applied.