Skip to content

Python unresolved import issue returns #5518

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

Closed
roubles opened this issue Apr 30, 2019 · 23 comments
Closed

Python unresolved import issue returns #5518

roubles opened this issue Apr 30, 2019 · 23 comments
Assignees
Labels
info-needed Issue requires more information from poster

Comments

@roubles
Copy link

roubles commented Apr 30, 2019

Environment data

  • VSCode Version: 1.33.1
  • Extension version (available under the Extensions sidebar): 2019.4.11987
  • OS and version: Ubuntu 18.04
  • Python version (& distribution if applicable, e.g. Anaconda): 3.6
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): virtualenv

Description

My code references imports from a virtual environment as follows:

from commonpy.utils.foo import bar

In my settings.json I have my pythonPath setup to use that virtualenv:

{
    "python.pythonPath": "/home/rouble/.virtualenvs/vanilla/bin/python3"
}

The virtualenv is setup correctly, and my code runs from the command line. However, within vscode I get "unresolved import" warnings for any imports from the virtualenv.

I have tried the workaround listed on this, now closed, defect: #3840

Workaround: #3840 (comment) -- No dice.

Expected Behavior

No 'unresolved import' warnings since the libraries exist in the virtualenv and the code runs fine on the command line.

Actual behaviour

Tonnes of 'unresolved import' warnings

Steps to reproduce:

Write code that references imports from a virtualenv virtual environment as follows:

from commonpy.utils.foo import bar

In your settings.json set your pythonPath to use that virtualenv:

{
    "python.pythonPath": "/home/rouble/.virtualenvs/vanilla/bin/python3"
}

Thats it. This very setup works on mac, and does not work on linux.

@ghost ghost added the triage-needed Needs assignment to the proper sub-team label Apr 30, 2019
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Apr 30, 2019
@karrtikr
Copy link

Please inform if you are using Language server or jedi, i.e check the value of setting python.jediEnabled in settings.json.

@karrtikr karrtikr added the info-needed Issue requires more information from poster label Apr 30, 2019
@karrtikr
Copy link

karrtikr commented May 2, 2019

@AlexLemna Please inform if you are using Language server or jedi, i.e check the value of setting python.jediEnabled in settings.json.

@roubles
Copy link
Author

roubles commented May 3, 2019

python.jediEnabled is not present in the settings.json. So I believe it defaults to true, which is, jedi as the IntelliSense engine.

@AlexLemna
Copy link

I'm really inexperienced and it's possible I've set something up wrong, but I'm getting similar errors. @karrtikr python.jediEnabled is not present in my settings.json file. I've tried to provide details below - let me know if I need to add more.

Environment data:

  • VSCode Version: 1.33.1 (user setup)
  • Extension version (available under the Extensions sidebar): 2019.4.12954
  • OS and version: Windows_NT x64 10.0.17763
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.3 64-bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv

Description

My files are in a directory like this:

Logic\
     __init__.py
    LogicController.py
    RandomName.py
    RepeatFunction,py
rosevomit.py

where my main file rosevomit.py imports LogicController.py and LogicController.py imports RandomName.py and RepeatFunction.py. Both rosevomit and LogicController run fine as __main__, but Visual Studio Code gives me warnings about importing RandomName and RepeatFunction. Here's a screenshot:

ImportError

For what it's worth, I get similar (but slightly different?) errors in regular Visual Studio.

UnresolvedImportError

Either way, the code runs fine! It's just weird.

@karrtikr
Copy link

karrtikr commented May 3, 2019

No worries, i'll help you do it. @roubles @AlexaLemma Can both of you please provide the following logs mentioned in the template:

  • Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

  • Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)

@karrtikr
Copy link

karrtikr commented May 3, 2019

@AlexLemna As for your issue,

Please check this comment and this solution.

Basically python doesn't know where to look for modules by default, when you import. You have to specify the folder, in your case you need to create an .env file with content PYTHONPATH = ./Logic, to tell python to look for modules in that folder.

@AlexLemna
Copy link

@karrtikr Thanks, that fixed it! I had tried the suggestions in those comments earlier, but I made the mistake of literally copy-pasting from those suggestions instead of adjusting it for my project's actual directory structure.

