Skip to end of metadata
Go to start of metadata

Import Assistant

Sometimes you use a class or a package that has not yet been imported. In such situations, PyCharm helps locate the missing file and then adds it to the list of imports. This feature is known as the Import Assistant.

To explore this feature, let's use our everlasting class Solver. Type the following code:

As soon as you type math.sqrt(d), PyCharm shows a pop-up, suggesting you to add the missing import statement:

Press Alt+Enter, as the pop-up suggests, and the import statement immediately appears in the imports section. However, the cursor does not move from the current position, your editing session does not interrupt, so you can go on typing:

By the way, if there is more than one possible source of import, PyCharm will show the suggestion list, and you will have to choose one:

Configuring Import Assistant

If the window that pops up when you don't want to see it, gets to your nerves, you can switch it off. For the current file, click Hector:

In the window that opens, clear the check box Import popup - thus you will cancel popping the prompt up in the active editor tab. So doing, the option Show import popup should be enabled in the Auto-Import page of the editor settings (Settings → Editor → Auto-Import):

Otherwise the Hector settings will take no effect.

However, if you want to cancel the import pop-up in all the the newly created files, clear the check box Show import popup (Settings → Editor → Auto-Import).

Quick fix "Import this name"

What happens when Import Assistant is off? Nothing terrible. Instead of showing you the prompt, PyCharm will just underline a reference to the missing resource and show the red light bulb . Click this bulb, or press Alt+Enter to see what to do:

In our example, let's choose to import the missing library - so the import statement is added to the imports section of the class, and the red underline disappears.

Optimizing imports

As you go on working with your code, sooner or later you cease using some of the import statements. However, they still persist and clutter your project, and you have to stop what you are doing, scroll to the head of the file, find the unused imports, and remove them. This approach is error-prone - it's quite easy to forget to remove imports when you remove usages.

PyCharm helps getting rid of the extraneous import statements without disturbing your editing session. This is done by means of 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.

By the way, note that unused imports (as all unused symbols) are greyed out:

To remove unused imports, press Ctrl+Alt+O (or choose Code → Optimize Imports on the main menu). PyCharm shows the following dialog box, where you have to select the target file (current file, or all files in the current directory):

After clicking OK the unused import statements disappear:

Other topics of this tutorial

Your Rating: Results: BrokenPartially brokenWorksStableNo problems at all 3 rates

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.