interactive shell debugging with pycharm

2019-01-07 04:14发布

I am new to PyCharm. I have been using IDLE for a long time.

It is very convenient to use Python objects after script execution in IDLE. Is there any way to use script objects after its execution with interactive python shell using PyCharm?

For example, we have a 'test' project with one file 'test.py':

a = '123'
print a

after execution we can get the result:

123
Process finished with exit code 0

How can I use string 'a' with interactive shell?

9条回答
beautiful°
2楼-- · 2019-01-07 04:35

In addition to the suggestion I made on Ramkins's answer, you can run the file directly with the console by right clicking inside the file and selecting Run File in Console.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-07 04:40

Leave command line open after executing

For anyone still having this problem: Go to the Run/Debug menu, choose Edit Configuration, check the box 'Show command line' this will enable you to enter parameters in the console at the >>> prompt and test your function.

Change setting for current file only

Global configuration

To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)

Change setting for all Python files

查看更多
虎瘦雄心在
4楼-- · 2019-01-07 04:41

Built-in python shell for current debug session

  1. Set a breakpoint at the line of interest in your code (i.e. by clicking the gutter), and launch debug (right-click in the editor then pick Debug myfile.py...).
  2. When the breakpoint is reached, locate the Debug > Console tab, and then click the Show command line icon (see screenshot).

This will enable a python shell (notice the green >>> on the screenshot) where you can access all the variables in the current scope, and do everything you usually do in the Python shell.

In recent pycharm versions you get the full ipython interpreter instead of the plain python shell (if ipython is installed).

Enabling Python shell for the debugged app

The Evaluate expression window

As a more comfortable alternative, if you only need to inspect some variables, access members or call methods on an object in scope, once a breakpoint is reached, select an expression in the current scope, right-click -> Evaluate Expression (or use the hotkey shown in the menu under RunEvaluate Expression...), edit as needed — you can type any python expression, with auto-completion available — and then press Enter (or click Evaluate) to inspect the result.

Multiple and multiline expressions are supported: to get a multiline version of the Inspect dialog click the Code fragment mode or select more than one line of code in the editor and then evaluate the expression. You can assign values to existing variables (new ones cannot be defined), and run entire chunks of code. inspecting variables in the current scope

Mouse hover over variables

To see the value of a variable after you hit a breakpoint in debug mode, hover the mouse pointer over the variable (1-2 seconds) and the value will be shown in a tooltip.

enter image description here

The hint will contain a icon — clicking it will open the inspector in a popup.

enter image description here

For the variables in scope the inspector is shown in the bottom panel under Debug > Debugger.

查看更多
登录 后发表回答