- First run
- Run/Debug Configuration - what is it?
- Regular run
- Test run
- Debug run
- REPL - Running an interactive console
- You are working with PyCharm 2.7 or higher
- You have already created a Python project and populated it. You can use a project created in the Getting Started tutorial, and the class Solver.
Off we go. Open the Solver.py for editing and right-click the editor background. Then choose Run 'Solver' on the context menu:
The script runs and shows output in the Run tool window:
Let's explore in detail what we've done and what we see.
Run/Debug Configuration - what is it?
Each script or test you wish to run or debug from within PyCharm, needs a special profile that specifies script name, working directory, and other important data required for running or debugging. PyCharm comes with a number of such pre-defined profiles, or run/debug configurations that serve patterns, against which you can create any number of run/debug configurations of your own.
Every time you click the Run or Debug buttons (or choose Run or Debug commands on the context menu), you actually launch the current run/debug configuration in the run or debug mode.
If you look at the very first image, you will notice that in the combobox there is no run/debug configuration at all; on the second image it appears. It means that the Solver run/debug configuration has been created automatically by PyCharm, when you've chosen Run 'Solver' on the context menu. Now, as this run/debug configuration is shown in the combobox, it is current.
Look at the main toolbar: the current run/debug configuration is visible in the combobox. To its right you see the Run and Debug buttons; the run/debug configuration in the combobox is Solver:
You also see that its icon is shown semi-transparent. What does it mean? It means that the Solver run/debug configuration is temporary - PyCharm has created it automatically.
OK, now click the down arrow to reveal the available commands and, below the separator line, the list of existing run/debug configurations:
Should you have more run/debug configurations, the list of existing ones will become broader. If you click one of the run/debug configurations in this list, it will become current.
Save run/debug configuration
Choose this command to save the temporary run/debug configuration 'Solver' - now this configuration becomes permanent. As such, it gets the normal icon.
This command is first in the list. Choose Edit configuration, and see the Run/Debug Configurations dialog box open:
Here you see two nodes: Python and Defaults. Under the first node, there is a single 'Solver' configuration, under the second node you see a whole lot of run/debug configurations.
What does it mean?
Under the Defaults node, you see only the stubs, or patterns. If you create a new run/debug configuration, it is created on the grounds of the selected pattern. If you change anything under the Defaults node, then the corresponding run/debug configuration you create will be different.
For example, you want to change the Python interpreter to use a remote or other local interpreter. OK, select the interpreter of your choice in the Python page - then any newly created run/debug configuration of the Python type will use this interpreter.
Under the Python node, you see the only run/debug configuration "Solver'. It belongs to the Python type, and is created against the pattern Python. It is denoted with the icon of the normal opacity - which corresponds to the permanent run/debug configuration (remember, it became permanent because you've saved it - however, any specially created run/debug configuration also becomes permanent). As an example, create a new run/debug configuration of the Python type for the same Solver script, and call it 'Solver1'.
If you change anything in one of the existing run/debug configurations, then only this particular run/debug configuration will be affected.
You've already executed the Solver script in one of the most staright-forward ways. Let us now explore the other ways to run a script.
As you have already learnt, running a script means in fact launching the current run/debug configuration. So, to run the Solver script, follow this procedure:
- In the main toolbar, click the run/debug configuration combobox, and make sure that 'Solver' run/debug configuration is the current one.
- Do one of the following:
- Click the Run button , located next to the run/debug configuration combobox.
- Press Shift+F10
- On the main menu, choose Run → Run
Now you can observe results in the Run tool window.
We won't discuss here why testing is necessary - let's just assume that it is so, and discuss how PyCharm can help with it.
Selecting test runner
First, choose the test runner. To do that, click on the main toolbar to open Settings/Preferences dialog, and then click Python Intergated Tools page. Here is where you will select the default test runner:
In this case, this is Nosetests. Apply changes and close the dialog.
Creating a test
To run a test, you have to create it first. PyCharm suggests a smart way to stub out a test: click the class name and then press Ctrl+Shift+T, or on the main menu, choose Navigate → Test. If a test exists, you can jump directly to it; if it doesn't - PyCharm will create it:
Click the suggested action, and PyCharm will show the following dialog:
Click OK, and see the test class opened in the editor:
Great! PyCharm has produced a test class for us. However, this is but a stub, and lacks the actual testing functionality. OK, this is your task to create test methods.
Running a test
When ready, right-click the test class name - the Run node of the context menu shows Nosetest run/debug configuration:
Launch it and observe results in the Test Runner tab of the Run tool window:
First of all, why do we need debugging? Suppose, you hit a run-time error. How to find out its origin? This is where debugging is necessary.
With PyCharm, you can debug your applications without leaving the IDE. The only thing you need to do beforehand, is to place breakpoints in the required places. Let's explore this in details.
Breakpoint - what's this?
Here we'll use the Python line breakpoints.
This is definitely the easiest part of the process Just click the left gutter on the lines you want to explore - and the breakpoints are there:
Note that each breakpoint is denoted also with a red stripe over the entire line. This color corresponds to a breakpoint that has not yet been reached. Later we'll see how the line at breakpoint changes its color.
By the way, removing breakpoints is same easy - just click the left gutter again.
Hover your mouse pointer over a breakpoint. PyCharm shows a tooltip with the most essential breakpoint information - line number and script address. However, if you want to change breakpoint settings, you have to right-click a breakpoint:
Try changing breakpoint settings for your own, and see how the breakpoint icon changes.
So, we are now ready for debugging. Let's start.
First of all, select the same 'Solver' run/debug configuration from the run/debug configurations combobox, and click the Debug icon to the right:
What happens hext?
- PyCharm starts, and then suspends execution at the first breakpoint.
- The line at breakpoint becomes blue. It means that PyCharm has reached the line with the breakpoint, but has not yet executed it.
- The Debug tool window appears. This tool window shows all the important information related to debugging, and allows managing the debugging process.
Though the product documentation gives complete information about the buttons and controls of the Debug tool window, let's explore it briefly. We see two tabs: the Debugger tab and the Console tab.
- The Debugger tab is visible by default and consists of three areas: Frames, Variables, and Watches. These areas are the lists of frames and threads with their states, variables and watches. When you select a frame, you see the variables corresponding to the selected frame. Each area can be hidden or shown.
- The Console tab. To view the contents of the console and work in it, you have to click this tab first, to bring it forward.
You can change the location of the Debugger tab areas, and both Debugger and Console tabs, if you don't like the default location. Refer to the Moving tabs and areas section for details.
Working in the Debugger tab
OK, we've paused at the first breakpoint. The Frames area shows the first thread demo in the 7th line of the script Solver; the corresponding variables a,b,c are already defined, but the variable d is not. What's next?
Press F9 (or click in the toolbox to the left) - the programm will resume and pause at the next breakpoint. This way you can step through all the set breakpoints, observing the variables used in the application.
For more information about working with frames, watches, evaluating expressions etc. refer to the product documentation and to the.
Working in the Console tab
Why do we need it at all? For example, you'd like to see the error messages, or perform some calculations not related to the current application... With PyCharm this is not a problem.
Click the Console tab to bring it forward:
Then, in the toolbar of the tab, click the button . The Python prompt appears:
The console thus became interactive. Try to execute Python commands in this interactive console:
Note that interactive console provides code completion (Ctrl+Space) and history (Up/Down arrow keys). Refer to the page Using Debug Console for more information.
Finally, if you want the Console tab to be always visible, just move it to a separate window:
This command completely drops the current run or debug session and lauches the current run/debug configuration anew. To restart a running session, click the button in the toolbar of the run tool window, or in the toolbar of the Debug tool window.
REPL - Running an interactive console
Finally, if you are used to working with an interactive console, you can also do that right from within PyCharm. To launch the console, choose Tools → Run Python Console... on the main menu:
The interactive Python console runs in a separate tab of the Run tool window:
In this console, you can also do a lot of interesting things: enjoy code completion, history, executing each current statement etc.. Here we'll see how our ever-lasting Solver.py will work from within the Python console.
Open Solver.py in the editor (there're many ways to do it... as an exercise, try Ctrl+E - View → Recent Files). Then select all contents of the Solver.py file (Ctrl+A, or Edit → Select All), and press Alt+Shift+E (or on the context menu choose Execute Selection in Console):
Lo and behold! PyCharm has loaded the selected code in the Python console and executed it!
What have we done in this tutorial?
- Refreshed our memories on how to run a Python script in a most straight-forward way.
- Learnt about run/debug configurations.
- Ran a script.
- Created and ran a test.
- Debugged a script.
- Ran same script in a REPL console.