Scientific project tutorial
In this tutorial, you will create a project to run and debug Python code with data visualization.
Before you start, make sure that conda is installed.
Add sample code
Create an empty Python project. Add a new Python file named main.py by right-clicking the project root and selecting . Then add the following code to the main.py file:
Process warnings shown for the numpy
and matplotlib
imports and enable the packages in the project.
Run
Run your project . The code is executed and shows two graphs in the Plots tool window. Clicking the preview thumbnail displays the respective graph:
![Preview the graph Preview the graph](https://resources.jetbrains.com/help/img/idea/2024.2/py_matplotlib_run.png)
You can modify the project code to plot only one graph at a time. You can execute fragments of your code by creating code cells.
Execute code cells
Modify the main.py file by adding the "#%%" lines.
In the gutter, click the icon
on the line with the scatter plot cell mark. Only the scatter graph will be built.
Now click the icon
on the line with the y versus x plot cell mark. The corresponding graph should appear.
Debug
Let's put a breakpoint at the line:
This line appears twice in our example code, and so there will be two breakpoints.
Right-click the editor background and from the context menu choose Debug <project_name>.
You see the Debug tool window and the grey characters in the editor. This is the result of the inline debugging, which is enabled.
The line with the first breakpoint is highlighted. It means that the debugger has stopped at the line with the breakpoint, but has not yet executed it. If we execute this line (for example, by clicking the Step Over on the stepping toolbar of the Debug tool window), we'll see the graph:
![Debugging the matplotlib code Debugging the matplotlib code](https://resources.jetbrains.com/help/img/idea/2024.2/py_matplotlib_debug.png)
Next, look at the Variables tab of the Debug tool window. If you click the View as Array link next to the area
array, the Data View tool window opens:
![View as array View as array](https://resources.jetbrains.com/help/img/idea/2024.2/py_matplotlib_view_as_array.png)
Mind the only column in the table - it's explained by the fact that the area
array is one-dimensional.
Run in console
Right-click the editor background and choose the Run File in Python Console command.
This command corresponds to running a run/debug configuration for the main.py file with the Run with Python console checkbox selected:
![Run with Python console Run with Python console](https://resources.jetbrains.com/help/img/idea/2024.2/ij_run_config_for_command_line.png)
When this command is run, the >>>
prompt appears after the output in the Run tool window, and you can execute your own commands.
Summary
So, what has been done with the help of IntelliJ IDEA?
The file main.py was created and opened for editing.
The source code has been entered
The source code has been run and debugged.
Finally, we ran the file in console.