Description of content:

  • Why use an IDE?
  • 2. PyCharm
  • 3. Basic use of PyCharm
  • PyCharm program debugging
  • 5. Share the global Python interpreter

Why use an IDE?

First, what is an IDE?

A: IDE (Integrated Development Environment) is used to provide application programs in the program Development Environment. It generally includes code editor, compiler, debugger, graphical user interface and other tools. Simply put, it’s a development kit.

After installing Python, you can develop Python directly in one of two ways:

  • Terminal type Python to useIDLE comes with PythonTo develop;
  • Use things like Sublime Text and Notepad++Code editing toolWrite the code directly and save it with the suffix`.py`File, then command line typepython xxx.pyThe execution.

The above two methods are feasible, but there are some inconveniences, such as code indentation. In Python, code blocks are represented by indentation. If a line of code is indented incorrectly, the program may report an error or the result is not as expected, which is difficult to check. A good IDE can bring us a lot of convenience, in addition to the automatic indentation, intelligent prompt completion, convenient dependency library management and other functions, can undoubtedly improve the efficiency of our code.

2. PyCharm

PyCharm is a Python IDE designed by JetBrains to execute PY files. It also supports syntax highlighting, smart hints, code jumping, library management, and quick and easy switching between Python interpreters. The Community version of PyCharm is free and generally sufficient, although you can use the Professional version if you are willing to pay. Open the website address to download, www.jetbrains.com/pycharm/dow… , then select the corresponding system, download and install.

3. Basic use of PyCharm


① Create a project

Welcome page Click “Create New Project” to open the Project creation window

On the left, there are a number of template projects. The default is Pure Python. On the right, select the path where the project is saved and the project name.

② New program

Click project, right click “New”, then click “Python File”.

Enter the program name in the pop-up dialog box and click “OK” to complete the creation.

③ Write programs

Double-click on the left side to open the test.py file created, on the right side of the code can be written, here wrote a simple procedure, define a variable A, assign a value of 1, and then print out a!

④ Running program

Right-click the program and find Run ‘test’ to Run the program, or you can just press the shortcut key.

So when you run it, you get a result at the bottom, like this one here that prints a value of 1.

⑤ Error positioning

There is no guarantee that our program will be correct. Sometimes the program will stop running because of some problems, so we need to locate the error location. For example, if we change print(a) to print(a/0), we will intentionally cause the division by 0 error. The result is as follows:

F:\Project\Python\Test\venv\Scripts\python.exe F:/Project/

Traceback( most recent call last) :

File "F:/Project/Python/Test/test.py",line 2.in <module>

print(a/0)

ZeroDivisionError:division by zero

Copy the code

Click test.py to locate the error, line 2, and then modify it. Of course, although such obvious errors can be located directly, more complex errors need to be located through program debugging.

PyCharm program debugging

General program debugging process: “Break point”, “Single step debugging”, “Value Tracing”

(1) the breakpoint

When the program executes to the code where the breakpoint is, the application is suspended, the thread is suspended, and can then be traced through the debugger.

The breakpoint is also simple: click on the left side of a line of code, and a small red dot appears as shown.

The little red dot is the breakpoint, and in PyCharm, there are various types of breakpoints:

  • 1. Line breakpoints

For “debug for a particular line”, click on the left side of the line to set. Right-click on the breakpoint and the Settings dialog will appear as follows:

If you uncheck Enabled, the breakpoint is disabled as shown below:

  • 2. Method breakpoints

To place a breakpoint in front of a method name, usually to detect input parameters and return values of the method. Such as:

  • 3. Variable breakpoints

Sometimes we don’t care about how the program is running and just “watch what happens to a variable.” We can add a breakpoint before the variable definition. Such as:

In the process of running the program, if the value of the variable changes, the program will automatically stop and locate the variable value changes for the developer to debug.

  • 4. Conditional breakpoint (breakpoint set Condition)

Sometimes there is a scenario where we hit a breakpoint into the body of a loop, and we only care about what happens at a certain number of loops. For example, if we have a loop that runs 10 times, we want to know what happens at the eighth time of the loop. If you don’t know the conditional breakpoint, you need to press “Run to Cursor” until our condition is met. For example:

To use conditional breakpoints, mail breakpoints, enter “equality conditions” as shown below:

And then you can see that the program just jumps to I =8 and then suspends, which is very convenient.

  • 5. Log breakpoints

During debugging, we can print logs to locate the approximate location of abnormal code to narrow the scope of the problem, and then use breakpoints to locate the problem precisely. If it is a normal print log, we need to wait to rebuild the program. If we use “log breakpoint”, we avoid this meaningless wait. Using a log breakpoint is very simple. Right-click a breakpoint and uncheck “Suspend”. A pop-up window like the one shown below appears, and select “Evaluate and log” to enter the desired output.

After running the debug, you can see the console output the corresponding log information when the log breakpoint is executed, and the program is running normally and does not hang.

If you want to see more detailed information, such as the location of the breakpoint and the stack information when it is triggered, check the boxes “Breakpint hit” message “and” Stacktrace “to make the output more detailed.

  • 6. Temporary breakpoints

Temporary breakpoints are breakpoints that are triggered once and then automatically deleted. There are two ways to set this:

  • 1. Move the cursor to the Line you want to click and click the menu bar “Run” -> “Toggle Temporary Line Breakpoint”, equivalent to the shortcut key: “Ctrl+Alt+Shift+F8”
  • 2. Easier operation: Hold down Alt and click the left sidebar.

To Remove temporary breakpoints, deselect Remove once Hit to change temporary breakpoints to normal breakpoints.

  • 7. Abnormal breakpoints

Used to listen for program exceptions, once the program crashes, directly locate the exact location of the exception. Click “Run” -> “View Breakpoints” to open the Breakpoints View. Click “+”, then select “Python Exception Breakpoint”, and enter the Exception to debug in the window that pops up:

In addition to setting exception breakpoints, you can see all breakpoints set by the project, manage and configure breakpoints.

Details of debugging tools

Here, the debugging tool is divided into five areas as shown in the figure:

Zone A (Step debugging tool)

icon The name of the Functional description
Show Exception Point Show execution pointsTo locate the breakpoint currently being debugged.
Step Over Single step over, directly execute the method, enter the next step, will not enter the method inside.
Step Into Step into theIf a custom method is encountered, enter the inner part of the method. Otherwise, no method is entered.
Step Into My Code Step into the, into their own method, do not enter the system method
Force Step Into Both system and custom methods enter
Step Out Step out of, jumps out of the currently entered method and returns the next line at the method call (which also means the method has been executed).
Force Run to Cursor Execute to cursor, can be regarded as a temporary breakpoint, the program runs to the current cursor line pause
Evaluate Expression Calculate the expression. During debugging, you can modify the value of any variable through assignment or expression.

Note:

If you want to Force a Cursor to the Cursor, you need to “Force Run to Cursor”. You can do this in either of the following ways:

1. Right-click the Cursor and select Force Run to Cursor.

2. Press the shortcut keys Ctrl + Alt + F9.

Zone B (Control debugging tools)

icon The name of the Functional description
Continue program run The program runs to a breakpoint and pauses. If there is another breakpoint, click to skip to that breakpoint. If there is no breakpoint, the program continues.
Pause the program Pause the program
Abort program execution Abort program execution
Check the breakpoint You can view all breakpoints, manage and configure breakpoints
Disable all breakpoints Toggles the state of all breakpoints (enabled/disabled). When disabled, breakpoints will not be triggered.
Restore the layout Restore to original layout
Set up the Debugging related configurations, such as whether to display the return value after executing a method
Fix or cancel the label Fix or cancel the label

Zone C (Frame debugging window)

Frames are: stack frames, a data structure used to “store data” and “partial process results”; Each call to a method takes up a portion of memory on the stack, in frames, that are created as the method is called. Each stack frame contains “incoming parameters,” “return address,” “local variables,” and “information to support program debugging.” A thread consists of multiple stack frames.

Area D (variable area)

In this area you can see all the current data (method parameters, local variables, instance variables) in the stack frame. Right-click a variable to “set \ copy variable value”, “jump to variable location in the code” and so on. A common action: Select “Add to Watches” to monitor this variable individually.

Area E (Monitoring window)

In addition to the previous method of right-clicking to add monitoring, you can also add it directly by clicking “+” in the E area.

PyChram supports not only monitoring variables, but also monitoring expressions, such as a+1 above.

5. Share the global Python interpreter

PyCharm enables the virtual environment by default. Every time a project is created, all modules that depend on it need to be installed by PIP. You can actually share the current project with the global Python interpreter. Select ‘Inherit Global Stie-Packages’ when creating the project, as shown in the following figure:


If this article is helpful to you, welcome to leave a message, like, forward quality three lian, thank 😘~