python-language-server icon indicating copy to clipboard operation
python-language-server copied to clipboard

Python Unresolved import issue

Open dmayo77 opened this issue 7 years ago • 49 comments

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 https://github.com/Microsoft/vscode-python/issues/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 https://github.com/Microsoft/python-language-server/issues/541 which is 'closed' and https://github.com/Microsoft/python-language-server/issues/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.

dmayo77 avatar Apr 10 '19 15:04 dmayo77

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.

dmayo77 avatar Apr 10 '19 17:04 dmayo77

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

dmayo77 avatar Apr 10 '19 17:04 dmayo77

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

tab-cmd avatar Apr 15 '19 22:04 tab-cmd

I have the same problem, any solution ?

charce36 avatar Apr 23 '19 19:04 charce36

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

jakebailey avatar Apr 23 '19 19:04 jakebailey

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.

zgoda-mobica avatar Apr 24 '19 13:04 zgoda-mobica

I resolved installing Ubuntu 18.04

charce36 avatar Apr 24 '19 15:04 charce36

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

jakebailey avatar Apr 24 '19 16:04 jakebailey

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?

OllieKampo avatar May 03 '19 19:05 OllieKampo

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.

jakebailey avatar May 03 '19 19:05 jakebailey

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?

OllieKampo avatar May 04 '19 12:05 OllieKampo

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.

chudytom avatar May 18 '19 11:05 chudytom

@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.

OllieKampo avatar May 18 '19 15:05 OllieKampo

@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.

chudytom avatar May 18 '19 15:05 chudytom

I have the same problem, V1.34.0, python3.7.2, MacOS 10.14.5

sabretus avatar May 23 '19 08:05 sabretus

I try this extension every 6 months but bugs like this are pervasive. I have disabled all 6 available linters and these kinds of squiggly line errors which aren't really errors continue to arise. I would love a way to completely disable all visual notes in vscode from the python extension. Sadly, another six months disabling the python extension entirely.

mikemac8888 avatar May 30 '19 13:05 mikemac8888

My previous comment in the thread shows how to disable these messages: https://github.com/microsoft/python-language-server/issues/918#issuecomment-489214816

If you don't want anything from the LS, you can disable any of the ones listed in our README, though note that "unresolved import" is special in that it means that the LS wasn't able to find an import, so the analysis quality will suffer (hence why we show it).

I'm going to be looking into editable installs next, and a ZenHub epic which links all the issues together is here: #1139 (though I'm not sure how egg files will play into that)

jakebailey avatar May 30 '19 15:05 jakebailey

I'm going to be looking into editable installs next, and a ZenHub epic which links all the issues together is here: #1139 (though I'm not sure how egg files will play into that)

Great news! I appreciate the work you guys do, but this import issue is a deal breaker for quite a few of us, I believe.

egabrum avatar May 30 '19 17:05 egabrum

Still happening to me on v1.35.1 / Python 3.7 / Linux

  • > reload window fixes it. Confirmed (first time it was after killing a python process that was taking 100% CPU, but it seems unrelated)

gatopeich avatar May 31 '19 09:05 gatopeich

The 100% usage from the python process is likely unrelated; if it happens again I'd appreciate it if you could use ps or top to find the arguments given to that script and open a new issue for us to look at (so we can figure out which module is hanging when being inspected).

jakebailey avatar May 31 '19 16:05 jakebailey