Unconfigurable Auto Remove of Unused Python Imports

Answered

At some point the Python plugin started to remove unused imports (no questions asked) from python files by default.

I typically have either commented off imports or unused imports during development which end up enabling and/or integrating later on, which is why I'd like to keep them around.

I don't have "optimize imports on the fly" or nothing of the sort enabled and I can't find a way to switch this behavior off.

Any guidance on the problem?

1
13 comments

 

You might have accidentally enabled running "Optimize Imports" as part of "Reformat Code" action. There is a checkbox for it in "Reformat File" dialog (Ctrl+Alt+Shift+L). Optimize Imports is also run during some refactorings like "Move..." to remove obsolete imports.

0

Thanks I believe that was it. It would be nice to to add a setting to Editor -> Code Style -> Python -> Imports that allows you to keep "unused" imports around instead of deleting them. Any guidance on how to submit such a feature request?

1

Sure, feel free to create a dedicated issue on YouTrack, but I doubt it makes a lot of sense since deleting unused imports is basically the main purpose of "Optimize Imports". As for removal of commented out import statements, it's a known pesky problem that will be fixed in PyCharm 2017.1.

0
Avatar
Permanently deleted user

+1 for this.

I want to be able to ignore unused imports in some files, e.g when i import signal handler in the ready() under app.py:

import yourproject.yourapp.signals.handlers #noqa

1
Avatar
Permanently deleted user

@Lywan:  I was looking for a solution for this, but decided to do this:

import settings

# this line is to prevent PyCharm code reformatting from removing the import
var = settings.SOMETHING_IN_MY_SETTINGS_FILE
0
Avatar
Permanently deleted user

This has also been an issue for me, mostly during moves.

I'm dealing with external python libraries that haven't always been coded in the cleanest ways (APIs for some third party software), some of which run code on import. 

As a result, it looks like the import is unused, while it's actually doing stuff. Sadly I'm not able to go and fix the bad code from these external APIs. PyCharm has removed these imports (including one that was enabling autosaves in our primary software!) on multiple occasions while re-organizing our code. It's super dangerous to have it do that without even a warning.

3

Yes, I realize the risk here, but in general it's difficult and time-consuming to tell that some of unused imports should be left as is due to their side-effects. You can prevent removal of them by using special comments to suppress the corresponding intention.

0

Specifically, you can use this comment to protect the following import from removal:

# noinspection PyUnresolvedReferences

(this is what "Suppress for statement" does, I saw the previous comment before and assumed it was saving something in the idea config).

1

How do you prevent the removal of unused imports during Moves? I don't see any options to change it during the Move (and having leave a comment in front of every import seems like the wrong way to go about it).

Also, why does it remove imports during Moves? I moved a file in one package in my dir, and it removes all the unused imports in modules in completely different directories that aren't even in the package. This doesn't seem like an intended, or obvious, side-effect.

Even if there is a reason why unused imports should be removed on a Move refactor, I definitely think it should be much more obvious that this will happen; currently there's no warning and it takes a while to find out why all the imports disappeared.

1

I have to second this question - just discovered this "bug", when I moved a file to a sub-directory, and suddenly my git-status had half a dozen files modified. I did not do a code Reformat, just a file Move. The project files were updated with the moved-file's new location (good), but also had unused imports removed (bad), which I wanted to keep.

0

Just tested it in 2022.1.4 and moving the file to a sub-directory (using F6 for Refactor >> Move) did not remove the unused imports for me.

0

Well, that makes one of us. :) I updated from 2022.1.3, to 1.4, just to check - and same issue.

0

I found an issue related to moving a class: https://youtrack.jetbrains.com/issue/PY-54707/Move-refactoring-shouldnt-remove-unused-imports

This one is reproduced for me, but moving a file still works fine. Weird.

0

Please sign in to leave a comment.