Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code debugging always gives ImportError on internal modules in Python

I answered my own question, see solution below. Leaving this all here for posterity since I did quite a lot that others had said would work and had worked for them, yet none of it worked.

Problem

Below is a very simple python file. When trying to use Visual Studio Code's debugger on this file, I always get an ImportError on any internal module, like app.models or app.tests.scripts.stubs. If those modules are listed below faker, or any other external module, the debugger will go past the external module without throwing an error, but will throw an error on app.models or whatever internal module is listed first. This is true in the 8-10 files I've tried this in.

What I've tried

I've specified the exact pythonPath in both launch.json and User Settings, as below. I've added "exceptionHandling" to ignore ImportErrors. I've uninstalled and reinstalled Don Jayamanne's excellent Python extension. I've ran the Python: Select Workspace Interpreter and pointed it to ./.venv/bin/python2.7, ./.venv/bin/python2, and ``./.venv/bin/python`.

Files

launch.json Python configuration

    "name": "Python",
    "type": "python",
    "request": "launch",
    "stopOnEntry": false,
    "pythonPath": "/Users/REDACTED/REDACTED/.venv/bin/python2.7",
    "program": "${file}",
    "cwd": "${workspaceRoot}",
    "env": null,
    # we have 10+ environmental shell scripts, so I can't really use "envFile" in
    # a meaningful way, though I have tried pointing it at one of the shell scripts
    # but was still unable to debug.
    "envFile": "${workspaceRoot}/.venv",
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
    ],
    "exceptionHandling": {
        "ignore": [
            "ImportError"
        ]
    }
},

Python File

from app.models import Kelly
import app.tests.scripts.stubs
from faker import Faker
import factory
from datetime import datetime
from bson.objectid import ObjectId

fake = Faker()

kelly_names = [REDACTED]


class KellyFactory(factory.Factory):
    class Meta:
        model = Kelly
    id = ObjectId()
    is_archived = False
    email = factory.LazyAttribute(
        lambda kel: '%[email protected]' % kel.name.split()[0])
    name = fake.word(ext_word_list=kelly_names)
    phone = fake.phone_number()
    date_last_modified = datetime.now()


kelly = KellyFactory()
like image 849
Jason Avatar asked Oct 29 '22 02:10

Jason


1 Answers

Solution

While walking through everything I've done, to post this question, I tried to change the path program from "${file}" to it's absolute path. That allowed the debugger to get beyond the imports without errors. Then I had an issue because I wasn't in any environment so I entered a dev environment on my machine, though I didn't have to change anything else in VSCode. However, I'm now having an unverified breakpoint issue and am hunting down that issue. Will update once I figure that out.

What I've tried to solve Unverified Breakpoint issue

I changed "program" back to "${file}", and debugging works exactly as expected, with breakpoints working, on a super-simple file like this

import sys
print(sys.version)
print(sys.executable)

However that results in the ImportError again on a more complicated file like the Python File listed above in the original question, despite the "exceptionHandling" specified in launch.json.

like image 113
Jason Avatar answered Dec 22 '22 09:12

Jason