I patched importanize, which worked for me.
The error occurs because the imp module (used in importanize) was deprecated in Python 3.4 and removed in Python 3.12.
Solutions
1. Quick Fix: Use Python ≤3.11
# Example using pyenv (recommended):
pyenv install 3.11.9
pyenv local 3.11.9
python -m pip install importanize
2. Patch importanize (For Python ≥3.12)
-
Edit the problematic file:
Locate site-packages/importanize/utils.py and change:
import imp # ← Remove this line
import importlib.machinery # ← Add this line
-
Replace _get_module_path:
With:
def _get_module_path(module_name):
paths = sys.path[:]
if os.getcwd() in sys.path:
paths.remove(os.getcwd())
spec = importlib.machinery.PathFinder.find_spec(module_name, path=paths)
return spec.origin if spec else ''
### 3. Recommended Alternatives
These actively maintained tools offer similar import sorting:
```bash
# isort (most popular)
pip install isort
isort your_file.py
# reorder-python-imports (Black-compatible)
pip install reorder-python-imports
reorder-python-imports your_file.py
I patched importanize, which worked for me.
The error occurs because the
impmodule (used inimportanize) was deprecated in Python 3.4 and removed in Python 3.12.Solutions
1. Quick Fix: Use Python ≤3.11
2. Patch importanize (For Python ≥3.12)
Edit the problematic file:
Locate
site-packages/importanize/utils.pyand change:Replace _get_module_path:
With:
def _get_module_path(module_name):
paths = sys.path[:]
if os.getcwd() in sys.path:
paths.remove(os.getcwd())