如何使用pycharm调试/断点我的django应用程序?

时间:2022-02-04 21:02:17

I'm trying to work out how to run the debug stuff that pycharm seems to offer (well, it allows me to set breakpoints, anyway, so i'm assuming there's a nice gui for it)

我正在尝试解决如何运行pycharm提供的调试功能(不管怎样,它允许我设置断点,所以我假设它有一个很好的gui)

I've concluded that i cannot use the ctrl-shift-r and then "runserver" command, and that instead i'd need to setup a "run configuration"? I made a "django server" one, but i don't know what values to put etc. When i run it, it tells me that some setting is wrong - i'm pretty sure it isn't, because the standard "runserver" command works fine.

我的结论是,我不能使用ctrl-shift-r和“runserver”命令,相反,我需要设置一个“运行配置”?我做了一个“django服务器”,但是我不知道要放什么值等等。当我运行它时,它告诉我一些设置是错误的——我很确定不是,因为标准的“runserver”命令工作正常。

And that's about all i concluded. If there is a nifty tutorial or steps to get it so i can

这就是我的结论。如果有一个很好的教程或步骤,我可以得到它

  1. put in a break point
  2. 放一个断点
  3. go to the page that triggers that breakpoint and follow the code's inner working in pycharm
  4. 转到触发该断点的页面,并跟踪代码在pycharm中的内部工作

i'd be thrilled!

我很激动!

cheers!

干杯!

UPDATE: in case you're wondering, here is the error i got:

更新:如果你想知道,这是我得到的错误:

Traceback (most recent call last):

回溯(最近一次通话):

File "manage.py", line 11, in import settings

文件”管理。在导入设置中,第11行

File "C:\development\PycharmProjects\dumpstown\settings.py", line 185, in add_to_builtins('gravatar.templatetags.gravatar')

文件“C:\开发\ PycharmProjects \ dumpstown \设置。第185行,在add_to_builtins('gravatar.templatetag .gravatar')中

File "C:\development\python\lib\site-packages\django\template\base.py", line 1017, in add_to_builtins

python文件“C:\开发\ \ lib \网站\ django模板\ \基地。在add_to_builtins中,第1017行

builtins.append(import_library(module))

