Debug Python module using VS Code by pressing F5











up vote
0
down vote

favorite












My project structure is as follows:



Project Folder
--setup.py
----Module Folder
------ __init__.py
------ __main__.py


My __main__.py file contains the entry point to my application and the setup file is configured like this:



from setuptools import setup

setup(name='my_project',
version='0.1.0',
packages=['my_project'],
entry_points={
'console_scripts': [
'my_project= my_project.__main__:main'
]})


This means I can run my code without the debugger attached using:



python -m my_project


I've tried debugging using VS Code by navigating to my __main__.py file and pressing F5 to run but this doesn't work and throws an exception. How do I configure Visual Studio Code to run this module in debug mode?
Also how do I ensure the program also runs the module and not the file I am looking at when I press F5?










share|improve this question


























    up vote
    0
    down vote

    favorite












    My project structure is as follows:



    Project Folder
    --setup.py
    ----Module Folder
    ------ __init__.py
    ------ __main__.py


    My __main__.py file contains the entry point to my application and the setup file is configured like this:



    from setuptools import setup

    setup(name='my_project',
    version='0.1.0',
    packages=['my_project'],
    entry_points={
    'console_scripts': [
    'my_project= my_project.__main__:main'
    ]})


    This means I can run my code without the debugger attached using:



    python -m my_project


    I've tried debugging using VS Code by navigating to my __main__.py file and pressing F5 to run but this doesn't work and throws an exception. How do I configure Visual Studio Code to run this module in debug mode?
    Also how do I ensure the program also runs the module and not the file I am looking at when I press F5?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      My project structure is as follows:



      Project Folder
      --setup.py
      ----Module Folder
      ------ __init__.py
      ------ __main__.py


      My __main__.py file contains the entry point to my application and the setup file is configured like this:



      from setuptools import setup

      setup(name='my_project',
      version='0.1.0',
      packages=['my_project'],
      entry_points={
      'console_scripts': [
      'my_project= my_project.__main__:main'
      ]})


      This means I can run my code without the debugger attached using:



      python -m my_project


      I've tried debugging using VS Code by navigating to my __main__.py file and pressing F5 to run but this doesn't work and throws an exception. How do I configure Visual Studio Code to run this module in debug mode?
      Also how do I ensure the program also runs the module and not the file I am looking at when I press F5?










      share|improve this question













      My project structure is as follows:



      Project Folder
      --setup.py
      ----Module Folder
      ------ __init__.py
      ------ __main__.py


      My __main__.py file contains the entry point to my application and the setup file is configured like this:



      from setuptools import setup

      setup(name='my_project',
      version='0.1.0',
      packages=['my_project'],
      entry_points={
      'console_scripts': [
      'my_project= my_project.__main__:main'
      ]})


      This means I can run my code without the debugger attached using:



      python -m my_project


      I've tried debugging using VS Code by navigating to my __main__.py file and pressing F5 to run but this doesn't work and throws an exception. How do I configure Visual Studio Code to run this module in debug mode?
      Also how do I ensure the program also runs the module and not the file I am looking at when I press F5?







      python visual-studio-code vscode-settings






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 0:50









      James Mundy

      1,80922444




      1,80922444
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          After some research I've found a solution:




          1. Navigate to the top right section in the debug menu and click the cog to create a launch.json file for this project. This will be used to configure VS Code.


          Configure VS Code




          1. If there isn't one already a launch.json file be created, into it paste this:


          {
          "version": "0.2.0",
          "configurations": [
          {
          "name": "Python Module",
          "type": "python",
          "request": "launch",
          "program": "${file}",
          "console": "integratedTerminal",
          "pythonPath": "${config:python.pythonPath}",
          "module": "my_project",
          "cwd": "${workspaceRoot}",
          }
          ]
          }



          I found this code here: https://github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308




          1. Just using this answer didn't work for me though and I got the error: No module named my_projectbut I found this answer: https://github.com/DonJayamanne/pythonVSCode/issues/826
            In it the final comment tells you add the following to the config.


          "env": {"PYTHONPATH":"${workspaceRoot}"},



          This fixes the error and now you can press F5 and your module will be debugged.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235050%2fdebug-python-module-using-vs-code-by-pressing-f5%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            After some research I've found a solution:




            1. Navigate to the top right section in the debug menu and click the cog to create a launch.json file for this project. This will be used to configure VS Code.


            Configure VS Code




            1. If there isn't one already a launch.json file be created, into it paste this:


            {
            "version": "0.2.0",
            "configurations": [
            {
            "name": "Python Module",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "pythonPath": "${config:python.pythonPath}",
            "module": "my_project",
            "cwd": "${workspaceRoot}",
            }
            ]
            }



            I found this code here: https://github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308




            1. Just using this answer didn't work for me though and I got the error: No module named my_projectbut I found this answer: https://github.com/DonJayamanne/pythonVSCode/issues/826
              In it the final comment tells you add the following to the config.


            "env": {"PYTHONPATH":"${workspaceRoot}"},



            This fixes the error and now you can press F5 and your module will be debugged.






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              After some research I've found a solution:




              1. Navigate to the top right section in the debug menu and click the cog to create a launch.json file for this project. This will be used to configure VS Code.


              Configure VS Code




              1. If there isn't one already a launch.json file be created, into it paste this:


              {
              "version": "0.2.0",
              "configurations": [
              {
              "name": "Python Module",
              "type": "python",
              "request": "launch",
              "program": "${file}",
              "console": "integratedTerminal",
              "pythonPath": "${config:python.pythonPath}",
              "module": "my_project",
              "cwd": "${workspaceRoot}",
              }
              ]
              }



              I found this code here: https://github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308




              1. Just using this answer didn't work for me though and I got the error: No module named my_projectbut I found this answer: https://github.com/DonJayamanne/pythonVSCode/issues/826
                In it the final comment tells you add the following to the config.


              "env": {"PYTHONPATH":"${workspaceRoot}"},



              This fixes the error and now you can press F5 and your module will be debugged.






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                After some research I've found a solution:




                1. Navigate to the top right section in the debug menu and click the cog to create a launch.json file for this project. This will be used to configure VS Code.


                Configure VS Code




                1. If there isn't one already a launch.json file be created, into it paste this:


                {
                "version": "0.2.0",
                "configurations": [
                {
                "name": "Python Module",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "pythonPath": "${config:python.pythonPath}",
                "module": "my_project",
                "cwd": "${workspaceRoot}",
                }
                ]
                }



                I found this code here: https://github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308




                1. Just using this answer didn't work for me though and I got the error: No module named my_projectbut I found this answer: https://github.com/DonJayamanne/pythonVSCode/issues/826
                  In it the final comment tells you add the following to the config.


                "env": {"PYTHONPATH":"${workspaceRoot}"},



                This fixes the error and now you can press F5 and your module will be debugged.






                share|improve this answer












                After some research I've found a solution:




                1. Navigate to the top right section in the debug menu and click the cog to create a launch.json file for this project. This will be used to configure VS Code.


                Configure VS Code




                1. If there isn't one already a launch.json file be created, into it paste this:


                {
                "version": "0.2.0",
                "configurations": [
                {
                "name": "Python Module",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "pythonPath": "${config:python.pythonPath}",
                "module": "my_project",
                "cwd": "${workspaceRoot}",
                }
                ]
                }



                I found this code here: https://github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308




                1. Just using this answer didn't work for me though and I got the error: No module named my_projectbut I found this answer: https://github.com/DonJayamanne/pythonVSCode/issues/826
                  In it the final comment tells you add the following to the config.


                "env": {"PYTHONPATH":"${workspaceRoot}"},



                This fixes the error and now you can press F5 and your module will be debugged.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 0:50









                James Mundy

                1,80922444




                1,80922444






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235050%2fdebug-python-module-using-vs-code-by-pressing-f5%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff