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. Using Import Assistant is the preferred way to handle imports in PyCharm because import optimizations are not supported via command line.
The same possibility applies to XML files. When you type a tag with an unbound namespace, the import assistant suggests creating a namespace and offers a list of appropriate choices.
Automatically add import statements
You can configure the IDE to automatically add import statements if there are no options to choose from.
In the Settings/Preferences dialog Ctrl+Alt+S, click .
In the Python section, configure automatic imports:
Select Show import popup to automatically display an import popup when tying the name of a class that lacks an import statement.
Select one of the Preferred import style options to define the way an import statement to be generated.
Disable auto import
If you want to completely disable auto-import, make sure that:
All import tooltips are disabled.
The automatic insertion of import statements is disabled.
The Optimize Imports feature helps you remove unused imports and organize import statements in the current file or in all files in a directory at once according to the rules specified in .
Optimize all imports
Select a file or a directory in the Project tool window ( ).
Do any of the following:
From the main menu, select(or press Ctrl+Alt+O).
From the context menu, select Optimize Imports.
(If you've selected a directory) Choose whether you want to optimize imports in all files in the directory, or only in locally modified files (if your project is under version control), and click Run.
Optimize imports in a single file
Place the caret at the import statement and press Alt+Enter or use the icon.
Select Optimize imports.
Optimize imports when committing changes to Git
If your project is under version control, you can instruct PyCharm to optimize imports in modified files before committing them to VCS.
Press Ctrl+K or selectfrom the main menu.
Click and in the Before commit area, select the Optimize imports checkbox.
Automatically optimize imports on save
You can configure the IDE to optimize imports in modified files automatically when your changes are saved.
In the Settings/Preferences dialog Ctrl+Alt+S, select .
Enable the Optimize imports option and apply the changes.
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:
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>|
Toggling relative and absolute imports
PyCharm helps you organize relative and absolute imports within a source root. With the specific intention, you can convert absolute imports into relative and relative imports into absolute.
If your code contains any relative import statement, PyCharm will add relative imports when fixing the missing imports.
Note that relative imports work only within the current source root: you cannot relatively import a package from another source root.
The intentions prompting you to convert imports are enabled by default. To disable them, open project Settings/Preferences (Ctrl+Alt+S), select , and deselect the Convert absolute import to relative and Convert relative import to absolute.
Adding import statements on code 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
Configure auto-import on completion
You can disable auto-import on completion and use quick-fixes instead:
In the Settings/Preferences dialog Ctrl+Alt+S, go to .
Ignoring missing import statements
If you use a module in your code that doesn't have any corresponding stub, PyCharm might show a missing statement error. To suppress this error message, use the
# type: ignore comment: