13

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?


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"
        }
    ]
}

Cross-posted:

2
  • Please post your current launch.json file content. Especially, the program and pythonPath values are important in this case. Commented Feb 20, 2020 at 9:03
  • @nima posted the whole thing! thanks for reminding me to do that. Forgot somehow. Commented Feb 20, 2020 at 14:38

2 Answers 2

7
+50

You are using module instead of program in launch.json. When using module you must pass only the module\sub-module name, not the entire path. Visual Studio will then load the specified module and execute it's __main__.py file.

This would be the correct input, assuming automl is a module and experiments is a submodule:

"module": "automl.experiments"

If you want to point to your script directly you can use the path you were using previously, just changing module to program:

"program": "${workspaceFolder}/automl/experiments/experiments_model_optimization.py"

Sign up to request clarification or add additional context in comments.

3 Comments

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 ~?
I don't have a Linux installation so I can't verify, but you can try ${env:HOME}. I'd recommend using ${workspaceFolder} , since it'll allow you to move your "project" to any folder you want. For reference: code.visualstudio.com/docs/editor/variables-reference
yup that worked! Make sure to include the money sign $ peeps!
2

So this is what I did. Once I opened the launch.json file by going to the debugger tab on the left:

enter image description here

then I clicked on Add configuration and then the launch.json file opened. Then at the bottom right there is a blue botton with Add Configuration:

enter image description here

then I filled in the stex that appeared after selecting Python file. The text that appeared was:

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }

and I changed it to:

        {
            "name": "Python: My Trainable",
            "type": "python",
            "request": "launch",
            "program": "/Users/brandomiranda/automl-meta-learning/prototyping_tests_playground/trainable_optimizers/my_trainable.py",
            "console": "integratedTerminal"
        },

then I made sure I selected it on the left debug menu so that each time I ran it with the F5 short cut it ran the right file no matter where I was in VS code.


Current file:

{
    // 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: My Trainable Step Size Higher",
            "type": "python",
            "request": "launch",
            "program": "/Users/brandomiranda/automl-meta-learning/prototyping_tests_playground/trainable_optimizers/trainable_step_size.py",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Experiments Protype1",
            "type": "python",
            "request": "launch",
            "program": "${env:HOME}/automl-meta-learning/automl/experiments/experiments_model_optimization.py",
            "console": "integratedTerminal"
        },
        {
            "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"
        }
    ]
}

seems ${end:HOME} is important for getting home path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.