3 minutes read

When you have created your project in PyCharm and added some Python scripts, it is time to execute it. PyCharm provides several methods to execute code including the ability to execute the entire file or code selection. You can use the Python console to run code fragments and stand-alone commands. You can also use Run/Debug configurations to customize the way you're executing the code.

Run a Python script

Open your Python file in the editor. Then you have the following options to run it:

  • Right-click anywhere in the editor and select Run <filename>.

  • Alternatively, press Ctrl+Shift+F10 (Windows and Linux) or Ctrl+Shift+R (macOS).

  • If your code contains the main clause, click the run icon in the gutter, and then select Run <filename>.

The output of each Python script is displayed in its tab of the Run tool window. If you re-run an application, the new output overwrites the contents of the tab. To preserve the output of an application, even if you re-run it, pin the output tab.

That was the simplest way to run your Python code in PyCharm. However, in some cases, you may need to customize the way you execute your code. Moreover, you may need to run different Python applications using the same set of configuration options. Try Run/Debug configurations!

Use run/debug configurations

PyCharm uses run/debug configurations to run, debug, and test your code. Each configuration is a named set of startup properties that define what to execute and what parameters and environment should be used.

You can create a run configuration manually, or you can execute a Python file, and PyCharm will create a temporary configuration for that file with the default parameters. Later, you can modify and save it.

Look at PyCharm after you have executed your Python script:

The configuration for the Championship script appears in the list of the available run/debug configurations. You can modify it by clicking the Modify Run Configuration icon in the Run tool window.

You can open and edit the configuration by expanding the list of run/debug configurations and selecting Edit Configurations.

You should see a Python script run/debug configuration with the default parameters. You can alter them and fill in some optional parameters.

See the following reference in the official documents for more details: Python Run/Debug Configuration Parameters.

Read–Evaluate–Print–Loop (REPL)

If you have some experience with Python and know how to execute commands line by line, you can do it in PyCharm, too. The main reason for using the Python console from PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes. The console is always available in the lower group of tool windows.

Click Show variables on the console toolbar to preview the variables as you're executing Python commands. Also, you can click New Console to add a new Python console. You can rename each console to come up with meaningful names.

See Using a Python Console part of the official documentation for more details.

Read more on this topic in Testing Python Code 101 with PyTest and PyCharm on Hyperskill Blog.

Conclusion

To sum up:

  • You can execute your Python script using a context menu, pressing a shortcut, and clicking the icon next to the main clause.

  • Once you've executed a script, PyCharm creates a run/debug configuration. You can use it for the next runs, or to modify and customize the way you want to execute your script.

  • You can use the Python console to run commands and execute scripts.

296 learners liked this piece of theory. 12 didn't like it. What about you?
Report a typo