Python unresolved import issue returns
See original GitHub issueEnvironment 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:
- Created 4 years ago
- Reactions:3
- Comments:23
Top GitHub Comments
You can either shorten
PYTHONPATH
and make core folder a module like you said. (Although remember to change imports toimport 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 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.
Basically, I had
PYTHONPATH = ./core
in my.env
file instead ofPYTHONPATH = ./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 mycore\
folder? Or would I just shortenPYTHONPATH
back toPYTHONPATH = ./core
and add an__init__.py
file tocore\
?(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.)