pythonvisual-studio-codeimportairflowpythonpath

Import error in VSCode despite setting the PYTHONPATH


I have an airflow environment locally deployed on WSL, and I am using VScode to debug and code.

My app folder structure is as follows:

~/workspaces
|--- .env
|---organization/gcp/datalake
    |--- dags
        |--- //My dags
    |--- plugins
        |--- __init.py__
        |--- operators
            |--- __init.py__
            |--- facebook_operators.py
        |--- hooks
            |--- __init.py__
            |--- facebook_hooks.py

I am having trouble understanding the behavior of VSCode regarding the imports.

I added the dags and plugins folders to the PYTHONPATH via .env file. My VSCode is opened directly on the workspaces directory.

The problem :

I get import errors, although I can successfully go to definition of the class I want to import.

Example : In my facebook_operators.py

from hooks.facebook_hooks import FacebookAdsHook raises the following error :

No name 'facebook_hooks' in module 'hooks'

The contents of my .env file:

PROJECTDIR=~/workspaces/organization/gcp/datalake
PYTHONPATH=${PROJECTDIR}/plugins
PYTHONPATH=${PROJECTDIR}/dags:${PYTHONPATH}

Where did I go wrong? I'd like to understand and solve this error please.


Solution

  • You need to add "python.analysis.extraPaths" to your .vsode/settings.json file like so:

    {
      "editor.formatOnSave": true,
      "python.analysis.extraPaths": ["./airflow/plugins"]
    }