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?

8 comments
Comment actions Permalink

 

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
Comment actions Permalink

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?

0
Comment actions Permalink

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
Comment actions Permalink

+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
Comment actions Permalink

@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
Comment actions Permalink

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.

1
Comment actions Permalink

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
Comment actions Permalink

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).

0

Please sign in to leave a comment.