Creating and Optimizing Imports
When you reference a class that has not been imported, PyCharm helps you locate this file and add it to the list of imports. You can import a single class or an entire package, depending on your settings.
The import statement is added to the imports section, but the caret does not move from the current position, and your current editing session does not suspend. This feature is known as the Import Assistant.
The same possibility applies to XML files. When you type a tag with an unbound namespace, the import assistant suggests to create a namespace and offers a list of appropriate choices.
Creating imports on the fly
Import packages on-the-fly
- Start typing a name in the editor. If the name references a class that has not been imported, the following prompt appears:
If the pop-up annoys you, change this behavior for the current file. Just click Hector in the Status bar, and clear the check box Import Pop-up:
The unresolved references will be underlined, and you will have to invoke intention action Add import explicitly.
Press Alt+Enter. If there are multiple choices, select the desired import from the list.
You can define your preferred import style for Python code by using the following options available on the Auto Import page of the project settings ( ):
|from <module> import <name>||import <module>.<name>|
Sooner or later, some of the imported classes or packages become redundant to the code. PyCharm provides the Optimize Imports feature, which enables you, whenever it is convenient, to remove unused imports from your current file, or from all files in the current directory at once. This helps you avoid unused, excessive and duplicating imports in your project. One can remove unused import statements in the entire project or in the current file only.
Besides cleaning the code from the unused imports, PyCharm formats the existing import statements according to the Style Guide for Python Code. So doing, PyCharm splits import statements into separate lines, and sorts them into groups (refer to the Imports section for details).
Also, imports are sorted alphabetically and case-sensitively within the respective groups:
You can modify the sorting policy in the Import tab of the Python code style settings ( ). See Python Code Style Settings for more information.
Optimize imports in the entire project
Switch the focus to the Project tool window and do one of the following:
On the main menu, choose.
The Optimize Imports dialog opens.
If your project is under version control, the option Only VCS changed files is enabled. Select or clear this option as required.
One way of dealing with unused import is to use the quick-fix that appears when you set the caret at the highlighted unused import. However, you can optimize imports in a larger scope as described below.
Optimize imports in the current file:
On the main menu, choose.
Place the caret at the import statements, click , and choose Remove unused import.
Open the Reformat File Dialog (Ctrl+Shift+Alt+L) and select the Optimize imports checkbox.
Add import statements on code completion
You can disable auto import on completion and use quick-fixes instead:
In the Settings/Preferences dialog (Ctrl+Alt+S), click General under editor, and then click Auto Import. The Auto Import page opens.