Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

How to correctly set specific module to debug in VS code? #2088

Closed
brando90 opened this issue Feb 18, 2020 · 8 comments
Closed

How to correctly set specific module to debug in VS code? #2088

brando90 opened this issue Feb 18, 2020 · 8 comments

Comments

@brando90
Copy link

Environment data

  • PTVSD version: XXX
  • OS and version: XXX
  • Python version (& distribution if applicable, e.g. Anaconda): XXX
  • Using VS Code or Visual Studio:

Version: 1.42.1
Commit: c47d83b293181d9be64f27ff093689e8e7aed054
Date: 2020-02-11T14:44:27.652Z
Electron: 6.1.6
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Darwin x64 19.3.0

Actual behavior

Does nothing

Expected behavior

Run the file I said no matter what file I am on.

Steps to reproduce:

  1. IDK

I was following the instruction by VS code's website but it seemed that nothing that I tried worked.

I created a new configuration as required but whenever I put the path it refuses to work in VS code although the path VS code complains about in the integrated terminal window works fine when I call it manually.

The error the debugger throws is the following:

(automl-meta-learning) brandomiranda~/automl-meta-learning/automl/experiments ❯ env PTVSD_LAUNCHER_PORT=59729 /Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python /Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/launcher -m /Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization.py 
E+00000.025: Error determining module path for sys.argv
             
             Traceback (most recent call last):
               File "/Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/../ptvsd/server/cli.py", line 220, in run_module
                 spec = find_spec(options.target)
               File "/Users/brandomiranda/miniconda3/envs/automl-meta-learning/lib/python3.7/importlib/util.py", line 94, in find_spec
                 parent = __import__(parent_name, fromlist=['__path__'])
             ModuleNotFoundError: No module named '/Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization'
             
             Stack where logged:
               File "/Users/brandomiranda/miniconda3/envs/automl-meta-learning/lib/python3.7/runpy.py", line 193, in _run_module_as_main
                 "__main__", mod_spec)
               File "/Users/brandomiranda/miniconda3/envs/automl-meta-learning/lib/python3.7/runpy.py", line 85, in _run_code
                 exec(code, run_globals)
               File "/Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/__main__.py", line 45, in <module>
                 cli.main()
               File "/Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/../ptvsd/server/cli.py", line 361, in main
                 run()
               File "/Users/brandomiranda/.vscode/extensions/ms-python.python-2020.2.63072/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/../ptvsd/server/cli.py", line 226, in run_module
                 log.exception("Error determining module path for sys.argv")
             

/Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python: Error while finding module specification for '/Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization.py' (ModuleNotFoundError: No module named '/Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization')

then I tried running the file it complains manually and it runs it just fine...

(automl-meta-learning) brandomiranda~/automl-meta-learning/automl/experiments ❯ python /Users/brandomiranda/automl-meta-learning/automl/experiments/experiments_model_optimization.py
--> main in differentiable SGD
-------> Inside Experiment Code <--------

---> hostname:

device = cpu
Files already downloaded and verified
Files already downloaded and verified
Files already downloaded and verified

even when I hover over the path name and click it with command + click then it takes me to the path from within VS code. Which seems bizzare. So somehow only when I run it in debugger mode does it not work. Why?


Cross-posted:

https://stackoverflow.com/questions/60215436/how-to-correctly-set-specific-module-to-debug-in-vs-code

@brando90
Copy link
Author

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: Experiments Protype1",
            "type": "python",
            "request": "launch",
            "module": "${workspaceFolder}/automl/experiments/experiments_model_optimization.py" // ~/automl-meta-learning/automl/experiments/experiments_model_optimization.py
        },
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "enter-your-module-name-here",
            "console": "integratedTerminal"
        },
        {
            "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"
        }
    ]
}

@brando90
Copy link
Author

how do I find the PTVSD version?

@int19h
Copy link
Contributor

int19h commented Feb 18, 2020

Ah, I see the problem. You're using "module", but then specifying the path to the file - it should be a fully qualified module name instead. Or if you really do want to start it as a file, then it must be "program" rather than "module".

Basically, this:

"program": "foo/bar.py"

is as if you did this from command line:

python foo/bar.py

and this:

"module": "foo.bar"

is as if you did:

python -m foo.bar

Note that in the latter case, you're also responsible for ensuring that the current directory and/or PYTHONPATH is such that foo.bar can be resolved.

@int19h int19h closed this as completed Feb 20, 2020
@brando90
Copy link
Author

Ah, I see the problem. You're using "module", but then specifying the path to the file - it should be a fully qualified module name instead. Or if you really do want to start it as a file, then it must be "program" rather than "module".

Basically, this:

"program": "foo/bar.py"

is as if you did this from command line:

python foo/bar.py

and this:

"module": "foo.bar"

is as if you did:

python -m foo.bar

Note that in the latter case, you're also responsible for ensuring that the current directory and/or PYTHONPATH is such that foo.bar can be resolved.

but when I try to add a configuration VS code asks me for different options. Which one do I choose?
Screen Shot 2020-02-23 at 4 06 15 PM

@brando90
Copy link
Author

how do you have VS code fill in for your home path? i.e. if I had ~/foo/main.py, how can I have VS code fill for ~?

@int19h
Copy link
Contributor

int19h commented Feb 24, 2020

You should pick the one that is most appropriate to your use case. Based on the configuration you've show, it sounds like "Python File" will work better for you. You can edit launch.json afterwards so that it always starts a particular named file, instead of the currently selected one.

Here is the list of all variable substitutions that can be done in launch.json. I don't think there's anything specifically for ~, but you should be able to use {env:HOME} (on Linux and OS X) or {env:USERPROFILE} (on Windows) to similar effect.

@brando90
Copy link
Author

brando90 commented Feb 27, 2020

{env:HOME}

I get an error:

FileNotFoundError: [Errno 2] No such file or directory: '{env:HOME}/automl-meta-learning/automl/experiments/experiments_model_optimization.py

code:

        {
            "name": "Python: Experiments Protype1",
            "type": "python",
            "request": "launch",
            "program": "{env:HOME}/automl-meta-learning/automl/experiments/experiments_model_optimization.py",
            "console": "integratedTerminal"
        }

thanks for the help btw.

@brando90
Copy link
Author

works, dont forget the money sign! $

            "name": "Python: Experiments Protype1",
            "type": "python",
            "request": "launch",
            "program": "${env:HOME}/automl-meta-learning/automl/experiments/experiments_model_optimization.py",
            "console": "integratedTerminal"
        }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants