Repository: internetarchive/openlibrary

Issue:
Investigate Python Unused Imports
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] -->
There are [a lot](https://lgtm.com/query/7596990423930761565/) of unused imports which clutter things.

### Describe the problem that you'd like solved
<!-- A clear and concise description of what you want to happen. -->
Refactoring to remove all unused imports.

### Proposal & Constraints
<!-- What is the proposed solution / implementation? Is there a precedent of this approach succeeding elsewhere? -->
Remove all ununsed imports. This will reduce technical debt, decluttering things to make them clearer.
<!-- Which suggestions or requirements should be considered for how feature needs to appear or be implemented? -->

### Additional context
<!-- Add any other context or screenshots about the feature request here. -->

UPDATE:
As per @cclauss's comments below, for detecting unused imports, we should really be running
```
python3 -m pip install flake8
flake8 --select=F401
```

### Stakeholders
<!-- @ tag stakeholders of this bug -->
@cclauss @imskr 


Discussion:
@teymour-aldridge could you help us with this one?
Work is in Progress refer #3447 As soon as this will get merged, I will land another PR.
I'd be happy to help. I'm a bit busy at the moment, but should have some time on the weekend.
To automatically catch these sort of errors it might be worth installing CodeClimate on the repository (which automatically reports code duplication, ununsed imports, overly complex functions, etc) as well as LGTM.com (which catches security bugs in open source). Both are free for open sorce software!
`flake8` which we already run in our continuous integration can autodetect these issues.

Detection is not the problem.  The problem is that no one is reviewing PRs so they sit open for months at a time.  Let's ___not___ add CodeClimate as it is full of uncontrollable false positives.
Oh right. I may have jumped the gun a bit here. I'd be happy to work to try and help resolve some of these issues.
Happy to help; need some guidance. These are the steps I would take?
1. Clone the repository at https://github.com/internetarchive/openlibrary
2. Run the query and get the results, https://lgtm.com/query/7596990423930761565/
3. Remove offending import from file
4. Commit back to repo

`python3 -m pip install flake8`
[`flake8 --select=F401`](https://flake8.pycqa.org/en/3.8.3/user/error-codes.html?highlight=unused%20import)
Can I too work on this issue, sir?
Please refer to #3447...  That must be reviewed and landed first.
Hi, @cclauss @imskr I would like to work on this issue.

Can I remove unused imports from other files excluding code.py and conftest.py?
@mekarpeles I started working on this issue, so I just want to know that is there a way to automatically remove unused imports or do I need to remove them manually.
CAUTION: Some imports are done for side effects so linters like flake8 may say they are not needed but things will break without them.  Please do just a file or two per PR.
@cclauss I have removed unused imports till .\openlibrary\plugins\openlibrary\utils.py so should I create a PR and commit them 2 at a time? or create separate PR for each of them?
@cclauss I would like to remove unused imports from other files as well. So should I first create a PR for unused Python standard library imports?
> Should I first create a PR for unused Python standard library imports?

I thought that #3909 was that PR.
> I thought that #3909 was that PR.

@cclauss I mean for other files (files after openlibrary/core/waitinglist.py).
 I removed all **unused imports** till  openlibrary/core/waitinglist.py and in PR #3909 I removed all **standard unused imports** till  openlibrary/core/waitinglist.py. 

OK.  Please creating more PRs that only remove Python standard library imports.  Once those are all landed, we can look at PRs for other imports.
@cclauss As #3965 is merged should I remove other unused imports and commit them 2 files at a time?
@cclauss Should I go ahead and remove other unused imports (Non-standard ones)
Sorry but not right now.  These changes create issues that are quite difficult to find.

Right now we are trying to upgrade to Python 3 and need to maximize stability to deal with [bytes vs. str](https://github.com/internetarchive/openlibrary/pulls?q=label%3A%22Python+3%3A+bytes+vs.+str%22) issues.
Hey, is this issue still open?? I can take it up.

It is still open but as mentioned above, we have a fair number of imports that have side-effects.  This means that [flake8 F401](https://github.com/internetarchive/openlibrary/issues/3483#issuecomment-648125806) might say that an import is not needed but removing it will create a crash/bug/etc.  We are super-close to [removing Python 2](https://github.com/internetarchive/openlibrary/projects/25#card-52427678) and once that is done, we can go full force on this issue.

If you want to dig in now, just modify a single file per pull request.  If PRs land cleanly, that is great.  If they do not land cleanly, we can add comments that describe the side effects.  Thanks for your interest!
if this issue is still open I would like to take it up
The issue is open but we have not removed the Python 2 code so let's hold off on attacking this one a little bit longer.
I want to be a participant  in solving this issue, i am a beginner can anyone tell me from where to start and till now what is the progress for this issue?

let's hold off on attacking this one a little bit longer.
Can anyone please have a look at my PR It has been open for a while with no reviews! @cclauss @cdrini 
Hey @cclauss Is this issue still open I would like to contribute

@cclauss, I would like to be next in line to take up this issue.
The comments above still apply:
> It is still open but as mentioned above, we have a fair number of imports that have side effects. This means that https://github.com/internetarchive/openlibrary/issues/3483#issuecomment-648125806 might say that an import is not needed but removing it will create a crash/bug/etc.  If you want to dig in, modify one or two files per pull request. If PRs land cleanly, that is great. If they do not land cleanly, we can add comments that describe the side effects. Thanks for your interest!

Resolving patch:
diff --git a/openlibrary/plugins/admin/code.py b/openlibrary/plugins/admin/code.py
--- a/openlibrary/plugins/admin/code.py
+++ b/openlibrary/plugins/admin/code.py
@@ -9,7 +9,6 @@
 import traceback
 import logging
 import json
-import yaml
 
 from infogami import config
 from infogami.utils import delegate
@@ -27,19 +26,12 @@
 
 from openlibrary import accounts
 
-from openlibrary.core import lending, admin as admin_stats, helpers as h, imports, cache
+from openlibrary.core import admin as admin_stats, helpers as h, imports, cache
 from openlibrary.core.waitinglist import Stats as WLStats
 from openlibrary.core.sponsorships import summary, sync_completed_sponsored_books
-from openlibrary.core.bookshelves import Bookshelves
-from openlibrary.core.ratings import Ratings
-from openlibrary.core.booknotes import Booknotes
-from openlibrary.core.observations import Observations
 from openlibrary.core.models import Work
-
 from openlibrary.plugins.upstream import forms, spamcheck
 from openlibrary.plugins.upstream.account import send_forgot_password_email
-from openlibrary.plugins.admin import services
-
 
 logger = logging.getLogger("openlibrary.admin")
 


Test patch: