如何使用PyCharm调试Python代码

本文介绍了如何使用PyCharm高效地调试Python代码,包括管理断点、附加到本地进程以及利用带有加载环境的Python解释器进行调试。通过这些功能,PyCharm使得Python的调试过程变得更为轻松。
摘要由CSDN通过智能技术生成

by Ori Roza

通过Ori Roza

如何使用PyCharm调试Python代码 (How to use PyCharm to debug your Python code)

Debugging code in any language might be frustrating, but it is especially so in Python where we cannot recognize a bug immediately.

使用任何一种语言调试代码都可能令人沮丧,但是在Python中尤其如此,我们无法立即识别出错误。

In addition, Python provides us with the PDB library as a tool for debugging, which can also be difficult to handle.

另外,Python为我们提供了PDB库作为调试工具,这也很难处理。

Luckily, we have the PyCharm IDE. It uses PyDev and gives us a new experience of debugging!

幸运的是,我们有PyCharm IDE。 它使用PyDev,并为我们提供了新的调试体验!

In this article, I will go over of the main and most useful debugging features PyCharm has to offer and teach you how to use them efficiently.

在本文中,我将介绍PyCharm提供的主要和最有用的调试功能,并教您如何有效地使用它们。

断点 (Breakpoints)

Breakpoints might be unnecessary when we are facing a bug which occurs in a certain condition.

当我们遇到在特定条件下发生的错误时,可能不需要断点。

Also, when we have a lot of them, it’s a mess.

而且,当我们有很多的时候,很混乱。

Fortunately, PyCharm gives us the ability to manage breakpoints in an efficient way:

幸运的是,PyCharm使我们能够以有效的方式管理断点:

  1. Press Ctrl+Shift+F8 (or Run->View Breakpoints)

    按Ctrl + Shift + F8(或运行->查看断点)
  2. All the breakpoints that we set on the project will be listed as shown below (see 1)

    我们在项目上设置的所有断点将如下所示(请参见1)

3. As we can see, for each breakpoint we can set a condition that will trigger the breakpoint (see 2)

3.如我们所见,我们可以为每个断点设置一个条件,该条件将触发该断点(请参见2)

4. Also, we can set a very special condition which controls whether the breakpoint will be triggered when an exception occurs (see 3) in two different states:

4.同样,我们可以设置一个非常特殊的条件,该条件控制在两种不同状态下发生异常时(参见3)是否触发断点:

a. On termination (after the script ends)

一个。 终止时(脚本结束后)

b. On raise (before the script ends)

b。 正在提高(脚本结束之前)

附加到本地流程 (Attach to local processes)

Have you ever wondered to yourself whether it’s possible to debug a remote process?

您是否曾经想过自己是否可以调试远程进程?

Yes you can! (and it’s so easy!)

是的,你可以 ! (非常简单!)

Whether you execute other processes in the background or create them as a part of the flow, PyCharm provides you with a very efficient way to debug remote processes:

无论您是在后台执行其他进程还是将其创建为流程的一部分,PyCharm均可为您提供一种非常有效的方式来调试远程进程:

  1. As shown below, open Run->Attach to Local Process

    如下所示,打开运行->附加到本地进程

2. Now choose the Python process you want to be debugged:

2.现在,选择要调试的Python进程:

3. Then, the process you chose will be debugged in PyCharm:

3.然后,您选择的过程将在PyCharm中调试:

具有加载环境的Python解释器 (Python Interpreter With The Loaded Environment)

Making calculations and manipulating the variables of the current debugged code saves time and allows us to make changes on an actual sandbox!

进行计算和处理当前调试代码的变量可以节省时间,并使我们能够在实际的沙箱中进行更改!

PyCharm provides us a Python interpreter with the loaded environment.

PyCharm为我们提供了一个带有加载环境的Python解释器。

  1. On the console tab, press the marked button:

    在控制台选项卡上,按标记的按钮:

2. As you can see below, the interpreter recognizes our variables!

2.如下所示,解释器识别出我们的变量!

结论 (Conclusion)

PyCharm provides us many great tools, and this debugger is one of them.

PyCharm为我们提供了许多出色的工具,而该调试器就是其中之一。

Debugging can be hard sometimes, but if you use the right tools, it can be easier and even fun!

有时调试可能很困难,但是如果使用正确的工具,调试会变得更加容易甚至有趣!

I hope this article taught you something new, and I am looking forward to your feedback. Please, do tell — was this useful for you?

希望本文能教给您一些新的知识,并期待您的反馈。 请告诉-这对您有用吗?

翻译自: https://www.freecodecamp.org/news/debugging-with-pycharm-be282bcd6172/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值