builtins.append(import_library(模块)

File "C:\development\python\lib\site-packages\django\template\base.py", line 963, in import_library

python文件“C:\开发\ \ lib \网站\ django模板\ \基地。在import_library中,第963行

raise InvalidTemplateLibrary("ImportError raised loading %s: %s" % (taglib_module, e))

提高invalidtemplatbrary(“ImportError提高装入%s: %s”% (taglib_module, e))

django.template.base.InvalidTemplateLibrary: ImportError raised loading gravatar.templatetags.gravatar: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

django.template.base。病人:恐惧增加了负重。无法导入设置,因为环境变量DJANGO_SETTINGS_MODULE没有定义。

Where the application itself, on a "runserver" never has any issues.

在“运行服务器”上的应用程序本身从来没有任何问题。

UPDATE: as per my answer below, pycharm is broken for add_to_builtins.

更新:根据我的回答,pycharm被分解为add_to_builtins。

3 个解决方案

#1


29  

  1. Setup your virtual environment

    设置您的虚拟环境

    1. Create or open your project in PyCharm
    2. 在PyCharm创建或打开你的项目
    3. Go to File - Settings in the menu (or just click on the settings icon)
    4. 进入文件-菜单中的设置(或者点击设置图标)
    5. Go to Python Interpreter
    6. 去Python解释器
    7. Click on Add in the top
    8. 单击顶部的Add
    9. Go to the bin folder where you created the virtual environment and select python
    10. 转到创建虚拟环境的bin文件夹并选择python
  2. Set a breakpoint

    设置一个断点

    1. Next to the line of code you want to set the breakpoint. To the left there is usually a grey line. Usually next to the line numbers. Just click there and a big red dot will appear. Looks like this
    2. 在要设置断点的代码行旁边。在左边通常有一条灰色的线。通常在行号旁边。只要点击这里,一个大大的红点就会出现。看起来是这样的
  3. Hit the Run in debug mode button.

    单击debug模式下的Run按钮。

    1. Next to the Green play button there is a button with a bug on it. Use that to launch the runserver in debug mode. Now when you use your web application and the code hits the breakpoint it will stop there and you will be able to step in and over or resume in pycharm.

      在绿色的播放按钮旁边有一个按钮,上面有一个bug。使用它在调试模式下启动runserver。现在,当您使用web应用程序时,当代码到达断点时,它将停止在那里,您将能够在pycharm中不断地进入或恢复。

      • If you still are not able to debug you might need to edit the pycharm project config. You can do that like this.
      • 如果您仍然不能调试,您可能需要编辑pycharm项目配置。你可以这样做。

You can look at http://garmoncheg.blogspot.it/2012/01/establishing-dev-environment-with.html too

您可以查看http://garmoncheg.blogspot.it2/01/building -dev-environment-with。html太

#2


4  

So i gave all the answers here a +1 for trying - but they're not the issue. Near as i can tell, the answer is that pycharm is broken. Which is a pain, but the solution is easy -

我给了所有的答案+1表示尝试,但这不是问题所在。就我所知,答案是pycharm坏了。这是一种痛苦,但解决方法很简单?

IF you dont' want to use the little green button at the top of pycharm, or use the pycharm debugging feature? then don't worry, you don't need to do anything. Continue using ctrl-shift-r and runserver (or whatever your shortcut to manage.py is)

如果您不想使用pycharm顶部的绿色小按钮,或者使用pycharm调试功能?那么别担心,你什么都不需要做。继续使用ctrl-shift-r和runserver(或任何您要管理的快捷方式)。py是)

IF you do want to use the little green "run" button, or if you want to use pycharm's debugging kit, then you absolutely cannot use the "add_to_builtins", at least in the settings.py file (I never put it anywhere else myself, pycharm might require it elsewhere?). add_to_builtins doesn't work in pycharm - it gets itself caught in a loop of grave consequences when you use the little green button or the debug button. Using ctrl-shift-r and runserver doesn't, curiously, have this problem.

如果您确实想使用绿色的“run”按钮,或者如果您想使用pycharm的调试工具,那么您绝对不能使用“add_to_builtins”,至少在设置中是这样。py文件(我从来没有把它放在别的地方,pycharm可能需要它在别的地方?)add_to_builtins在pycharm中不能用——当你使用绿色的小按钮或者调试按钮时,它会陷入严重后果的循环中。奇怪的是,使用ctrl-shift-r和runserver并没有这个问题。

The good news is that "add_to_builtins" isn't a must-have, just a nice-to-have. Just add the "{% load x %}" command to each template where you use x and you will be set. Alternatively, save a hundred bucks and use some sort of free eclipse tool.

好消息是,“add_to_builtins”并不是必须拥有的,而只是一个美好的拥有。只需将“{% load x %}”命令添加到您使用x的每个模板中,您将被设置。或者,您可以节省100美元,并使用某种免费的eclipse工具。

#3


3  

The problem is that the DJANGO_SETTINGS_MODULE variable which should point to your project's settings file, wasn't set anywhere, since the variable is reported to be undefined.

问题是应该指向项目的设置文件的DJANGO_SETTINGS_MODULE变量没有在任何地方设置,因为该变量被报告为未定义的。

The solution and an explanation you can refer to can be found in a previous post

您可以参考的解决方案和解释可以在以前的帖子中找到。

#1


