-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Unresolved import if sys.path modified in __init__.py #6241
Comments
@farleylai, thanks for letting us know about this. If I understood correctly, To get what you want, the extension would have to automatically add
As to the first one, this situation is not unique to the extension. I recommend using one of the existing approaches out there. |
@ericsnowcurrently
This alternative I mentioned is to insert the path in
I am saying the programmer has done this such that whenever
So my first intuition is to have the extension to evaluate
|
Thanks for clarifying, @farleylai. I understand now and was able to quickly reproduce the problem. Based on the information you've provided thus far I was able to fill in the blanks for file content, which I've included below. Using Jedi ( You are correct that the sys.path manipulation in # main.py
from mypackageA import app
app.App()
# mypackageA/__init__.py
import os.path
import sys
PKG_ROOT = os.path.dirname(__file__)
PROJECT_ROOT = os.path.dirname(PKG_ROOT)
LIB = os.path.join(PROJECT_ROOT, 'external repo', 'lib')
sys.path.insert(0, LIB)
# mypackageA/app.py
from config import Config # <--- error marked here
class App:
def __init__(self):
cfg = Config()
...
# external repo/lib/config.py
class Config:
... |
Thanks for your timely confirmation. |
Hello @ericsnowcurrently , I have a similar issue that may add value to this conversation. I have a simple project setup
I am trying to import Repo Location: https://github.com/rrmistry/vscode-python-relative-import [EDIT]: I discovered the root cause. Please see comment #6241 (comment) |
Hello @ericsnowcurrently , please disregard my previous comment. The issue I faced was trivial. The IPython Kernel's current working directory was on the Project root instead of my module sub-folder. I discovered the correct approach upon importing a working jupyter notebook using the extension. For reference, what is required is to add the following to the begining of #%% Change working directory from the workspace root to the .py file location.
import os
try:
os.chdir(os.path.join(os.getcwd(), 'mymodule')) # change directory
print(os.getcwd())
except:
pass # ignore error in-case re-running this cell
# (e.g. new cwd = `Project root/mymodule/mymodule`) If no objections, I'll leave these comments here for anyone else who may run into the same issue. Thanks, |
It is unlikely that LS will be actually executing Python code to get |
In my case the problem was solved replacing
for
in my That's a little odd but it worked |
@MikhailArkhipov @nunesvictor |
I have a bit similar situation, my use case is that I am developing a polyglot monorepo containing multiple AWS Lambda functions. Depending on the use case, they are written in Go, TypeScript and Python. I try to keep all the functions "contained" in such a way that the folder containing the AWS Lambda function source code has the manifest-file ( I've initialized each of the Python AWS Lambda functions with their own virtual environment Then in the AWS Lambda source code stored in import logging as log
log.getLogger().setLevel(log.DEBUG)
from pathlib import Path
path = Path(__file__).parent.absolute()
sys.path.insert(0, str(path) + "/.venv/lib/python3.7/site-packages")
log.debug("sys.path:" + str(sys.path))
# After this I import some local dependencies from .venv
import boto3 # Local boto3 version imported as I require newer version than the one in Lambda runtime environment
import cfnresponse
# ... and possibly many more depending on the use case. Where of course the This works fine for the local development in The problem is that VS Code linter and intellisense doesn't keep up with that solution. I can get the VS Code linter & intellisense working by defining in PYTHONPATH=src/lambda/<function-name>/.venv/lib/python3.7/site-packages The problem here is that I may have several of these functions, let's say PYTHONPATH=src/lambda/foo/.venv/lib/python3.7/site-packages:src/lambda/bar/.venv/lib/python3.7/site-packages … to get the dependencies of Unfortunately this becomes problematic as I might have quite many different AWS Lambda functions, so I'd have to define all the different AWS Lambda source code folders into So it'd be nice if there was a singular method for defining the dependencies path for these Python AWS Lambda functions, that would work for the actual code (in AWS Lambda & in |
Parsing source to extract |
For projects that include external repos, it would be handy to insert the relative dependency paths ONLY when importing the necessary module.
Here is an example project layout:
In
main.py
, when anapp
instance that depends on theConfig.py
module in the external repo is created, the sys.path must includeexternal repo/lib
at the time of importingapp
:Instead of setting the PYTHONPATH statically, an alternative is to modify
sys.path
inmypackageA.__init__.py
by inserting paths toexternal repo/lib
for theapp
module to importConfig
. However, vscode showsunresolved import
warnings in this use case.The text was updated successfully, but these errors were encountered: