Code completion
This section covers various techniques of context-aware code completion that allow you to speed up your coding process.
Basic completion
Basic code completion helps you complete the names of classes, methods, and keywords within the visibility scope.
When you invoke code completion, PyCharm analyzes the context and suggests the choices that are reachable from the current caret position (suggestions also include Live templates) .
If basic code completion is applied to a part of a field, parameter, or variable declaration, PyCharm suggests a list of possible names depending on the item type.
Invoking Basic code completion for the second time shows the names of classes, functions, modules, and variables in the entire project, regardless of dependencies.
Invoke basic completion
Start typing a name.
Press Ctrl+Space or choose
from the main menu.The images below show basic code completion for the following cases:
Methods:
Method parameters:
Dictionaries:
Django templates:
File paths completion in Python string literals:
If necessary, press Ctrl+Space for the second time (or press Ctrl+Alt+Space).
This shows the names of classes, functions, modules, and variables.
Type-matching completion
Smart type-matching code completion filters the suggestion list and shows only the types applicable to the current context.
Invoke type-matching completion
Smart type-matching code completion filters the suggestion list and shows only the types applicable to the current context. To invoke type-matching completion, start typing your code and press Ctrl+Shift+Space. Alternatively, select from the main menu.
Statement completion
You can create syntactically correct code constructs by using statement completion Ctrl+Shift+Enter. It inserts the necessary syntax elements and gets you in a position where you can start typing the next statement.
Complete a method declaration
Start typing a method declaration and press Ctrl+Shift+Enter after the opening parenthesis.
PyCharm automatically completes a method declaration with the mandatory parameter
self
. Start typing a method declaration in a Python class, and PyCharm will insertself
after the opening bracket of the parameter list. This behavior is configurable in the Smart Keys page of the editor settings.
Complete a code construct
Start typing a code construct and press Ctrl+Shift+Enter.
PyCharm automatically completes the construct and adds the required punctuation. The caret is placed at the next editing position.
Complete statements in collections
PyCharm automatically inserts a trailing comma before a line break in multiline collection literals. In dict literals, it inserts a colon between a key and a value. Press Ctrl+Shift+Enter to invoke statement completion:
Note that because of the language ambiguity regarding syntax of some incomplete collection literals, a colon is inserted neither after the first key of a dict literal nor after the first item of a parenthesized tuple.
Hippie completion
Hippie completion is a completion engine that analyses your text in the visible scope and generates suggestions from the current context. It helps you complete any word from any of the currently opened files.
Expand a string at caret to an existing word
Type the initial string and do one of the following:
Press Alt+/ or choose
to search for matching words before the caret.Press Alt+Shift+/ or choose
to search for matching words after the caret and in other open files.
The first suggested value appears, and the prototype is highlighted in the source code.
Accept the suggestion, or hold the Alt key and keep pressing \ until the desired word is found.
Postfix code completion
Postfix code completion helps you reduce backward caret jumps as you write code. You can transform an already-typed expression to a different one based on a postfix you type after the dot, the type of expression, and its context.
Enable and configure postfix completion
In the Settings dialog (Ctrl+Alt+S), open and select the Enable postfix completion checkbox.
Select Tab, Space, or Enter to be used for expanding postfix templates.
Enable/disable a particular postfix template for the selected language.
Transform a statement with a postfix
Type an expression and then type a postfix after a dot, for example,
.if:
def f(a): a.ifThe initial expression gets wrapped with an
if
statement:def f(a): if a:
You can disable certain postfix completion templates in the Editor | General | Postfix Completion settings page Ctrl+Alt+S.
You can select Tab, Space, or Enter to expand postfix templates.
You can edit the predefined postfix templates, for example, to replace a long key with a shorter one, or to expand the list of applicable expression types.
Create custom postfix templates
Press Ctrl+Alt+S to open settings and then select
.Click the Add button () on the toolbar.
In the popup menu that opens, choose the language that you need to create a postfix template for.
In the Create New Template dialog that opens, specify Key, a combination of symbols after the dot that will invoke the template.
Select which expression types the new template will be applicable to, and type the target expression in the following format:
$EXPR$ <target_expression>
, for example,[x for x in $EXPR$]
.Add
$END$
where you want the caret to be placed after the template is applied, for example:[x$END$ for x in $EXPR$]To have PyCharm automatically transform the topmost applicable expression when the template is invoked, select the Apply to the topmost expression checkbox.
Otherwise, if the checkbox is cleared, PyCharm will prompt you to choose the expression when you invoke the template.
F-string completion
Whenever you open a curly brace in an ordinary string literal, PyCharm provides the same completion suggestions as for an f-string. When you select one of the suggested options, the IDE automatically converts the literal into an f-string by adding a missing f
prefix and a closing curly brace.
Auto import on module name completion
PyCharm automatically adds an import statement when you refer any module member or package in the Python code and invoke code completion. Auto-import on code completion is also applied to some popular package name aliases, such as np
for numpy
or pd
for pandas
.
Machine-learning-assisted completion ranking
PyCharm allows you to prioritize completion suggestions based on choices that other users made in similar situations.
The ML completion mechanism doesn't add any new elements but orders the elements retrieved from code. Data is not exposed anywhere; it is collected locally.
Enable ML completion
Press Ctrl+Alt+S to open settings and select Editor | General | Code Completion.
Under Machine Learning Completion Ranking, enable the Sort completion suggestions based on machine learning option, and select the languages for which you want to use ML completion.
Enable relevance markers
Press Ctrl+Alt+S to open settings and select Editor | General | Code Completion.
Enable the following options:
Mark position changes in the completion popup: use the and icons to indicate whether the relevance of a suggestion is increasing or decreasing and therefore the suggestion has moved up or down the suggestion list.
Mark the most relevant item in the completion popup: use the icon to indicate the most suitable suggestion on the list.
The suggestion list will look as follows with the icons marking reordered and the most relevant items.
Configure code completion settings
To configure code completion options, go to the Editor | General | Code Completion settings page Ctrl+Alt+S.
You can choose the following settings:
Item | Description |
---|---|
Match case | Select if you want the letter case to be taken into account for completion suggestions. Choose whether you want to match the case for the first letter or for all letters. |
Automatically insert single suggestions for | Automatically complete code if there's just one suggestion for basic and smart type-matching completion. |
Sort suggestions alphabetically | Select if you want to sort items in the suggestion list in the alphabetical order instead of sorting them by relevance. You can change this behavior at any time by clicking in the suggestion list and toggling the Sort by Name option. |
Show suggestions as you type | Select if you want the suggestion list to be invoked automatically, without having to call completion explicitly. This option is enabled by default. |
Insert selected suggestion by pressing space, dot, or other context-dependent keys | Select if you want to insert the selected suggestion by typing certain keys that depend on the language, your context, and so on. |
Show the documentation popup in | Select to automatically show a popup for each item in the suggestion list with the documentation for the class, method, or field currently highlighted in the lookup list. In the field to the right, specify the delay (in milliseconds), after which the popup should appear. |
Insert parentheses automatically when applicable | If this option is enabled, PyCharm automatically inserts a pair of opening and closing parentheses when you complete a function/method. Clear the checkbox to suppress inserting parentheses automatically. If you use an opening parentheses To use an opening parentheses For more information, refer to Use specific keys to insert suggestions. |
Completion tips and tricks
Open completion settings from the completion popup
You can quickly access code completion settings right from the completion popup.
Click the icon in the completion popup and select Code Completion Settings.
After that, the Editor | General | Code Completion settings page Ctrl+Alt+S opens.
Narrow down the suggestion list
You can narrow down the suggestion list by typing any part of a word (even characters from somewhere in the middle) or invoking code completion after a dot separator . PyCharm will show suggestions that include the characters you have entered in any positions.
This makes the use of wildcards unnecessary:
In case of CamelCase or snake_case names, type the initial letters only. PyCharm automatically recognizes and matches the initial letters.
Accept a suggestion
You can accept a suggestion from the list in one of the following ways:
Press Enter or double-click a list item to insert it to the left of the caret.
Press Tab to replace the characters to the right from the caret.
Use Ctrl+Shift+Enter to make the current code construct syntactically correct (balance parentheses, add missing braces and semicolons, and so on).
You can also use specific keys and custom characters to accept the selected completion suggestion. To enable these features, go to the Editor | General | Code Completion settings page Ctrl+Alt+S and do the following:
To use specific keys, select the Insert the selected suggestion by pressing space, dot, or other context-dependent keys checkbox. These keys depend on the language, your context, and so on.
To also use custom characters, enter the characters into the Additional characters to accept the completion field.
View reference
You can use the Quick Definition view by pressing Ctrl+Shift+I when you select an entry in the suggestion list:
You can use the Quick Information view by pressing Ctrl+Q when you select an entry in the suggestion list:
View code hierarchy
You can view code hierarchy when you've selected an entry from the suggestion list:
Ctrl+H - view type hierarchy
Ctrl+Shift+H - view method hierarchy.
Troubleshooting
If code completion doesn't work, this may be due to one of the following reasons:
The Power Save Mode is on ( ). Turning it on minimizes power consumption of your laptop by eliminating the background operations, including error highlighting, on-the-fly inspections, and code completion.
Your file doesn't reside in a content root , so it doesn't get the required class definitions and resources needed for code completion.
For more information, refer to Configuring Project Structure.
A file containing classes and functions that you want to appear in the completion suggestion list is marked as a plain text file.
External libraries that contain functions that you want to appear in the completion suggestion list are not added as dependencies or global libraries.
Code completion popup might not appear automatically if it takes too long to gather the completion options. For example, if the computer is busy with another task. In this case, you may still activate the completion popup manually via Ctrl+Space.