[Tools] VScode|How to debug Python projects in Linux is more convenient? Also known as VScode how to debug Python projects (compatible environment Ubuntu18.04)

I have used Anaconda, Jupyter, Pycharm, VScode, VS2022, and pdb to write python projects or maintain python environments. Each has its own advantages and disadvantages, but VScode yyds!

You may be intimidated by the hype about Python configuration items on the Internet, and by the introduction of various plug-ins in VScode, but in fact it is just a pure text editor, and it is quite simple to use.

Solution 1: VScode

In general, debugging with VScode will encounter some operational problems, one is to switch the python version, the other is how to put the files in order to correctly refer to each other for multi-file project files, and the third is how to pass parameters when debugging.

Let me briefly explain it next.

First, the official website link for VScode download: https://code.visualstudio.com/ .
Secondly, install the plug-in "Python", the name of the plug-in is Python, just install this one.
The software packages in the application store of some systems are very old, such as Ubuntu 18.04, and the software downloaded from the application store does not support Chinese input methods.

1 Switch Python version in VScode (when running and debugging)

First of all, let me declare and emphasize that you don't need to configure environment variables to switch Python versions.

Ctrl+Shift+P, select the configuration item, enter pythonthe python configuration item, and select interpreterthe configuration item to select the Python interpreter.

As shown below:

Please add a picture description

After modification, the interpreter will be automatically adopted when running and debugging.

2 Debug local modules and third-party libraries

For launch.jsonthe configuration of the file, please refer to the document-Launch configurations on the VScode official website .

Question 1: vscode python debugging flashback

Reference: vscode python debugging flashback

Solution: Go to the official website to install VScode (the castrated version installed in the software store cannot support Chinese input), and downgrade the Python plugin of VScode. I recommend Ubuntu 18 to use version 2021.05.08.

insert image description here

Question 2: Local module debugging method (cannot find the module)

Reference: python [No module named] can't find the module I wrote 3 situations and solutions - Marilyn Chrysanthemum - CSDN Blog

  1. Python projects can be organized into packages. If organized into packages, its directory structure is as follows:

    my_package/
    ├── my_package_code/
    │   ├── __init__.py
    │   ├── module1.py
    │   └── module2.py
    ├── README.md
    ├── setup.py
    └── requirements.txt
    

    Generally, two levels of directories are required.
    As for the python package, it cannot be run directly, but it can be imported directly after installation.

    The writing method of setup.pyis worth opening a new article. I suggest that if you don’t know how to write it, it’s best to ask ChatGPT directly and modify it on the template it generates for you, so that the demand for new knowledge is the least.

  2. If it is organized into modules, it can be run directly more conveniently, as long as the entry code of the module is placed in the root directory, for example:

    my_package/
    ├── my_package_code/
    │   ├── __init__.py
    │   ├── module1.py
    │   └── module2.py
    ├── __init__.py
    ├── main.py
    ├── README.md
    ├── setup.py
    └── requirements.txt
    

    This case can be used to python3 -m my_package.mainrun the module.

So how to debug the above code in VScode? There are two ways, one is to write another function and call it; the other is more recommended, directly use the module debugger of VScode.

1) Call directly, then debug

Create a new file and import this module to breakpoint debugging, as follows:

import onemodule.test
onemodule.test.main() #在此行打断点

Disadvantages of this approach:

  1. When jumping, it is easy to jump to the content you have installed in the site-packages directory. When you want to modify it, you have to go back to find the original code.
  2. If you have not finished the installation program, the module may not be found, and you have to manually quote the path, for example:
    import sys
    import os
    sys.path.append(os.pos.path.dirname(__file__))
    
    import onemodule.test
    onemodule.test.main() #在此行打断点
    

Also, be careful launch.jsonto add it in the file justMyCode: false, otherwise you cannot jump into the module file.

As shown in the figure below, click Add Configuration to enter launch.jsonthe file:

Please add a picture description

As shown below, justMyCodechange the options falseto:

Please add a picture description

2) (recommended) Python module debugger that comes with VScode

It can be run directly python -m onemodule.test, and the corresponding launch file can be written in the same way.

You can first generate a launch file with VScode:

insert image description here
It will probably generate something like this:

{
    
    
    "name": "Python: 模块",
    "type": "python",
    "request": "launch",
    "module": "module_name"
},

In fact, to put it bluntly, it is used when debugging files "program": "xxxx.py", and is used when debugging modules "module": "xxxx". Others, such as parameter configuration, are the same as file debugging.

Question 3: Passing command line parameters in VScode

As above, click to enter the configuration launch.jsonfile, as shown in the figure below, add the args parameter:

Please add a picture description

If you can't understand why you need to configure debugging and think that printing can solve most of the problems, you can consider learning about "conditional breakpoints" and variable monitoring.

Solution 2: pdb

Reference: How to dynamically debug Python's third-party library - ybdesire-CSDN Blog

Advantages: No configuration is required at all; modules/third-party libraries can be directly debugged; for python programs that already know how to run them on the command line, but do not know how to run them in VScode, you may wish to directly debug them pdb.
Disadvantages: No convenient graphical interface.

When you need to debug, just insert the following code before the debugged code:

import pdb
pdb.set_trace()

Add one more command line pdb, such as executing: python -m pdb onemodule.pdbtest, to debug.

Similar to gdb debugging, pdbthe code printed on the terminal is about to be executed rather than executed.

Common commands:

  1. The n command (next) allows the code to run in a single step; the s command (single step into, fine running), this command will enter the method.
  2. <variable_name>.d (data), check the value of the intermediate variable variable_name. (Note: For most types, just enter the variable name directly)
  3. b <line>, breakpoint at the first line; b command, view all breakpoints.
  4. c command (continue), let the code run directly.
  5. clear <breakpoint_order>, clear the first breakpoint_order breakpoint.
  6. l command to view multiple lines of currently running code.
  7. Any python code can be directly input, such as print("1"), very convenient.
  8. q command, quit.

All articles on this account are original, welcome to reprint, please indicate the source of the article: https://blog.csdn.net/qq_46106285/article/details/130469097 . Baidu and various collection sites are not trustworthy, please be careful when searching. Technical articles are generally time-sensitive. I am used to revising and updating my blog posts from time to time, so please visit the source to view the latest version of this article.

Guess you like

Origin blog.csdn.net/qq_46106285/article/details/130469097