PyCharm does this automatically now by use of Code -> Optimize Imports. It also sorts them into groups per PEP 8.

See https://www.jetbrains.com/help/pycharm/optimizing-imports.html for details.

Answer from Quark on Stack Overflow
🌐
JetBrains
jetbrains.com › help › pycharm › creating-and-optimizing-imports.html
Auto import | PyCharm Documentation
3 weeks ago - To optimize imports in a file, you can also press Ctrl+Alt+Shift+L, select Optimize imports, and click Run. If your project is under version control, you can configure PyCharm to optimize imports in modified files before committing them to VCS.
Discussions

How do I optimize imports in Python using PyCharm? - Stack Overflow
But for me, it just turns grey ... to optimize my imports but just deleting my text altogether when I click it. I can find anything on the web to help me with what I'm dealing with leading me to believe that its probably an easy fix that I, for whatever reason can't seem to find. I really don't have a clue as to what to do and im increasingly becoming more and more frustrated. ... That is okay, PyCharm is only signaling ... More on stackoverflow.com
🌐 stackoverflow.com
How to stop Pycharm adding imports to my code automatically ?
Have you checked the settings? It should not add imports automatically. Or do you use any PyCharm plugins that do that extra stuff? Just disable features you don't like or need. More on reddit.com
🌐 r/pycharm
7
14
December 3, 2024
pycharm - Is there a shortcut to merge import statements - Stack Overflow
Is there any other short cut available to do this in Pycharm? just curious. ... Now in the imports tab of Python Code Style, check the box saying Join "from" imports with the same source as shown in the above screenshot, now whenever you run Optimize imports by running Ctrl+Alt+O or code > ... More on stackoverflow.com
🌐 stackoverflow.com
Optimize Imports not completely working correctly...
There is nothing wrong here. csv/json/datetime/etc are python builtins and thus come first. Django and tqdm are external libraries and come second. FInally your own source code imports. More on reddit.com
🌐 r/pycharm
1
3
July 2, 2022
🌐
JetBrains
jetbrains.com › pycharm › guide › tips › optimize-imports
Optimize Imports - PyCharm Guide
February 17, 2023 - With the Optimize Imports action ⌃⌥O (macOS) / Ctrl+Alt+O (Windows/Linux), you can quickly remove any unused imports, merge imports from the same module, and optionally sort the import statements.
🌐
JetBrains
blog.jetbrains.com › pycharm › 2018 › 11 › let-pycharm-do-your-import-janitorial-work
Let PyCharm Do Your Import Janitorial Work | The PyCharm Blog
January 15, 2024 - PyCharm can optimize imports in a single file. But you can also optimize imports across the entire project. Select the folder at your project root, then trigger Optimize Imports (from the menu, the shortcut, or Find Action.)
🌐
YouTube
youtube.com › watch
How to optimize imports in PyCharm - YouTube
In this very short video you will learn how to automate the organizing and cleaning up of your Python imports with Optimize Imports.00:00 - Start00:12 - Show...
Published   May 29, 2020
🌐
Quora
quora.com › What-does-optimize-imports-mean-on-PyCharm
What does 'optimize imports' mean on PyCharm? - Quora
Answer (1 of 2): it means that if you are doing this : [code]from math import cos from math import sin from math import sqrt from math import factorial [/code]and you optimise imports it will convert that to : [code]from math import cos, sin, sqrt, factorial [/code]further more it will remove a...
🌐
JetBrains
jetbrains.com › pycharm › guide › tips › optimize-imports
Clean Up Messy Imports With Optimize Imports - JetBrains Guide
February 17, 2023 - With the Optimize Imports action ⌃⌥O (macOS) / Ctrl+Alt+O (Windows/Linux), you can quickly remove any unused imports, merge imports from the same module, and optionally sort the import statements.
Find elsewhere
🌐
JetBrains
jetbrains.com › guide › python › tips › generate-imports-while-typing
Generate Imports While Typing - JetBrains Guide
February 17, 2023 - While typing, press ⌃␣ (macOS) / Ctrl+Space (Windows/Linux) to tell PyCharm to autocomplete on symbols from modules. When you select the completion target, PyCharm will also generate the import -- in the right place, merging it with an existing ...
🌐
JetBrains
youtrack.jetbrains.com › issue › IDEA-236571
Optimize imports not working with shortcut Left Ctrl ... - YouTrack
April 3, 2020 - {{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
Zditect
zditect.com › blog › 55903494.html
Redirecting...
We cannot provide a description for this page right now
🌐
JetBrains
jetbrains.com › help › pycharm › reformat-and-rearrange-code.html
Reformat and rearrange code | PyCharm Documentation
January 6, 2026 - Optimize imports: select this option if you want to remove unused imports, add missing ones, or organize import statements.
🌐
Reddit
reddit.com › r/pycharm › optimize imports not completely working correctly...
r/pycharm on Reddit: Optimize Imports not completely working correctly...
July 2, 2022 -

Hi - I'm using the optimize imports functionality and it mostly works but it misses some libraries. The ones I've seen so far are tqdm and dateutil. Here's what it looks like after I optimize the imports. Any idea why this is happening / how to fix it? Note that from tqdm import tqdm is grouped with the django imports:

import csv
import json
from datetime import datetime
from decimal import Decimal
from itertools import chain

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.db.models import (Avg, Count, ExpressionWrapper, F, FloatField, Q, Sum, fields)
from django.http import HttpResponse, StreamingHttpResponse
from django.shortcuts import render
from tqdm import tqdm

from common.dashboard_util import (construct_cache_name, correct_start_dates, get_all_drilldown_objects,
                               get_dates_for_columns, get_drilldown_object_id, get_landing_page_variables,
                               get_lookup_values_by_time_period, get_master_query_from_slug, get_time_to_find)
from core.util import (get_parent_object_from_slug)
from eclose.models import eCloseDefaultFilter
from loan.models import Loan
🌐
JetBrains
jetbrains.com › guide › python › tips › one-import-per-line
One Import Per Line Preference - JetBrains Guide
February 17, 2023 - PyCharm 2018.3 adds a preference: "One Import Per Line". With this setting in place, PyCharms Optimize Imports action will rewrite lines like this:
🌐
JetBrains
jetbrains.com › help › idea › creating-and-optimizing-imports.html
Auto import | IntelliJ IDEA Documentation
3 weeks ago - Press Ctrl+Alt+S to open settings and then select Editor | General | Auto Import. Enable the Optimize imports on the fly option and apply the changes.