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.

Debugger spuriously stops on exceptions when using python interpreter from a virtualenv

See original GitHub issue

Environment data

VS Code version: 1.19.1 Python Extension version: 0.9.1 Python Version: 2.7.14 OS and version: MacOs Sierra 10.12.6 Virtualenv version: 15.1.0

Actual behavior

With breaking on Uncaught Exceptions option enabled, the debugger breaks inside re module when using python interpreter coming from a virtual environment created by virtualenv. Hitting Continue button has no end, it keeps breaking inside re module, that happens to be indirectly imported in the first line of my code, so I can’t move forward with debugging without disabling the Uncaught Exceptions option. When using the same version of python interpreter, but installed globally, the debugger does not break inside re module, it progresses through the code and breaks on first uncaught exception in my code.

Expected behavior

Debugger should behave the same way, no matter if I use virtualenv or system wide version of python.

Steps to reproduce:

  • Create and activate virtualenv
mbp:~$ virtualenv code/python-venvs/testenv
New python executable in /Users/leszek/code/python-venvs/testenv/bin/python2.7
Also creating executable in /Users/leszek/code/python-venvs/testenv/bin/python
Installing setuptools, pip, wheel...done.

@mbp:~$ . code/python-venvs/testenv/bin/activate

(testenv) @mbp:~$ which python
/Users/leszek/code/python-venvs/testenv/bin/python

(testenv) @mbp:~$ python --version
Python 2.7.14
  • Create sample code:
(testenv) @mbp:~$ echo '#!/Users/leszek/code/python-venvs/testenv/bin/python

import json

print var' > code/python-venvs/testenv/src/test.py
  • Open the src dir in VSCode, then open the test.py file, and click “Set as interpreter” above the first line of code to activate the interpreter provided by the virtualenv.

  • Go to the debugger and click on the gear icon to populate launch.json with default settings.

  • Select Uncaught Exceptions on the list of breakpoints

  • Start the debugger with the Python configuration selected.

  • The debugger breaks inside sre_parse.py file, instead it should break on the 5th line of code where I’m referencing undefined variable var.

  • Keep clicking on Continue or just F5 on the keyboard, the debugger keeps breaking in the same place.

  • In VSCode, change the interpreter to the system wide one, in my case /usr/local/bin/python2, installed with brew:

(testenv) @mbp:~$ deactivate

@mbp:~$ which python2
/usr/local/bin/python2

@mbp:~$ /usr/local/bin/python2 --version
Python 2.7.14
  • Restart the debugger, it doesn’t break inside sre_parse.py file, instead it breaks on the 5th line of my code.

Logs

Output from Python output panel

Output from Console window (Help->Developer Tools menu)

extensionHost.ts:290 [Extension Host] debugger listening on port 9334
extensionHost.ts:200 [Extension Host] debugger inspector at %cDebugger listening on port 9334.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9334/739de81d-5f39-40fa-926c-e43873c6758f

console.ts:123 [Extension Host] (node:91038) DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
t.log @ console.ts:123

Settings

User settings:

{
    "editor.rulers": [79],
    "editor.fontSize": 11,
    "terminal.integrated.fontSize": 12,
    "editor.fontFamily": "Menlo, Monaco, 'Courier New', monospace",
    // "python.venvPath": "/Users/leszek/code/python-venvs",
    "python.linting.pylintPath": "/usr/local/bin/pylint",
    "editor.minimap.enabled": true,
    "window.zoomLevel": -1,
    // "debug.allowBreakpointsEverywhere": true
}

Workspace settings:

{
    "python.pythonPath": "/Users/leszek/code/python-venvs/testenv/bin/python"
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput"
            ]
        },
        ... skipping the rest here

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
yozletcommented, Jun 5, 2018

I ran into this problem when trying to debug a custom Django management command running in python 3.6.5 under pipenv. Switching to the experimental debugger fixed it for me. Thank you!

0reactions
DonJayamannecommented, Sep 12, 2018

Closing as the new release is out with the fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is Python debug using correct virtual environment but ...
When I open this code and have Python Interpreter as Global or venv1 , "requests" is underlined (could not be resolved). When I...
Read more >
PyCharm Educational 183.4588.180 Release Notes
Feature PY‑5138 Link to source code from .po files Feature PY‑3454 Highlight HTTP links in reStructuredText Feature PY‑3451 Support Structure View for reStructuredText Feature PY‑2239 Custom...
Read more >
Debugging Python with a virtual environment in VSCode
In VS Code open the Command Palette (⇧ ⌘ P). Then select “Python: Select Interpreter".
Read more >
PyDev Releases
Terminate remote debugger without any process attached no longer throws an exception. PyDev debugger threads are hidden in the threading module. Line validation ......
Read more >
Exception handing and debugging in Python - Index of /
Exceptions are raised by different kinds of errors arising when executing Python code. In your own code, you may also catch errors, or...
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