rosevomit\
    .vscode\
    core\
        Logic\
             __init__.py
            LogicController.py
            RandomName.py
            RepeatFunction,py
        rosevomit.py
    .gitignore
    foo.env

Basically, I had PYTHONPATH = ./core in my .env file instead of PYTHONPATH = ./core/Logic. Like you said, adding the Logic folder to the path fixed it.

Just out of curiosity - will I need to add a second PYTHONPATH statement if I add another folder with modules to my core\ folder? Or would I just shorten PYTHONPATH back to PYTHONPATH = ./core and add an __init__.py file to core\?

(Also - do you still need my logs now that my issue is resolved? Sorry for not including them - I'll check the template next time.)

@karrtikr
Copy link

karrtikr commented May 4, 2019

Just out of curiosity - will I need to add a second PYTHONPATH statement if I add another folder with modules to my core\ folder? Or would I just shorten PYTHONPATH back to PYTHONPATH = ./core and add an init.py file to core?

  • You can either shorten PYTHONPATH and make core folder a module like you said. (Although remember to change imports to import Logic.RandomName for it to work)

  • You can also concatenate the same PYTHONPATH env variable to contain more module directories. See this.
    In your case, PYTHONPATH=./mymodule:${PYTHONPATH} should work.

Although remember, the way of concatenating environment variables is platform dependent. See this.

@karrtikr
Copy link

karrtikr commented May 4, 2019

@roubles Please confirm if you have PYTHONPATH env specified to search for commonpy. Also, please send the logs.

@roubles
Copy link
Author

roubles commented May 15, 2019

Ok, so here is what I have. Under .vscode/settings.json

{
    "python.pythonPath": "/home/rouble/.virtualenvs/vanilla/bin/python3",
    "python.envFile": "${workspaceRoot}/.env"
}

Let me know if any of this is not needed or superfluous.

In my project root folder I have .env

PYTHONPATH=~/.virtualenvs/vanilla/lib/python3.6/site-packages:${PYTHONPATH}

Here are the logs from Output panel:

Starting Microsoft Python language server.
[Info  - 10:55:04 PM] GetCurrentSearchPaths /home/rouble/.virtualenvs/vanilla/bin/python3 
[Info  - 10:55:04 PM] Python search paths:
[Info  - 10:55:04 PM]     /home/rouble/.virtualenvs/vanilla/lib/python3.6
[Info  - 10:55:04 PM]     /home/rouble/.virtualenvs/vanilla/lib/python3.6/lib-dynload
[Info  - 10:55:04 PM]     /usr/lib/python3.6
[Info  - 10:55:04 PM]     /home/rouble/.virtualenvs/vanilla/lib/python3.6/site-packages
[Info  - 10:55:04 PM] Configuration search paths:
[Info  - 10:55:04 PM]     /home/rouble/PranabData/git/vanilla/~/.virtualenvs/vanilla/lib/python3.6/site-packages
[Info  - 10:55:05 PM] Microsoft Python Language Server version 0.2.74.0
[Info  - 10:55:05 PM] Initializing for /home/rouble/.virtualenvs/vanilla/bin/python3

For now, I am going back to PyCharm. This usecase works out of the box in PyCharm, and it should work of the box in VSCode as well.

@karrtikr
Copy link

karrtikr commented May 15, 2019

Can you please also provide details for commonpy module here?
from commonpy.utils.foo import bar
Where is it located? And if PYTHONPATH is appended with that location.

@potens1
Copy link

potens1 commented May 19, 2019

Hi, I don't want to hijack the thread, but I've the same problem, and I don't get (at all) the comments about the PYTHONPATH, in my case I use pipenv, but should be the same as author virtualenv, the 3rd party libraries in the environment are detected, so the PYTHONPATH should be ok and my own package is installed in the env (pip(env)-e .). I can open a new bug but it sounds to me that the same (2months long, now) bug. LanguageServer or jedi have the same problem (tried both)

@karrtikr
Copy link

Hi @potens1 ,

the 3rd party libraries in the environment are detected, so the PYTHONPATH should be ok

You have to explicitly set PYTHONPATH in .env for any operation in VSCODE that depends on Python resolving modules.
So i have to ask you, do you have an .env file with PYTHONPATH specified to the location of 3rd party libraries you have to use? If not, i can help you set one up for your purpose.

In that case, I would like if you could please open up on a new issue specifying your details separately.

@potens1
Copy link

potens1 commented May 21, 2019

Hi, thx for your answer,
Opened a new bug in #5703 since I found it append (or seems to) only when packages installed in dev mode

@dibyendumajumdar
Copy link

dibyendumajumdar commented May 21, 2019

Hi, I have a very simple setup where I have a bunch of Python files in the same directory, but importing one gives the same error reported here. Works fine when I run the program. It seems strange that files in the same folder cannot be imported. PyCharm by the way has no problem with it.

Update: I had to add .env file in the project folder and define PYTHONPATH
I don't understand why this is necessary when I opened a folder containing a bunch of source files. Kind of defeats the idea of opening a folder and starting to work. The fact that you need to do this isn't mentioned in getting started either.

@karrtikr
Copy link

Hi @dibyendumajumdar , there's a open issue #5710 to track this. Will be addressed soon enough.

Current scenario is that we have to explicitly set PYTHONPATH in .env for any operation in VSCODE that depends on Python resolving modules, for the reasons I mentioned earlier.

@dibyendumajumdar
Copy link

@karrtikr Sure but I shouldn't need to get this info from an issue - strange that all the videos and articles about getting started with Python don't mention this upfront. Maybe because everyone demos editing a single file.

@karrtikr
Copy link

Yes, i understand. Ideally you should not need to get this info from an issue.
The intent of the issue is to address this problem that it should be in Getting started.

@micimize
Copy link

micimize commented Jun 6, 2019

I have been unable to get .venv import resolution with a variety of setups, mostly trying with 2019.5.18678.
Most of my attempts have been in a multi-root context, and I am using poetry. I am unsure of how to tell if they are failing due to misconfiguration or bugs. Here a paraphrase of my latest failed attempt, modeled on the above:

  • workspace json:
{
  "folders": [{ "path": "a" }, { "path": "b" }],
  "settings": {
    "python.pythonPath": "${workspaceFolder}/.venv/bin/python",
    "python.envFile": "${workspaceRoot}/.env"
  }
}
  • workspace root .env:
PYTHONPATH=/Abs/path/to/a/.venv/lib/python3.7/site-packages/:/Abs/path/to/b/.venv/lib/python3.7/site-packages/:${PYTHONPATH}

I know that before yesterday I had a setup where each folder simply had a .vscode/settings.json pointing to their interpreter with a relative path, and that worked for following everything but relative imports. I tried to fix relative imports, and now the old setup doesn't work either, which is maybe the main reason I'm convinced it's a bug. Now following imports doesn't work for anything, although mypy still seems to know how to complain, and I don't get module not found linting(?) errors.

I've tried downgrading, using absolute paths, using a .env per folder, etc. But I really don't even know what folders in the venv should be added to the PYTHONPATH

@micimize
Copy link

micimize commented Jun 6, 2019

Ok! I got everything to work with the latest by enabling jedi in user settings ("python.jediEnabled": true)

For my multi-root environment, this is the setup that ended up working with jedi (including go-to definition, analysis, autocomplete):

Functioning Multi-Root setup with jediEnabled:

  • workspace json:
{
  "folders": [{ "path": "package-a" }, { "path": "package-b" }],
  "settings": {
    "python.pythonPath": "${workspaceFolder}/.venv/bin/python",
    "python.envFile": "${workspaceRoot}/.env",
  }
}
  • workspace root .env:
PYTHONPATH=../package-a/package_a:../package-b/package_b:${PYTHONPATH}

notice that the paths will be resolved from the folder level.

@karrtikr
Copy link

karrtikr commented Jun 6, 2019

@micimize Glad you got it working for now. Although, it should be working with python.jediEnabled: false as well if it works with true. I would recommend opening up a new issue to work on it if you're interested.

@karrtikr
Copy link

karrtikr commented Jun 6, 2019

If the solution stated in the thread does not work for people, please to open up a new separate issue, as issues could be unrelated.

@karrtikr
Copy link

Closing until further info is provided

@ghost ghost removed the triage label Jun 18, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

7 participants