naught101
naught101

Reputation: 19533

from . import [module] -> ImportError: cannot import name [module]

When trying to import my module in python 3.4, I get:

In [6]: import PyModLSM
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-9980d6d1561d> in <module>()
----> 1 import PyModLSM

/home/naught101/Documents/uni/phd/projects/ModularLSM/PyModLSM/PyModLSM/__init__.py in <module>()
     11     sys.exit()
     12
---> 13 from . import handlers
     14 from . import models
     15 from . import constants

ImportError: cannot import name 'handlers'

The directory structure is:

├── LICENSE.md
├── PyModLSM
│   ├── component.py
│   ├── constants.py
│   ├── data.py
│   ├── functions.py
│   ├── handlers.py
│   ├── __init__.py
│   ├── models.py
│   └── science_functions.py
├── README.md
└── setup.py

There are no circular imports, as far as I can see:

$ grep -irH "from.*import" PyModLSM/*py
PyModLSM/functions.py:    from .handlers import ModelHandler
PyModLSM/handlers.py:from .component import ModelComponent
PyModLSM/__init__.py:from . import handlers
PyModLSM/__init__.py:from . import models
PyModLSM/__init__.py:from . import constants
PyModLSM/__init__.py:from . import science_functions
PyModLSM/__init__.py:from .functions import *
PyModLSM/models.py:from .component import ModelComponent
PyModLSM/models.py:from . import science_functions as sf
PyModLSM/science_functions.py:from . import constants as c

This has started happening since I upgraded to python 3.4 (using conda) - I was previously on python 3.3, or possibly 3.2, and it was working ok.

Upvotes: 0

Views: 2251

Answers (2)

anox
anox

Reputation: 1

did you recognize the error line

PyModLSM/models.py:from .component import ModelComponent

probably it should be

from . component import ModelComponent

Upvotes: -1

naught101
naught101

Reputation: 19533

sigh.. Ok, this was caused by a missing required module, that was installed in the mean-time, and imported. Python was refusing to import handlers because that model was previously missing. Restarting Python allowed the import to continue properly.

Not sure if this question should be deleted, or left here in case someone else does the same thing..

Upvotes: 3

Related Questions