Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting startup script in PyCharm debugger console

Tags:

python

pycharm

In PyCharm, it is possible to set a script that runs upon opening a new console (through Settings -> 'Build, Execution, Deployment' -> Console -> Python Console -> Starting script).

Is there a way to similarly apply a startup script to the debugger console? I find myself importing the same packages over and over again, each time I run the code.

like image 593
GL246 Avatar asked Feb 10 '16 00:02

GL246


People also ask

How do I debug a script in PyCharm?

Start debuggingOpen the HTML file that references the JavaScript to debug or select the HTML file in the Project tool window. From the context menu of the editor or the selection, choose Debug <HTML_file_name>. PyCharm generates a debug configuration and starts a debugging session through it.

How do I step in PyCharm debugger?

From the main menu, select Run | Debugging Actions | Smart Step Into or press Shift+F7 .

How do I create a run configuration in PyCharm?

Add Run/Debug configurations to the Services windowSelect View | Tool Windows | Services from the main menu or press Alt+8 . In the Services tool window, click Add service, then select Run Configuration Type. Select a run/debug configuration type from the list to add all configurations of this type to the window.


1 Answers

When you run Python Console inside PyCharm, it executes custom PyCharm script at <PYCHARM_PATH>/plugins/python/helpers/pydev/pydevconsole.py.

On the other hand, when you run PyCharm Debug Console while debugging, it executes custom PyCharm script at <PYCHARM_PATH>/Plugins/python/helpers/pydev/pydevd.py with command line parameter --file set to script you are debugging.

You can modify pydevd.py file if you want (Apache 2 license), but the easier approach would be to create startup script in which you import modules you need, functions and such and import ALL while inside PyCharm Debug Console. This would reduce all your imports to one.

Walkthrough:

Let's create 2 files:

  • main.py - Our main script which we will debug
  • startup.py - Modules, functions or something else that we would like to import.

main.py content:

sentence = 'Hello Debugger'


def replace_spaces_with_hyphens(s):
    return s.replace(' ', '-')


replace_spaces_with_hyphens(sentence) # <- PLACE BREAKPOINT!

When breakpoint is hit, this is what we have inside scope:

Stack

If you always find yourself importing some modules and creating some functions, you can define all that inside startup.py script and import everything as from startup import *.

startup.py:

# Example modules you always find yourself importing.
import random
import time

# Some function you always create because you need it.
def my_imported_function():
    print("Imported !")

Inside Python Debugger Console, use from startup import * as mentioned above and you would see all modules and function inside scope, ready for use.

Debug import

like image 191
Dinko Pehar Avatar answered Oct 17 '22 02:10

Dinko Pehar