How to use pycharm
How to use pycharm
Step 1. Create and run your first Python project
Before you start
Ensure that the following prerequisites are met:
You are working with PyCharm Community or Professional
You have installed Python itself. If you’re using macOS or Linux, your computer already has Python installed. You can get Python from python.org.
To get started with PyCharm, let’s write a Python script.
Create a Python project
Although you can create projects of various types in PyCharm, in this tutorial let’s create a simple Pure Python project. This template will create an empty project.
Choose the project location. Click button next to the Location field and specify the directory for your project.
Also, deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.
Python best practice is to create a virtualenv for each project. In most cases, PyCharm create a new virtual environment automatically and you don’t need to configure anything. Still, you can preview and modify the venv options. Expand the Python Interpreter: New Virtualenv Environment node and select a tool used to create a new virtual environment. Let’s choose Virtualenv tool, and specify the location of the environment and the base Python interpreter used for the new virtual environment.
Specify a path to the Python executable (in case of non-standard installation)
Download and install the latest Python versions from python.org
Install Python using the Command-Line Developer Tools (macOS only).
Now click the Create button at the bottom of the New Project dialog.
Create a Python file
Select the option Python File from the context menu, and then type the new filename.
PyCharm creates a new Python file and opens it for editing.
Edit Python code
Let’s start editing the Python file you’ve just created.
Start with declaring a class. Immediately as you start typing, PyCharm suggests how to complete your line:
PyCharm informs you about the missing colon, then expected indentation:
Note that PyCharm analyses your code on-the-fly, the results are immediately shown in the inspection indicator in the upper-right corner of the editor. This inspection indication works like a traffic light: when it is green, everything is OK, and you can go on with your code; a yellow light means some minor problems that however will not affect compilation; but when the light is red, it means that you have some serious errors. Click it to preview the details in the Problems tool window.
If you notice any inspection warnings as you’re editing your code, click the bulb symbol to preview the list of possible fixes and recommended actions:
Let’s copy and paste the entire code sample. Click the copy button in the upper-right corner of the code block here in the help page, then paste it into the PyCharm editor replacing the content of the Car.py file:
This application is intended for Python 3
At this point, you’re ready to run your first Python application in PyCharm.
Run your application
Use either of the following ways to run your code:
If you click this icon, you’ll see the popup menu of the available commands. Choose Run ‘Car’ :
PyCharm executes your code in the Run tool window.
Here you can enter the expected values and preview the script output.
Note that PyCharm has created a temporary run/debug configuration for the Car file.
The run/debug configuration defines the way PyCharm executes your code. You can save it to make it a permanent configuration or modify its parameters. See Run/debug configurations for more details about running Python code.
Summary
Congratulations on completing your first script in PyCharm! Let’s repeat what you’ve done with the help of PyCharm:
Get started
PyCharm is a dedicated Python Integrated Development Environment (IDE) providing a wide range of essential tools for Python developers, tightly integrated to create a convenient environment for productive Python, web, and data science development.
Choose the best PyCharm for you
PyCharm is available in three editions:
Community (free and open-sourced): for smart and intelligent Python development, including code assistance, refactorings, visual debugging, and version control integration.
Professional (paid) : for professional Python, web, and data science development, including code assistance, refactorings, visual debugging, version control integration, remote configurations, deployment, support for popular web frameworks, such as Django and Flask, database support, scientific tools (including Jupyter notebook support), big data tools.
Edu (free and open-sourced): for learning programming languages and related technologies with integrated educational tools.
Supported languages
To start developing in Python with PyCharm you need to download and install Python from python.org depending on your platform.
PyCharm supports the following versions of Python:
Python 2: version 2.7
Python 3: from the version 3.6 up to the version 3.11
Supported platforms
PyCharm is a cross-platform IDE that works on Windows, macOS, and Linux. Check the system requirements:
4 GB of free RAM
8 GB of total system RAM
Multi-core CPU. PyCharm supports multithreading for different operations and processes making it faster the more CPU cores it can use.
2.5 GB and another 1 GB for caches
SSD drive with at least 5 GB of free space
Officially released 64-bit versions of the following:
Microsoft Windows 8 or later
macOS 10.14 or later
Pre-release versions are not supported.
Latest 64-bit version of Windows, macOS, or Linux (for example, Debian, Ubuntu, or RHEL)
You can install PyCharm using Toolbox or standalone installations. If you need assistance installing PyCharm, see the installation instructions: Install PyCharm
Start with a project in PyCharm
Everything you do in PyCharm, you do within the context of a project. It serves as a basis for coding assistance, bulk refactoring, coding style consistency, and so on. You have three options to start working on a project inside the IDE:
Open an existing project
Begin by opening one of your existing projects stored on your computer. You can select one in the list of the recent projects on the Welcome screen or click Open :
Otherwise, you can create a project for your existing source files. Select the command Open on the File menu, and specify the directory where the sources exist. PyCharm will then create a project from your sources for you. Refer to the section Importing Project from Existing Source Code for details.
Check out an existing project from Version Control
You can also download sources from a VCS storage or repository. Choose Git (GitHub), Mercurial, Subversion, Perforce (supported in Professional edition only), and then enter your credentials to access the storage.
Then, enter a path to the sources and clone the repository to the local host:
Refer to the section Version control for details.
Create a new project
To create a project, do one of the following:
From the main menu, choose File | New Project
On the Welcome screen, click New Project
In PyCharm Community, you can create only Python projects, whereas, with PyCharm Professional, you have a variety of options to create a web framework project.
When creating a new project, you need to specify a Python interpreter to execute Python code in your project. You need at least one Python installation to be available on your machine.
For a new project, PyCharm creates an isolated virtual environment : venv, pipenv, poetry, or Conda. As you work, you can change it or create new interpreters. You can also quickly preview packages installed for your interpreters and add new packages in the Python Package tool window.
Look around
When you launch PyCharm for the very first time, or when there are no open projects, you see the Welcome screen. It gives you the main entry points into the IDE: creating or opening a project, checking out a project from version control, viewing documentation, and configuring the IDE.
When a project is opened, you see the main window divided into several logical areas. Let’s take a moment to see the key UI elements here:
Project tool window on the left side displays your project files.
Editor on the right side, where you actually write your code. It has tabs for easy navigation between open files.
Navigation bar above the editor additionally allows you to quickly run and debug your application as well as do the basic VCS actions.
Tool windows are specialized windows attached to the bottom and sides of the workspace and provide access to typical tasks such as project management, source code search and navigation, integration with version control systems, and so on.
The status bar indicates the status of your project and the entire IDE, and shows various warnings and information messages like file encoding, line separator, inspection profile, and so on. It also provides quick access to the Python interpreter settings.
Also, in the bottom-left corner of the PyCharm window, in the Status bar, you see the button or
. This button toggles the showing of the tool window bars. If you hover your mouse pointer over this button, the list of the currently available tool windows show up.
See the pages User interface and Tool windows to learn more about showing or hiding tool windows.
Code with smart assistance
When you have created a new project or opened an existing one, it is time to start coding.
Create a Python file
Select the option Python File from the context menu, and then type the new filename.
PyCharm creates a new Python file and opens it for editing.
PyCharm takes care of the routine so that you can focus on the important. Use the following coding capabilities to create error-free applications without wasting precious time.
Code completion
Code completion is a great time-saver, regardless of the type of file you’re working with.
Basic completion works as you type and completes any name instantly.
Smart type-matching completion analyzes the context you’re currently working in and offers more accurate suggestions based on that analysis.
Intention actions
PyCharm keeps an eye on what you are currently doing and makes smart suggestions, called intention actions, to save more of your time. Indicated with a lightbulb, intention actions let you apply automatic changes to code that is correct (in contrast to code inspections that provide quick-fixes for code that may be incorrect ). Did you forget to add some parameters and field initializers to the constructor? Not a problem with PyCharm. Click the lightbulb (or press Alt+Enter ) and select one of the suggested options:
The full list of available intention actions can be found in File | Settings | Editor | Intentions or PyCharm | Preferences | Editor | Intentions for macOS users.
Keep your code neat
PyCharm monitors your code and tries to keep it accurate and clean. It detects potential errors and problems and suggests quick-fixes for them.
The complete list of available inspections can be found under Settings | Editor | Inspections (or PyCharm | Preferences | Editor | Inspections for macOS users). Disable some of them, or enable others, plus adjust the severity of each inspection. You decide whether it should be considered an error or just a warning.
Generate some code
Writing code can be a lot easier and quicker when you use the code generation options available in PyCharm. The Code | Generate menu Alt+Insert will help you with creating symbols from usage, as well as suggest overriding or implementing some functions:
Use live templates (choose Code | Insert Live Template or press Ctrl+J ) to produce the entire code constructs. You can explore the available ready-to-use live templates In the Settings/Preferences dialog ( Ctrl+Alt+S ) (Settings | Editor | Live templates or PyCharm | Preferences | Editor | Live Templates if you are a macOS user).
Find your way through
When your project is big, or when you have to work with someone else’s code, it’s vital to be able to quickly find what you are looking for and dig into the code. This is why PyCharm comes with a set of navigation and search features that help you find your way through any code no matter how tangled it is.
Basic search
Search for usages
To find where a particular symbol is used, PyCharm suggests full-scale search via Find Usages Alt+F7 :
Project navigation
You can tell a lot just looking at your File Structure, with its imports or call hierarchies:
Also, you can navigate to:
The icons in the left-hand gutter can also help you with navigation:
Navigate through the timeline
Search Everywhere
If you have a general idea of what you’re looking for, you can always locate the corresponding element using one of the existing navigation features. But what if you want to look for something in every nook and cranny? The answer is to use Search Everywhere!
To try it, click the magnifying glass button in the upper right-hand corner of the window, or invoke it with Double Shift (press Shift twice).
Run, debug and test
Now when you’ve played with the code and discovered what you can do with it, it’s time to run, debug and test your app.
The easiest way to run an application is to right-click in the editor, and then choose Run from the context menu:
If your Python script contains the __main__ clause, then you can click the button in the gutter, and then choose the desired command.
You can see the your script execution in the Run tool window.
When you run your application for the very first time, PyCharm automatically creates the temporary Run/Debug configuration. You can modify it to specify or alter the default parameters and save it as a permanent Run/Debug configuration.
See how to tune run/debug configurations in Run/debug configurations.
Debug
Does your application stumble on a runtime error? To find out what’s causing it, you will have to do some debugging. PyCharm supports the debugger on all platforms.
Debugging starts with placing breakpoints at which program execution will be suspended, so you can explore program data. Just click the gutter of the line where you want the breakpoint to appear.
Refer to the section Debugging for details.
It is a good idea to test your applications, and PyCharm helps doing it as simple as possible.
With PyCharm, you can:
Run and debug tests right from the IDE, using the testing run/debug configurations.
To learn about the numbers, read the Test Runner tab section.
PyCharm supports all the major Python testing frameworks:
For each of these frameworks, PyCharm provides its own run/debug configuration.
Refer to tutorial Step 3. Test your first Python application and to the Run tests section for details.
With PyCharm Professional you can run, debug, and test your Python code remotely. You can deploy your local applications to some remote server. To learn about deployment servers, refer to the section Configuring Synchronization with a Web Server. PyCharm Professional also helps compare local and remote folders, and synchronize the local copy with that deployed on the server.
Keep your source code under Version Control
If you are keeping your source code under version control, you will be glad to know that PyCharm integrates with many popular version control systems: Git (or GitHub), Mercurial, Perforce (supported in Professional edition only), Subversion. To specify credentials and any settings specific to a particular VCS, go to Settings | Version Control (or PyCharm | Preferences | Version Control if you are a macOS user).
The VCS menu gives you a clue about what commands are available. For example, you can see the changes you’ve made, commit them, create changelists and much more from the Local Changes view: VCS | Show Changes (or just press Alt+9 ). Also, find some VCS basic commands in the Navigation bar above the editor:
Refer to the section Version control for details.
Local history
In addition to traditional version control, you can use the local history. With Local History, PyCharm automatically tracks changes you make to the source code, the results of refactoring, and so on
Process data
PyCharm has an interactive Python console to perform smart operations over data with on-the-fly syntax check with inspections, braces and quotes matching, and of course, code completion. You can also benefit from the built-in support for Anaconda.
With the R plugin installed in PyCharm, you can perform various statistical computing using R language and use coding assistance, visual debugging, smart running and preview tools, and other popular IDE features.
As you might have noticed already, creating projects of the various types (Django, for example) requires a data source. It is also quite possible that you inject SQL statements into your source code.
PyCharm Professional does not enable you to create databases, but provides facilities to manage and query them. Once you are granted access to a certain database, you can configure one or more data sources within PyCharm that reflect the structure of the database and store the database access credentials. Based on this information, PyCharm establishes a connection to the database and provides the ability to retrieve or change information contained therein.
Access to the databases is provided by the Database window ( View | Tool Windows | Database ). This tool window allows you to work with the databases. It lets you view and modify data structures in your databases, and perform other associated tasks.
Customize your environment
Feel free to tweak the IDE so it suits your needs perfectly and is as helpful and comfortable as it can be. Go to File | Settings ( PyCharm | Preferences for macOS users) to see the list of available customization options.
Appearance
The first thing to fine-tune is the general «look and feel.» Go to File | Settings | Appearance and Behavior | Appearance ( PyCharm | Preferences | Appearance and Behavior | Appearance for macOS users) to select the IDE theme: the light themes or Darcula if you prefer a darker setting.
Editor
The many pages available under File | Settings | Editor ( PyCharm | Preferences | Editor for macOS users) help you adjust every aspect of the editor’s behavior. A lot of options are available here, from general settings (like Drag’n’Drop enabling, scrolling configuration, and so on.), to color configuration for each available language and use case, to tabs and code folding settings, to code completion behavior and even postfix templates.
Refer to the section Configuring PyCharm settings for details.
Code style
Code style can be defined for each language under File | Settings | Editor | Code Style ( PyCharm | Preferences | Editor | Code Style for macOS users). You can also create and save your own coding style scheme.
Keymap
PyCharm uses the keyboard-centric approach, meaning that nearly all actions possible in the IDE are mapped to keyboard shortcuts.
The set of keyboard shortcuts you work with is one of your most intimate habits — your fingers «remember» certain combinations of keys, and changing this habit is easier said than done. PyCharm supplies you with a default keymap (choose Help | Keyboard Shortcuts PDF from the main menu) making your coding really productive and convenient. However, you can always change it going to File | Settings | Keymap ( PyCharm | Preferences | Keymap for macOS users).
There are also some pre-defined keymaps (like Emacs, Visual Studio, Eclipse, NetBeans and so on), and you can also create your own keymap based on an existing one.
Refer to the section Configure keyboard shortcuts for details.
That’s it! Go ahead and develop with pleasure!
We hope this brief overview of essential PyCharm features will give you a quick start. There are many important features that make a developer’s life easier and more fun, and the source code neater and cleaner. Take these first few steps now, and then dig deeper when you feel the time is right:
Enjoy PyCharm! With any questions visit our Discussion Forum, twitter and blog, where you can find news, updates, and useful tips and tricks. Also, don’t hesitate to report any problems to our support team) or the PyCharm issue tracker.
Python IDE: PyCharm Tutorial for Beginners
The PyCharm IDE is one of the most popular editors used by professional Python developers and programmers. But if you are a beginner, don’t worry, it can help you too! Find out how to create Python projects using PyCharm and what basic features can help you write code more efficiently.
The PyCharm IDE is one of the most popular editors used by professional Python developers and programmers. The vast number of PyCharm features doesn’t make this IDE difficult to use–just the opposite. Many of the features help make Pycharm a great Python IDE for beginners.
If you are just starting to learn Python, you should try out different Python IDEs to see if you prefer working with PyCharm or with another editor.
In my previous article, I showed you how to install the PyCharm IDE on Windows and Ubuntu. In this article, you’ll find out how to create a program and run it using PyCharm. You’ll also get to know some of the features that help beginners write code.
After the PyCharm IDE, we’ll move on to other popular Python IDEs and review them. Follow our blog or subscribe at the bottom to get the hang of other IDEs like Spyder, Jupyter, and more!
How to create and run Python scripts in PyCharm
To create a Python script right click on the project name node and choose from the pop-up menu New › Python File.
The New Python file window appears.Enter the name of the file in the Name field.
Click OK and you are ready to code in the PyCharm editor.
Copy this example code into the editor:
Save the file by clicking on the title tab,
then pressing Ctrl+S or by clicking File › Save All.
How to Run Python Code in PyCharm
We can run a project or a single file by selecting the project/file node and choosing from the menu Run › Run (the green Run button) or by using a keyboard combination Alt+Shift+F10.
The Run window appears:
Click the name of your script.
At the bottom of the editor the Output window with the result will be shown.
User Interface of the Editor
In almost all Python IDEs, the working area is divided into several parts that help make working with files and code more convenient. The screen below illustrates these different parts and what they are doing.
On the top is the main menu, which contains commands like open file, open project, create project or file, run, debug code, etc. Under the main menu is a toolbar with the same functionality, but it provides quick access to these functions. At the bottom is a status bar that displays messages about errors, warnings, and the status of a project. The central part of the screen consists of the editor where you read and write code. To the left, right, and bottom of the editor are tool windows. They allow for many different tasks, for example: see the node of the project and quickly open it, search in files, run options, change debugging settings, etc.
How to Customize the PyCharm Theme and Color Scheme
The PyCharm IDE allows you to customize many options. The first custom function is the theme of the IDE. You have a choice among different themes. To access the themes click on File › Settings.
The Settings window appears. From the left pane, expand the Editor and Color Scheme menus.
In the right pane select from the Scheme drop-down list one of the scheme names. For example it may be currently set to the Default scheme. Please note: the default color in PyCharm is Darcula and here, Default means your OS’s default theme.
Click the OK button.
If the Change PyCharm Theme window appears, click the Yes button. You will see a new theme like this below.
You can also change font colors in the scheme. You’ll find all the available options in the Settings window.
PyCharm Features with Shortcuts
Code Highlighting
The main feature that helps developers in writing code is highlighting. The editor highlights symbols and keywords in the code. It helps a developer to find these words in the script fast and it helps to notice errors. For example strings are green in color, integers are in blue, functions are in dark blue, etc. You can also change colors for groups of words in the settings of the IDE. Syntax highlighting improves the readability of the code.
If you click on the symbol or word, all occurences of this word or symbol will be highlighted.
PyCharm also helps you with errors. If you make a mistake in the code, this word will be indicated by red underlining. Hovering over this word with the mouse displays a tooltip with details.
Code Completion
Another very important feature, especially if you are just starting to learn Python, is code completion. While writing code you don’t always remember all the keywords in Python. If you start writing a word, the IDE gives you suggestions in a pop-up list, from which you can choose the appropriate word. It’s very convenient.
You can invoke code completion with the Ctrl+Space command.
If you write code without importing a required package, you’ll see this underlined in red. Select the word and press Alt+Enter or click the red bulb.
The IDE helps by suggesting that you import this package. Click on it and the line containing import will be inserted into the code. Now you can use the code with functions from the package math.
PyCharm also helps you remove unused imports if you remove code that depended on a package. If you see an orange bulb, expand it and choose the Optimize imports option.
You can do it also by pressing Ctrl+Alt+O or by choosing from the menu Code › Optimize imports.
Input Definition
The next helpful feature is the ability to see the definition of a function. If, for example, you type input, but forgot how many and which parameters this function takes, you can display the documentation about this function. Just select the word and choose from the menu View › Quick Definition.
You can also choose Quick Documentation.
Expression Type
Python doesn’t require declaration of data types for variables. However, during writing many lines of code, sometimes you need to find out what data type is assigned to a variable. In PyCharm it is easy. Select the variable and choose from the menu View › Expression type.
Of course you can also use the keyboard shortcut Ctrl+Alt+P. In a popup window you see the data type.
Code Folding
The next useful feature is hiding parts of code. If your code is long, it is very useful to hide sections of code to improve readability. You can select a block of code to hide and choose from the menu Code › Folding › Fold Selection.
The selected code will be folded.
If you click the plus symbol («+»), you expand the block of code.
Code Constructs with Surround Templates
PyCharm provides adding surrounding fragments of code with constructs based on the language of the source code like if. For example, you can write only the condition for an if statement and select it.
Then you can choose from the menu Code › Surround With or press Ctrl+Alt+T.
The list of statements appears.
If you choose if expr, your selected code will be inserted as the condition of an if expression.
Code Inspection
In PyCharm you can also easily navigate to the declaration of a variable, function, etc. Select the name of an object and press Ctrl+B. In the screen below we selected the a variable.
PyCharm allows for analysis of code by inspection. It detects compiling errors, unused code, memory leaks, and other problems. By default it analyses all open files and highlights all detected problems. Just select from the menu Code › Inspect Code.
The Specify Inspection Scope window appears, where you can choose to inspect all of a project or one file.
Click the OK button.
At the bottom of the IDE you see errors and warnings. In our script there are missing spaces around operators and whitespace before the ‘:’ character.
If there are not any errors or warnings, you see a message about it.
How to Install Packages in the PyCharm IDE
PyCharm is similar to other Python IDEs in that it allows you to install packages and modules. In PyCharm this is a very easy process. First choose from the menu File › Settings › Project: project name › Project Interpreter
You will see the Settings window:
Here is displayed a list of installed packages. To install a new package, click the plus icon, which is next to the names of the columns. The Available Packages window is displayed. Find the package and click the Install Package button at the bottom of this window.
You see the new package in the list.
Click the OK button.You are ready to use the package in your project! 🙂
Summary
This article is a short beginner overview of using the PyCharm IDE for growing your Python programming skills. I showed you how the PyCharm user interface works, how to run scripts, and what features can help you in writing code.
Please note, this editor contains many more features and tools, but I discussed only the most useful functions available in the Community version of the editor. If you haven’t used an IDE before, maybe PyCharm will turn out to be your favorite for writing Python scripts. Try it out and have fun!
Code Running Assistance
Prerequisites
You have already created a Python project and populated it with the following code:
You have Python interpreter already configured. Note that for the current project your Python interpreter version should be 3.0 or later.
First run
The script runs. Enter values for a, b, and c to review the output in the Run tool window:
Let’s explore in detail what we’ve done and what we see.
Each script or test you wish to run or debug from within PyCharm, needs a special profile that specifies the 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 from 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, marked with the green circle. It means that the Solver run/debug configuration has been created automatically by PyCharm, when you’ve chosen Run ‘Solver’ from the context menu. Now, as this run/debug configuration is marked with the green circle, it is current.
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
Edit run/debug configuration
Here you can see the Solver configuration and its default settings.
The default configuration options are derived from the predefined Python template. Click the Edit configuration templates link, to preview and modify the patterns.
If you change anything in one of the existing run/debug configurations, then only this particular run/debug configuration will be affected.
Redirect standard input from a text file
You can use the Run/Debug Configuration dialog to automatically submit input values from a text file instead of typing them in the Run tool window.
Create the in.txt file with the following values of a, b, and c:
To enable redirecting, select the Redirect input from checkbox and specify the path to the in.txt file.
Save the Run/Debug configuration and run it. Preview the execution results in the Run tool window.
Pass parameters to the running script
When running your Python scripts, you can pass various values as command-line arguments. Use the Parameter field in the Python run/debug configuration to add a parameter or to insert a macro.
Modify the code sample so that it can use a command-line argument value instead of «4» in the calculations.
In the Run/Debug Configurations dialog, select Solver from the list of the Python run/debug configurations. Then, click + in the Parameters field and select ClipboadContent from the list of the available macros.
In this example, the clipboard contains the value 76 and it is shown in the Macro preview area.
Click Insert to add the selected macro.
Run the Solver run/debug configuration. Note that the command line now contains «76» as an argument.
Copy any numeric value into the clipboard and rerun the configuration to evaluate the macro.
Similarly, you can include other helpful macros:
$FilePath$ : for the file path of the open file
$ModuleSdkPath$ : for the project interpreter path
$Prompt$ : for a string input dialog on running the configuration
Regular run
You’ve already executed the Solver script in one of the most straight-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:
On 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.
Now you can observe results in the Run tool window.
Test run
Selecting a test runner
First, choose the test runner. To do that, click on the main toolbar to open the Settings/Preferences dialog, and under the Tools node, click Python Integrated Tools page. Here is where you will select the default test runner:
In this case, this is Unittests. Apply changes and close the dialog.
Creating a test
Click the suggested action, and PyCharm will show the following dialog:
Click OK, and see the test class opened in the editor:
PyCharm has produced a test class for us. However, this is but a stub, and lacks the actual testing functionality. So, we’ll import the class to be tested, and add a test method. The resulting code might be as follows:
if you have a project directory structure, you will need to reflect that in the auto-completion of your import statement ( from Solver import Solver ).
Running a test
Launch it and observe results in the Test Runner tab of the Run tool window:
Debug run
First of all, why do we need debugging? Suppose, you hit a runtime 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.
A breakpoint is a line of the source code, where PyCharm will suspend, when this line is reached. PyCharm discerns several types of breakpoints, each one denoted with its own icon.
Here we’ll use the Python line breakpoints.
Setting breakpoints
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.
Debugging session
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 next?
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.
Next to the executed lines in the editor, the values of the variables appear.
The Debug tool window appears. This tool window shows all the important information related to debugging, and allows managing the debugging process.
Refer to the product documentation for details.
Working in the Debugger tab
OK, we’ve paused at the first breakpoint. What’s next?
Press F9 or click . The program 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, refer to the part Debug of the documentation.
Refer to the section Debugger tab for details.
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.
The Python prompt appears, and the console becomes 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.
You can always invoke the debug console by using the command Tools | Open Debug Command Line from the main menu.
Finally, if you are used to working with an Python console, you can also do that right from within PyCharm. To launch the console, choose Tools | Python Console. from the main menu:
You can handle the script execution in the interactive mode:
Python console enables executing Python commands and scripts line by line, similar to your experience with Python Shell.
Working with Python console
The main reason for using the Python console within PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes.
You can use up and down arrow keys to browse through the history of executed commands, and repeat the desired ones. To preview the variable values calculated in the course of the execution, click and check the Special Variables list.
The console is available for all types of Python interpreters and virtual environments, both local and remote.
Preview a variable as an array
When your variables are numpy arrays or dataframes, you can preview them as an array in a separate window. To try it, do one of the following:
Click the link View as Array / View as DataFrame :
From the context menu of a variable, choose View as Array / View as DataFrame :
The variable will be opened in the Data tab of the SciView window.
Run source code from the editor in console
Open file in the editor, and select a fragment of code to be executed.
Watch the code selection execution:
By default, the Python console executes Python commands using the Python interpreter defined for the project. However, you can assign an alternative Python interpreter.
Configure Python console settings
Select any available interpreter from the Python interpreter list. Note that you cannot introduce a new interpreter here. If you want to come up with the new interpreter, you need to create it first.
In needed, click the Configure Interpreters link to inspect the list of the installed packages and add new ones.
Mind the code in the Starting script area. It contains the script that will be executed after you open the Python console. Use it to pre-code some required Python commands.
When working on several Python scripts, you might want to execute each in a separate Python console.
Run several Python consoles
Click to add a new Python console.
All the commands you’re running in the Python console, are executed one by one. If the commands require substantial time to get executed, you might want to preview and manage the execution queue.
Manage the command execution queue
In the Python Console Command Queue dialog, review the list of commands. If needed, click to delete the command from the queue.
Note, once the command is executed, it disappears from the queue. To preview all previously executed commands browse the console history ().
Editor basics
The PyCharm editor is the main part of the IDE that you use to create, read and modify code.
The editor consists of the following areas:
The scrollbar shows errors and warnings in the current file.
Breadcrumbs help you navigate inside the code in the current file.
The gutter shows line numbers and annotations.
Tabs show the names of the currently opened files.
Navigation
You can use various shortcuts to switch between the editor and different tool windows, change the editor size, switch focus, or return to the original layout.
Maximize editor pane
You can maximize a split screen as well. In this case the active screen is maximized and other screens are moved aside.
Switch the focus from a window to the editor
Return to the editor from the command-line terminal
Return to the default layout
Jump to the last active window
Use the switcher for navigation
Keep Ctrl pressed to leave the switcher popup open.
Press Tab to move between elements. Press Backspace to remove the selected file from the list and close it in the editor.
Change the IDE appearance
You can switch between schemes, keymaps, or viewing modes.
You can also find and adjust the color scheme settings including the high contrast color scheme for people with eyesight deficiency on the Editor | Color Scheme page and the keymap settings on the Keymap page of the Settings/Preferences dialog ( Ctrl+Alt+S ).
Jump to the navigation bar
Scrollbar
When you work with code in the editor, PyCharm displays code analysis results that include errors and warnings on the scrollbar. You can check whether your code has issues and quickly navigate to them.
The top of the scrollbar has the Inspections widget that gives you a brief summary of the code problems. Click the widget get more information on each detected problem in the Problems tool window.
The stripes on the scrollbar indicate places where PyCharm found a problem. Hover over a stripe to see a tooltip describing the problem or click the stripe for a quick navigation.
It is normal to see many stripes while you are working on a file. Many of these errors, warnings, and suggestions are eventually resolved as you complete the code. Should any errors remain when you feel your code is complete we recommend that you explore and resolve them before compiling your project.
The different colors of stripes indicate severity of the problems from an error marked in red to a TODO comment marked in blue, but you can change the displayed colors if you need. For more information, refer to Change inspection severity.
Editor tabs
You can close, hide, and detach editor tabs. Every time you open a file for editing, a tab with its name is added next to the active editor tab.
You can use the tab’s context menu for the same purpose or located in the editor.
To configure the settings for editor tabs, use the Editor | General | Editor Tabs page of the Settings/Preferences dialog ( Ctrl+Alt+S ). Alternatively, right-click a tab and select Configure Editor Tabs from the list of options.
Open or close tabs
To close all opened tabs, select Window | Editor Tabs | Close All Tabs from the main menu.
To close all inactive tabs, press Alt and click on the active tab. In this case, only the active tab stays open.
To open a new tab at the end of the already opened one, select the Open new tabs at the end in the tab settings.
Copy path or filename
Right-click the tab.
From the list that opens, select your copy option.
PyCharm copies the item to the clipboard and you can paste it ( Ctrl+V ) wherever you need.
Move, remove, or sort tabs
To move or remove the icon on a tab, in the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | General | Editor Tabs and select the appropriate option in the Close button position field.
To place the editor tabs in a different part of the editor frame or hide the tabs, right-click a tab and select Configure Editor Tabs to open the Editor Tabs settings. In the Appearance section, in the Tab placement list, select the appropriate option.
To access the Editor Tabs settings when all tabs are hidden, select Window | Editor Tabs | Configure Editor Tabs from the main menu.
Pin or unpin a tab
You can pin an active tab in the editor so that it will stay open when the tab limit is reached or when you use the Close Other Tabs command.
To pin or unpin an active tab, right-click it and select Pin Tab or Unpin Tab from the context menu.
Detach a tab
When you detach a tab, the tab opens in a separated window and the window becomes reserved for the detached tab. If you try to detach another tab from the main frame, it will be opened in the new window.
Drag the tab you need outside of the main window and drag the tab back to attach it.
You can also use Alt+mouse for the same action.
Switch between tabs
You can also switch between recently viewed tabs or files.
Assign a shortcut for the opened tab
In the list of directories, click the Other directory and from the list of tabs, select the one for which you need to add a shortcut. The limit of tabs to which you can assign shortcuts is 9.
Change the default tab limit
PyCharm limits number of tabs that you can open in the editor simultaneously (the default tab limit is 10).
If the tab limit equals to 1, the tabs in the editor will be disabled. If you want the editor to never close the tabs, type some unreachable number.
Open files in the preview tab
The preview tab allows you to view files in a single tab one by one without opening each file in a new tab. This is helpful if you need to look through several files without exceeding the tab limit.
In the Project tool window, select a file that is not already open in any other tab.
The name of the file is written in italic to indicate the preview mode. Any other file that you select will replace the previous one in the preview tab.
Start editing the file to exit the preview mode and change it to a regular tab.
Note that when the preview tab is enabled, the Open Files with Single Click option is ignored. Double-click a file to open it in a regular tab.
Hide editor tabs if there is no more space
Select the Hide tabs if there is no space option. Extra tabs will be placed in the list located in the upper right part of the editor.
Change the font size in tabs
In the Size field, specify the font size and click OK to save the changes.
Keep in mind that the font size will change not only for tabs, but for tool windows as well.
Split screen
PyCharm offers various actions that you can invoke from main or context menu, editor, or the project tool window to split the editor screen.
In the editor, right-click the desired editor tab and select how you want to split the editor window ( Split Right or Split Down ). PyCharm creates a split view of the editor and places it according to your selection.
As an alternative, from the main menu, select Window | Editor Tabs and the Split and Move Right or Split and Move Down option.
You can drag a tab vertically or horizontally in order to split the editor, and drag the tab back to unsplit the screen.
You can open a file in the editor in the right split.
In the Project tool window, right-click a file and select Open in Right Split from the context menu. PyCharm will open a file in the split on the right of the editor.
If there are two splits and focus is in the left split, the file will be opened in the existing right split. If the focus is in the right split, the file will be opened in the next right split.
You can close all the open tool windows at once and thus enlarge the split screens. Position the caret in an editor tab and press Ctrl+Shift+F12 or just double-click the tab. PyCharm hides all the open tool windows so all the space is shared by split screens.
You can split the editor equally multiple times with the Equalize proportions in nested splits option.
To unsplit the screen, from the context menu, select Unsplit or Unsplit All to unsplit all the split frames.
Move the split screen
Place the caret inside the desired split frame.
From the list of options, select one of the following options:
Stretch Editor to Top
Stretch Editor to Left
Stretch Editor to Bottom
Stretch Editor to Right
You can assign a shortcut to each option and use a keyboard to stretch the split frame.
Quick popups
View quick definition of a symbol (tag, class, method/function, field, and so on.)
PyCharm displays the information in a popup. If you need, click the icon to open the Find tool window with the item’s definition and its usages.
View quick documentation for a code element or a file.
PyCharm displays a popup with the appropriate information.
You can press Ctrl+Q twice to open the popup in the Documentation tool window. (Press same shortcut to switch back to the popup).
If you need to change the font size of the text displayed in the popup window, click the icon and in the window that opens, change the font size according to your preferences.
External Python documentation becomes available when you properly configure it in the Settings/Preferences | Tools | Python External Documentation.
The action shows the current method or class declaration when it is not visible.
View a description of the error or warning at the caret.
Click the code analysis marker to see the list of found errors. Use F2 or Shift+F2 to navigate to next or previous error.
Copy tooltip text to the clipboard.
Hold Alt (on Linux, hold Ctrl+Alt ) and click the tooltip
To control the usages highlighting, select or clear the Highlight usages on element at caret option, in Settings/Preferences | Editor | General (the Highlight on Caret Movement area).
If you turn on the Power Save mode ( File | Power Save Mode ), the usages are not highlighted.
You can configure and change font, its size and font ligatures. You can apply the font size change for only one tab in the editor or increase and decrease the font size globally.
Configure the font and font ligatures
Select the needed font from the list, specify its size and the line heights. You can also enable ligatures and configure typography settings.
Click OK to save the changes.
Change the font size in the editor
In the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | General ( Mouse Control section).
Select the Change font size with Ctrl+Mouse Wheel in option.
You can configure the editor size on the Font page of the editor settings.
Increase or decrease font size for all the editors
In the editor, press Alt+Shift+. to increase the font size globally or press Alt+Shift+Comma to decrease it.
PyCharm displays a zoom indicator on the bottom of the editor that shows the current font size and the default one to which you can reset the current font size.
Useful editor configurations
You can use the Settings/Preferences dialog ( Ctrl+Alt+S ) to customize the editor’s behavior.
Check the following popular configurations:
Configure code formatting
From the list of languages select the appropriate one and on the language page, configure settings for tabs and indents, spaces, wrapping and braces, hard and soft margins, and so on.
Configure the color scheme settings for different languages and frameworks
Open the Color Scheme node and select the needed language or framework. You can also select the General option from the node’s list to configure the color scheme settings for general items such as code, editor, errors and warnings, popups and hints, search results, and so on.
Configure code completion options
Configure the caret placement
Select the Allow placement of caret after end of line option to place the caret at the next line in the same position as the end of the previous line. If this option is cleared, the caret at the next line is placed at the end of the actual line.
Select the Allow placement of caret inside tabs option to help you move the caret up or down inside the file while keeping it in the same position.
Configure the behavior of trailing spaces on save
For example, when you save your code either manually or automatically and want to preserve trailing spaces on the caret line regardless of what option is selected in the Strip trailing spaces on save list, select the Always keep trailing spaces on caret line option.
Configure the editor appearance options
For example, you can configure showing the hard wrap guide, or showing parameter hints.
Manage the appearance for long lines
In the Soft Wraps section, specify the appropriate options.
For example, you can specify file types to which you want to apply soft wraps. It might be helpful when you write documentation in markdown files.
Configure smart keys
You can configure a certain behavior for different basic editor actions depending on the language you use.
For example, for Java, SQL or Python, you can select the Jump outside closing bracket/quote with Tab option to enable navigation outside the closing brackets or quotes with Tab when you type your code.