29  

  1. Setup your virtual environment

    设置您的虚拟环境

    1. Create or open your project in PyCharm
    2. 在PyCharm创建或打开你的项目
    3. Go to File - Settings in the menu (or just click on the settings icon)
    4. 进入文件-菜单中的设置(或者点击设置图标)
    5. Go to Python Interpreter
    6. 去Python解释器
    7. Click on Add in the top
    8. 单击顶部的Add
    9. Go to the bin folder where you created the virtual environment and select python
    10. 转到创建虚拟环境的bin文件夹并选择python
  2. Set a breakpoint

    设置一个断点

    1. Next to the line of code you want to set the breakpoint. To the left there is usually a grey line. Usually next to the line numbers. Just click there and a big red dot will appear. Looks like this
    2. 在要设置断点的代码行旁边。在左边通常有一条灰色的线。通常在行号旁边。只要点击这里,一个大大的红点就会出现。看起来是这样的
  3. Hit the Run in debug mode button.

    单击debug模式下的Run按钮。

    1. Next to the Green play button there is a button with a bug on it. Use that to launch the runserver in debug mode. Now when you use your web application and the code hits the breakpoint it will stop there and you will be able to step in and over or resume in pycharm.

      在绿色的播放按钮旁边有一个按钮,上面有一个bug。使用它在调试模式下启动runserver。现在,当您使用web应用程序时,当代码到达断点时,它将停止在那里,您将能够在pycharm中不断地进入或恢复。

      • If you still are not able to debug you might need to edit the pycharm project config. You can do that like this.
      • 如果您仍然不能调试,您可能需要编辑pycharm项目配置。你可以这样做。

You can look at http://garmoncheg.blogspot.it/2012/01/establishing-dev-environment-with.html too

您可以查看http://garmoncheg.blogspot.it2/01/building -dev-environment-with。html太

#2


4  

So i gave all the answers here a +1 for trying - but they're not the issue. Near as i can tell, the answer is that pycharm is broken. Which is a pain, but the solution is easy -

我给了所有的答案+1表示尝试,但这不是问题所在。就我所知,答案是pycharm坏了。这是一种痛苦,但解决方法很简单?

IF you dont' want to use the little green button at the top of pycharm, or use the pycharm debugging feature? then don't worry, you don't need to do anything. Continue using ctrl-shift-r and runserver (or whatever your shortcut to manage.py is)

如果您不想使用pycharm顶部的绿色小按钮,或者使用pycharm调试功能?那么别担心,你什么都不需要做。继续使用ctrl-shift-r和runserver(或任何您要管理的快捷方式)。py是)

IF you do want to use the little green "run" button, or if you want to use pycharm's debugging kit, then you absolutely cannot use the "add_to_builtins", at least in the settings.py file (I never put it anywhere else myself, pycharm might require it elsewhere?). add_to_builtins doesn't work in pycharm - it gets itself caught in a loop of grave consequences when you use the little green button or the debug button. Using ctrl-shift-r and runserver doesn't, curiously, have this problem.

如果您确实想使用绿色的“run”按钮,或者如果您想使用pycharm的调试工具,那么您绝对不能使用“add_to_builtins”,至少在设置中是这样。py文件(我从来没有把它放在别的地方,pycharm可能需要它在别的地方?)add_to_builtins在pycharm中不能用——当你使用绿色的小按钮或者调试按钮时,它会陷入严重后果的循环中。奇怪的是,使用ctrl-shift-r和runserver并没有这个问题。

The good news is that "add_to_builtins" isn't a must-have, just a nice-to-have. Just add the "{% load x %}" command to each template where you use x and you will be set. Alternatively, save a hundred bucks and use some sort of free eclipse tool.

好消息是,“add_to_builtins”并不是必须拥有的,而只是一个美好的拥有。只需将“{% load x %}”命令添加到您使用x的每个模板中,您将被设置。或者,您可以节省100美元,并使用某种免费的eclipse工具。

#3


3  

The problem is that the DJANGO_SETTINGS_MODULE variable which should point to your project's settings file, wasn't set anywhere, since the variable is reported to be undefined.

问题是应该指向项目的设置文件的DJANGO_SETTINGS_MODULE变量没有在任何地方设置,因为该变量被报告为未定义的。

The solution and an explanation you can refer to can be found in a previous post

您可以参考的解决方案和解释可以在以前的帖子中找到。