Skip to content

ModuleNotFoundError: No module named 'imp' #76

@WyldKnyght

Description

@WyldKnyght

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)

  1. Edit the problematic file:
    Locate site-packages/importanize/utils.py and change:

    import imp  # ← Remove this line
    import importlib.machinery # ← Add this line
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions