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.

Unresolved import if sys.path modified in __init__.py

See original GitHub issue

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:

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:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
aripalocommented, Oct 21, 2019

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 with lambci/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 with pip install --upgrade -r requirements.txt (these steps I actually execute inside lambci/lambda Docker containers).

Then in the AWS Lambda source code stored in src/lambda/<function-name>/main.py I have the following:

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 <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:

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 foo and bar. Of course one can set multiple folders into PYTHONPATH separated by os.pathsep, which would mean I’d have to configure:

PYTHONPATH=src/lambda/foo/.venv/lib/python3.7/site-packages:src/lambda/bar/.venv/lib/python3.7/site-packages

… to get the dependencies of foo and bar 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.

1reaction
nunesvictorcommented, Sep 20, 2019

In my case the problem was solved replacing

"python.autoComplete.extraPaths": [
    "${workspaceFolder}/src"
],

for

"python.autoComplete.extraPaths": [
    "./src"
],

in my .vscode/settings.json file.

That’s a little odd but it worked

Read more comments on GitHub >

github_iconTop 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 >

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