Skip to content

Debugging a script run from within a virtual environment's directory does not work #2520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Reyshawn opened this issue Sep 7, 2018 · 9 comments
Assignees
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug

Comments

@Reyshawn
Copy link

Reyshawn commented Sep 7, 2018

Environment data

  • VS Code version: 1.27.1
  • Extension version (available under the Extensions sidebar): Python 2018.8.0
  • OS and version: macOS HighSierra 10.13.6
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.0
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: XXX

Actual behavior

When I set a breakpoint in the venv environment, and click the debug, the runpy.py file popped up and the program stopped at line 85.

Expected behavior

The program should have stopped at the line where I set the breakpoint.

Steps to reproduce:

  1. python3 -m venv test
  2. Open the test folder and change the workspace settings "python.pythonPath": "${workspaceFolder}/bin/python3"
  3. Then try to write some python code, for example,
a = 1
b = 2

print(a+b)

set a breakpoint at line 2, click the debug, a runpy.py file popped up and the program stops at line 85: exec(code, run_globals), as the following image shows.

screen shot 2018-09-07 at 09 54 40

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Nothing

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)

The following statements are executed in the integrated terminal :

[reyshawn:~/Desktop/test]$ cd /Users/reyshawn/Desktop/test ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" "PYTHONPATH=/Users/reyshawn/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd" /Users/reyshawn/Desktop/test/bin/python3 -m ptvsd --host localhost --port 49846 /Users/reyshawn/Desktop/test/a.py

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: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost"
        },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "console": "integratedTerminal",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "django": true
        },
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        },
        {
            "name": "Python: Current File (External Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal"
        }
    ]
}
@d3r3kk d3r3kk added bug Issue identified by VS Code Team member as probable bug needs verification area-debugging info-needed Issue requires more information from poster and removed needs verification labels Sep 7, 2018
@d3r3kk
Copy link

d3r3kk commented Sep 7, 2018

@primes2h Thanks for the report.

Can you run your python script from the command line? From the interactive shell prompt within VSCode?

@d3r3kk d3r3kk changed the title Can't hit the breakpoint in the venv environment Debugging stops in runpy.py before getting into intended script. Sep 7, 2018
@primes2h
Copy link

primes2h commented Sep 7, 2018

@d3r3kk I'm sorry, I can't right now (until Monday). :-(

Can someone else do it in the meantime please?

@Reyshawn
Copy link
Author

Reyshawn commented Sep 8, 2018

@d3r3kk I can run my python script from the command line, and also from the shell prompt within VSCode. When I use pdb to debug my python script, it works well. However, the same issue occurs repeatedly ( runpy.py file popped up and script stopped ) when I click the debug button in VSCode. I don't know if there's something wrong in the launch.json.

@Reyshawn
Copy link
Author

Reyshawn commented Sep 8, 2018

@d3r3kk The debugger will stop at the breakpoint in the last version of ms-python. ( version 2018.7.1 ) However, once I import some packages, similar issues occurs: sre_parse.py file popped up and the script stopped at line 167.

@brettcannon brettcannon added needs verification and removed info-needed Issue requires more information from poster labels Sep 10, 2018
@d3r3kk
Copy link

d3r3kk commented Sep 27, 2018

I think I understand the trouble now, and I can indeed reproduce undesirable behaviour, although slightly different in how it presents itself on my machine.

The file that @Reyshawn is debugging has been placed inside the virtual environment directory itself. I believe this is unexpected but I'm not sure if it is invalid or not, @brettcannon?

For now, please do this to set up your environment and let us know if it starts working for you or not:

  1. Create a new folder to begin working from, something like /Users/username/projects/my_project.
  2. Open a terminal and enter this directory. cd /Users/username/projects/my_project
  3. Create the virtual environment, call it what you like, but something like .venv is commonplace. python3 -m venv .venv.
  4. Activate your virtual environment. . .venv/bin/activate (on Windows, .venv/Scripts/Activate.ps1).
  5. Open VSCode in your project folder. code .
  6. Select the python interpreter for your project in VSCode by clicking on the status bar, or hitting F1 and typing Python: Select interpreter. Be sure to select the one in the .venv folder.
  7. Create your script in the root directory of your project as a sibling to the virtual environment folder.
  8. Add the following code to your script:
a = 1
b = 2   # put breakpoint here

print(a+b)
  1. Start debugging.

Does this workaround get you running?

@d3r3kk d3r3kk added info-needed Issue requires more information from poster and removed needs verification labels Sep 27, 2018
@brettcannon brettcannon added needs PR and removed info-needed Issue requires more information from poster labels Sep 28, 2018
@brettcannon
Copy link
Member

I can't think of any reason why this scenario shouldn't be supported, but it isn't typical so I'm not sure when we will prioritize fixing it.

@brettcannon brettcannon changed the title Debugging stops in runpy.py before getting into intended script. Debugging a script run from within a virtual environment's directory does not work Sep 28, 2018
@madig
Copy link

madig commented Dec 4, 2018

I think I'm hitting the same problem, only that I'm running a script from the root directory (interpreter is set to the venv) and have a breakpoint set in a venv'ed library. The debugger does not delve into the venv'ed library.

@OliveIT
Copy link

OliveIT commented Jan 23, 2019

I faced the same issue. Any updates?

@brettcannon
Copy link
Member

To debug into third-party code (which is anything inside a venv), set "debugStdlib": true in your launch.json. We are going to be getting a better name and improving the support in #2087.

@ghost ghost removed the needs PR label Feb 14, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

6 participants