Unresolved import if sys.path modified in __init__.py
See original GitHub issueFor 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:
Project root
+-- external repo/
+-- lib/
+-- config # defines Config class
+-- utils/
+-- mypackageA
+-- __init__.py
+-- app.py
main.py
In main.py
, when an app
instance that depends on the Config.py
module in the external repo is created, the sys.path must include external repo/lib
at the time of importing app
:
from mypackageA import app
app.App()
Instead of setting the PYTHONPATH statically, an alternative is to modify sys.path
in mypackageA.__init__.py
by inserting paths to external repo/lib
for the app
module to import Config
. However, vscode shows unresolved import
warnings in this use case.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (3 by maintainers)
Top Results From Across the Web
PyLint "Unable to import" error - how to set PYTHONPATH?
There are two options I'm aware of. One, change the PYTHONPATH environment variable to include the directory above your module.
Read more >Understanding Python imports, __init__.py and pythonpath
The python interpreter tries to look for the directory containing the module we are trying to import in sys.path . It is a...
Read more >Python Import Error (ModuleNotFoundError)
An ImportError is detected when Python has problems with a successful module import. Usually this problem is caused by the incorrect path and...
Read more >e) after installing before restart due to .egg-link and new ...
Unresolved references to editable packages (pip install -e) after installing before restart due to .egg-link and new entries in sys.path.
Read more >The definitive guide to Python import statements
All of the objects defined in the module or the package's __init__.py file are made available to the importer. Basics of the Python...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 (
requirements.txt
in Python’s case) and all its dependencies within it. This works well for AWS Lambda as it requires having all the dependencies bundled in the deployment package and also for local development withlambci/lambda
Docker.I’ve initialized each of the Python AWS Lambda functions with their own virtual environment
python3 -m venv .venv
and then installed vendor dependencies withpip install --upgrade -r requirements.txt
(these steps I actually execute insidelambci/lambda
Docker containers).Then in the AWS Lambda source code stored in
src/lambda/<function-name>/main.py
I have the following:Where of course the
<function-name>
is replaced with the actual function name.This works fine for the local development in
lambci/lambda
Docker and in actual AWS Lambda environment.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
${workspaceFolder}/.env
file the following:The problem here is that I may have several of these functions, let’s say
foo
andbar
. Of course one can set multiple folders intoPYTHONPATH
separated byos.pathsep
, which would mean I’d have to configure:… to get the dependencies of
foo
andbar
AWS Lambda functions to work in VS Code.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
PYTHONPATH
which will become tedious task.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
lambci/lambda
) and in VS Code.In my case the problem was solved replacing
for
in my
.vscode/settings.json
file.That’s a little odd but it worked