question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python unresolved import issue returns

See original GitHub issue

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: https://github.com/Microsoft/vscode-python/issues/3840

Workaround: https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-462977992 – 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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:23

github_iconTop GitHub Comments

1reaction
karrtikrcommented, 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.

1reaction
AlexLemnacommented, May 3, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python unresolved import: How to Solve Pylint Error
For the Python-specific imports, it resolves the unresolved import issue, but it won't resolve your own modules. When importing your own modules ...
Read more >
python - Pylint "unresolved import" error in Visual Studio Code
This issue was because of the Python interpreter. You need to select the interpreter in Visual Studio Code using Shift + Ctrl +...
Read more >
Fix Python Unresolved Import in VSCode - LinuxPip
"Unresolved Import" is an error message produced by VSCode, not Python itself. The message simply means that VSCode cannot detect the correct ...
Read more >
Unexpected error in python development: unresolved import ...
When developing a modular python code (with actual custom modules imported) you can use import __main__ from a module to extract data from...
Read more >
Re: [Pydev-code] unresolved import - SourceForge
After hunting around the context menu options for the full tree, I found that setting the immediate parent folder (python) in the tree...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found