Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python executable inference not run when using --py2 of --python-version #8504

Closed
mikolajz opened this issue Mar 6, 2020 · 5 comments
Closed
Labels
priority-2-low topic-python2 issues only applicable to Python 2

Comments

@mikolajz
Copy link

mikolajz commented Mar 6, 2020

  • Are you reporting a bug, or opening a feature request?
    I would say a bug.

  • What is the actual behavior/output?
    When using the --py2 flag, one needs to additionally pass --python-executable=/usr/bin/python2 flag for mypy to find site packages.

  • What is the behavior/output you expect?
    mypy should find the Python 2 binary on its own. I think it's a bug since there is code in main.py to do it, but it's run only when options.python_executable is None (c.f. infer_python_version). However, Options.__init__ initializes it with sys.executable and it's not None.

Changing Options.__init__ to initialize it to None fixes the problem, but I don't know if it may have negative side-effects.

I think the same applies to using --python-version to set any version different than the one mypy runs on.

  • What are the versions of mypy and Python you are using?
    mypy 0.761 running undef Python 3.5.2 to analyze Python 2.7.12 code.

    Do you see the same issue after installing mypy from Git master?
    Not checked, but the relevant code all looks the same.

@graingert
Copy link
Contributor

https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-python-version

--python-version X.Y

    This flag will make mypy type check your code as if it were run under Python version X.Y. Without this option, mypy will default to using whatever version of Python is running mypy. Note that the -2 and --py2 flags are aliases for --python-version 2.7.

    This flag will attempt to find a Python executable of the corresponding version to search for PEP 561 compliant packages. If you’d like to disable this, use the --no-site-packages flag (see Import discovery for more details).

@mikolajz but you'd usually want to point mypy at your python 2.7 virtual environment with https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-python-executable

@mikolajz
Copy link
Author

What we have in our case is a Docker image where the system Python2 is configured the way we like. It worked, but it was surprising why it didn't find the system libraries, instead trying to use Python3 system libraries.

It would be especially nice if it worked out of the box, since I couldn't find any hints of what libraries directory is used in any debug output, so have to add ad hoc debug prints to mypy source code to understand what's happening.

@ethanhs
Copy link
Collaborator

ethanhs commented Apr 20, 2020

@mikolajz if you point to the Python 2 executable it will be searched. Finding a Python executable is not an easy problem to solve in the general case, and choosing the wrong one is problematic. If you want to see what libraries are used, I believe -vv will indicate this information

@mikolajz
Copy link
Author

Right, -vv gives lots of useful info, including the paths that are used - thanks for the hint (maybe I was using only one -v?).

I personally don't care that much about a solution, since I already know that I need to add --python_executable, but it's quite tricky (and, as @graingert pointed, contrary to the flag documentation which promises that the proper binary will be searched). Maybe you should print a warning if someone tries to use --python-version without --python-executable? Is there a valid usecase for this?

@AlexWaygood AlexWaygood added priority-2-low topic-python2 issues only applicable to Python 2 labels Mar 25, 2022
@JukkaL
Copy link
Collaborator

JukkaL commented Mar 31, 2022

Closing due to Python 2 feature freeze (#12237).

@JukkaL JukkaL closed this as completed Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-2-low topic-python2 issues only applicable to Python 2
Projects
None yet
Development

No branches or pull requests

5 participants