Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Python Unresolved import issue #918

Closed

Description

@dmayo77

When pylinting a file in a big project, it appears as though libraries installed in a venv are not being discovered and thus I'm getting unresolved import issues.

Version: Visual Studio Code Version 1.33.0
Python 2.7.14 (which is what I have to use in my venv)

Sadly, I can't post a screen shot of the issues due to proprietary restrictions.
However, I am able to tab over to the Terminal window, which is pointed to my venv and open python and import each of the libraries that the Problems view indicates are problems from the linting operation without issues.

These aren't pylint errors either they are "Python(unresolved-import)" issues. Neither pytlint, pep8, or flake8 report issues with those imports.

Seems similar to microsoft/vscode-python#3840 which was flagged as being closed, but doesn't appear to be resolved, and I can't post there as that issue is locked.

Could also be simlar to #541 which is 'closed' and #887 which made me spawn this issue.

I'll see if I can reproduce in more a basic way without proprietary libraries in the venv so that it can be fixed and add here with the details.

Activity

  1. dmayo77 commented on Apr 10, 2019

    @dmayo77
    Author

    How to reproduce:

    1. Create an egg for a library you want to distribute (see https://mrtopf.de/en/a-small-introduction-to-python-eggs/)
    2. Install that library
    3. Attempt to import that egg in your code.
    4. Look at the problems reported by VS Code

    Example:

    Create the following two files (eggfunc.py and setup.py) in the same folder somewhere deep possibly in your virtual env. (For example, my vevn was at 'c:\venv' and I put them in 'c:\venv\a\a\eggtest'

    eggfunc.py
    def eggfunc(x, y): return x*y

    setup.py
    '
    #!/usr/bin/env python

    a simple egg

    from setuptools import setup

    setup(
    name="the_egg",
    version="0.1",
    description="A simple egg",
    author='you',
    author_email="[email protected]",
    url="http://bogus/",
    packages=[""])
    '

    Then in your virtual environment, navigate to the folder where those two files, create an egg and then install it.

    python setup.py bdist_egg
    python setup.py install

    Note that the_egg-0.1-pyX.X.egg is now installed in the site_packages folder

    Now, in Visual Studio code, create a 'simpletest.py' file in the root of some other folder in your venv, making sure Visual Studio is setup to use that venv appropriately.

    simpletest.py
    `
    """ a basic test to track down import issue """
    from the_egg import eggfunc

    if name == 'main':
    print eggfunc.eggfunc(1, 2)
    `

    Note in the PROBLEMS output in Visual Studio code, you'll get:

    • Unable to import 'the_egg'
    • unresolved import 'the_egg'
    • Undefined variable: 'eggfunc'

    But if you go to the TERMINAL and type in 'python simpletest.py' the code will work just fine.

  2. dmayo77 commented on Apr 10, 2019

    @dmayo77
    Author

    Sorry about the formatting, but hopefully you get the gist.

  3. tab-cmd commented on Apr 15, 2019

    @tab-cmd

    Same happens to me on V1.33.1 and Python 2.7.10. Same output as #887

  4. charce36 commented on Apr 23, 2019

    @charce36

    I have the same problem, any solution ?

  5. jakebailey commented on Apr 23, 2019

    @jakebailey
    Member

    We don't yet support egg files (#196), so these types of imports aren't yet going to work.

  6. zgoda-mobica commented on Apr 24, 2019

    @zgoda-mobica

    I have the same symptoms in test modules. Package is installed in current conda environment with pip install -e ., symbols are perfectly importable using basic REPL yet I still get unresolved import reported and none of IS functions work.

    import pytest
    
    from mypkg.convert.fields import bool2s, none2empty, sq2two

    Package is installed properly, code runs fine. Normal code that does relative imports from source tree works fine too.

  7. charce36 commented on Apr 24, 2019

    @charce36

    I resolved installing Ubuntu 18.04

  8. jakebailey commented on Apr 24, 2019

    @jakebailey
    Member

    If these are all related to editable installs (I don't think they are, not completely), then we currently haven't worked that out yet either. #989

  9. OllieKampo commented on May 3, 2019

    @OllieKampo

    I also have a similar problem, if I try to import any other python file it always says "unresolved" even though when I run it it works just fine...
    Can someone just tell me how to disable this annoying green highlighting all over my code?

  10. jakebailey commented on May 3, 2019

    @jakebailey
    Member

    https://github.com/Microsoft/python-language-server#linting-options-diagnostics

    {
        "python.analysis.disabled": ["unresolved-import", "undefined-variable"]
    }
    

    But disabling the messages won't solve the problem of the language server not finding dependencies, so the analysis will not be any better. Your issue doesn't sound the same as this issue, if it's affecting every import (and not just editable installed ones), so if you have a test project and can make a new issue, that'd be appreciated.

  11. OllieKampo commented on May 4, 2019

    @OllieKampo

    Edit: Sorry I've just realised you said to make a new issue, if this is not a simple fix then I will do that, and delete this comment.

    Thank you for your reply Jake.
    Its hard to explain the exact issue and I don't know all the correct terminology, i'm afraid I was also a bit frustrated when I posted my original comment so apologies for that.
    I'm a PhD computer science student but my masters was in mechanical engineering so I sometimes make simple mistakes with this kind of stuff which may be the issue here.

    Say I have files setup like this: (I've obviously omitted folders and files you don't need to see)

    C:.
    │   Potassco.code-workspace
    │
    ├───.vscode
    │       settings.json
    │
    └───Work
        └───Hospitality_Domain
            └───Version_7
                │   Architecture.lp
                │   domain_setup.lp
                │   Parser.py
                │   Robot_Hotel.sp
                │   Simulate.py
                │   SPARC.jar
                │
                └───__pycache__
                        Parser.cpython-36.pyc
    

    If I try to import Parser.py into Simulate.py as such:
    import Parser
    I get the unresolved import problem but the code runs just fine (as we would expect).

    However if I instead put this:
    import Work.Hospitality_Domain.Version_7.Parser
    I do not get the unresolved import but the code does not run, (which is also what I would expect).

    Any ideas?

  12. chudytom commented on May 18, 2019

    @chudytom

    I get the unresolved import problem but the code runs just fine (as we would expect).

    However if I instead put this:
    import Work.Hospitality_Domain.Version_7.Parser
    I do not get the unresolved import but the code does not run, (which is also what I would expect).

    Any ideas?

    I have the very same problem. There is always either en error on the IntelliSense part or while running the code.

  13. OllieKampo commented on May 18, 2019

    @OllieKampo

    Tomasz Chudzik (@chudytom) Yes I am unsure what this "editable install" thing is as well and whether I am using it or not?
    I have not had a reply on this thread so I created a new issue https://github.com/microsoft/python-language-server/issues/1099.

  14. chudytom commented on May 18, 2019

    @chudytom

    Tomasz Chudzik (@chudytom) Yes I am unsure what this "editable install" thing is as well and whether I am using it or not?
    I have not had a reply on this thread so I created a new issue https://github.com/microsoft/python-language-server/issues/1099.

    Greate idea. Just please fix the link because when I click on it, it doesn't redirect me properly. I had to copy it manually.

  15. 29 remaining items

  16. mattlefevre commented on Jun 20, 2019

    @mattlefevre

    I've got a crazy amount of log information at this point (yay!), and I don't think pasting it all in here would work out (the initial loading of VSC is about 3500 lines). I'm not sure exactly what you need, Jake Bailey (@jakebailey) , but here's all of the initial informational logs:

    Starting Microsoft Python language server.
    Downloading https://pvsc.azureedge.net/python-language-server-daily/Python-Language-Server-win-x64.0.3.18.nupkg... ##########Linting Output - pylint##########
    complete
    Unpacking archive... ##########Linting Output - pylint##########
    done
    [Info  - 3:59:16 PM] Analysis cache path: C:\Users\mlefevre\AppData\Local\Microsoft\Python Language Server
    [Info  - 3:59:17 PM] GetCurrentSearchPaths C:\Users\mlefevre\.virtualenvs\ambersite-fSB87Qgi\Scripts\python.exe 
    [Info  - 3:59:17 PM] Interpreter search paths:
    [Info  - 3:59:17 PM]     \python
    [Info  - 3:59:17 PM]     \python\pyspark
    [Info  - 3:59:17 PM]     c:\users\mlefevre\.virtualenvs\ambersite-fsb87qgi\lib
    [Info  - 3:59:17 PM]     c:\users\mlefevre\.virtualenvs\ambersite-fsb87qgi\scripts
    [Info  - 3:59:17 PM]     c:\users\mlefevre\appdata\local\programs\python\python37-32\lib
    [Info  - 3:59:17 PM]     c:\users\mlefevre\appdata\local\programs\python\python37-32\dlls
    [Info  - 3:59:17 PM]     c:\users\mlefevre\.virtualenvs\ambersite-fsb87qgi
    [Info  - 3:59:17 PM]     c:\users\mlefevre\.virtualenvs\ambersite-fsb87qgi\lib\site-packages
    [Info  - 3:59:17 PM] User search paths:
    [Info  - 3:59:29 PM] Microsoft Python Language Server version 0.3.18.0
    [Info  - 3:59:29 PM] Initializing for C:\Users\mlefevre\.virtualenvs\ambersite-fSB87Qgi\Scripts\python.exe
    

    Were there any other specific logs I should include here? Or did you want me to attach the entire set of initial logs as a .txt file?

  17. jakebailey commented on Jun 20, 2019

    @jakebailey
    Member

    The entire thing, but for now I'm more concerned about what those weird \python \python\pyspark paths are. And are those logs without any extraPath configuration? ./core in that list should be making it show up in user search paths.

    I haven't yet had time to look at your case yet (filing other issues and making sure 0.3 is okay to go live), but will be doing so next.

  18. mattlefevre commented on Jun 20, 2019

    @mattlefevre

    I definitely have ./core in my extraPaths. Here are the last few lines of my settings file. Is that all set up right?

        "python.jediEnabled": false,
        "python.autoComplete.extraPaths": [
            "./core",
        ],
        "python.analysis.downloadChannel": "daily",
        "python.analysis.logLevel": "Trace",
    

    As far as the \python and \python\pyspark paths... I'm not sure what those are either?

  19. jakebailey commented on Jun 20, 2019

    @jakebailey
    Member

    You have that in your global user settings, or the workspace settings? The other three are fine in the user settings, but since ./core is a project-specific thing, I'd expect that to be in .vscode/settings.json.

  20. mattlefevre commented on Jun 20, 2019

    @mattlefevre

    Jake Bailey (@jakebailey) - Moving that to the workspace settings seems to have cleared everything up! I honestly didn't realize there were separate settings.... 🤦‍♂️

    Thank you! You're my hero!

  21. jakebailey commented on Jul 25, 2019

    @jakebailey
    Member

    This issue has been stale for more than a month; I'm going to close it.

    If you're still having unresolved import issues, please check the docs here to see if extraPaths or PYTHONPATH are enough for you: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings

    If you're still having issues after trying that, please open a new issue with your case so we can look at it individually. We know there is still more work to be done when it comes to our import system (#443, #1360, others), but it's difficult to handle the varying cases all in one place.

    Thanks!

  22. chiboreache commented on Aug 30, 2019

    @chiboreache

    +1

  23. stevenlis commented on Feb 28, 2020

    @stevenlis

    Jake Bailey (@jakebailey) The issue has always been with me...
    image

  24. jakebailey commented on Feb 28, 2020

    @jakebailey
    Member

    Please file a new issue. This issue is 9 months old and was about unresolved imports in user code; requests is a third party library.

  25. larsjuetten commented on Apr 22, 2020

    @larsjuetten

    I'm going through this and related issues, and for the most part you can fix things by setting extraPaths to tell us more directories to use as import roots, for example:

    {
        "python.autoComplete.extraPaths": [
            "./src"
        ]
    }

    If you have more folders, you can list them all, and things should work.

    However, this doesn't seem to work when a package gets installed as editable. I believe this has something to do with how the interpreter's own default search paths get modified and how we merge all of this configuration together. I'm currently just going through all of the test projects everyone has provided to get a good view on the unresolved imports first before trying to nail that one.

    This did exactly solve the problem I was facing.
    Multiple repos including python-files and vscode got somehow confused. Python-wise (debugging and such) it was working and vscode was able to follow my breakpoint. But my linter (flake8) was complaining about unresolved imports and I could not follow "Go To Definition F12" ...
    After introducing this extraPaths it works like charm! :)

    Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

editable-installspip editable installs / setup.py develop

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions