PyCharm Debugger Tutorial

29/12/2020
Chưa phân loại
If you are a new python user and are looking for an environment specifically for Python development, integration, and debugging, PyCharm IDE can be best suited. It is available for all major operating systems, with a commercial and freemium license along with free community edition to start with.

PyCharm and Debugging

While many of us dread writing codes in the first place, several others find debugging a more frustrating task out of all. It is more time-consuming in Python since you don’t know where the bug is. Luckily, with PyCharm and its significant debugging features, users can engage in a unique debugging experience when running their python scripts.

Find out how through a detailed tutorial below:

To see how debugging works in PyCharm, let’s take a sample code snippet. Remember, you will have to run the script first and then start with debugging errors.

Create a new project and then a new file into it. Now, type the following code into the file.

import math

class Solver:
def demo(self, a, b, c):
d = b ** 24 * a * c
if d > 0:
disc = math.sqrt(d)
root1 = (-b + disc) / (2 * a)
root2 = (-b – disc) / (2 * a)
return root1, root2
elif d == 0:
return -b / (2 * a)
else:
return "This equation has no roots"

if __name__ == ‘__main__’:
solver = Solver()

while True:
a = int(input("a: "))
b = int(input("b: "))
c = int(input("c: "))
result = solver.demo(a, b, c)
print(result)

Breakpoints and How to Place them

Breakpoints are markers that help you suspend execution of your program at any specific point so you can examine the errors and behaviour of that particular line. Once marked, a breakpoint will remain in your code unless you explicitly remove it. To place them, click on the left gutter next to the line you want to suspend the application at.

For example:

How to Start Debugging?

Once you’re done adding breakpoints, your code is ready for debugging. The debugger will run the entire program except for the lines you have explicitly marked not to. To start the debugger session, click the ‘green play icon’ in the left. When a popup menu appears, select the option, ‘Debug Solver’.  (Solver, because that’s the name of the file.)

When the debugger starts, your program begins to execute in a Console Tab of the Debug Window. Since the program required input values, the debugger screen will ask you to do that below:

At the first breakpoint, the debugger suspends the program highlighting the line in blue:

To resume the debugging, click the ‘green play icon’ on top of the Debugger tab toolbar.

The concept of Inline Debugging

Inline debugging lets you view each variable’s value so to make the debugging process easier and more understandable. This feature is present by default in PyCharm. You can also turn it off from here.

The above image shows the grey text next to each line of code. These also display both the values and description of the code.

Moving forward

After stopping at the first breakpoint, click the green play icon to resume debugging.

Note: While debugging your script, there are several shortcuts to examine your code, its variables, and enable debugging features. Find out how:
Click on the button to enable inline debugging
Click on the button to view the parsed file, parse.py to view the backend of your code in detail.
When you keep pressing the same button, your application will move into the next loop until the code ends.

Watching your Variables in PyCharm

To have a deeper understanding of your code, PyCharm allows you to watch variables too. Click on the button  on top of the toolbar of the Variables tab. Next, types the name of the variable you want to keep watching throughout the execution of the code.

It will be something like this:

When the debugging session begins, your watch will show an error because the variable has not been defined yet. Once, your debugger reaches the line where you have defined the variable; the watch gets past the error.

Find out how in the images below:

Evaluating Expressions

To find out values or result of a particular expression at any point in the code, click the  button. Now, click Evaluate:

The best part about Evaluate is that it not only lets you watch the expressions but also change variable values too. Check that out in the image below.

Debugging Remote Processes in PyCharm

Whether it’s a code you are working on or some order process in the background, PyCharm lets you debug remote processes too:

To do that:
Open Run and select ‘Attach to Local Process’  Choose the process you want to debug. It could be any code that you want to attach.
When you select the process, the debugger will start to debug the script.

Conclusion

Debugging can be a challenge sometimes, but not if you use the right tools and practices! PyCharm’s Debugger tools is a great option for beginners and people who are new to Python. We hope the tutorial helps you attain a better hand at programming and debugging scripts.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

CentOS8 Release Date and Features

CentOS Logo Red Hat released RHEL version 8.0 on May 7, 2019 so lots of folks are looking where is the equivalent build...
13/02/2020

How to Install latest Google Play Music Manager on Linux

Google Play Music Manager is a simple open source application used for adding the music files on your computer to your Google...
28/12/2020

How to Make a Pull Request in Github

Git is a Version Control System (VCS). GitHub is a collaboration tool built on top of Git. It is also a Git graphical frontend....
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024