CodeRonin
CodeRonin

Reputation: 2099

modules exist, pyCharm suggest me the name, but import fails

I'm new in python and I'm facing with some problems with module/packages and import. I have a python project in pyCharm, this is my project's structure:

project 
   |
   |------ model (package)
             |
             |---- __init__.py
             |---- impianto.py
             |---- componente.py
             |---- sorgente.py
   |------- app.py

every module defines a class with the same name, so impianto.py defines a class called Impianto, componente.py defines a class called Componente and so on. In app.py I have an import for the Impianto class. This is what I do:

from model.impianto import Impianto

in impianto.py I import Componente like this:

from model.componente import Componente

and in componente.py I import Sorgente like this:

from model.sorgente import Sorgente

please note that Sorgente extends Componente and pyCharm suggested me the import names, but when I run app.py it gives me this error

  File "/home/gjcode/PycharmProjects/es3_2016/app.py", line 2, in 
  <module>
  from model.impianto import Impianto
  File "/home/gjcode/PycharmProjects/es3_2016/model/impianto.py", line 1, in <module>
from model.componente import Componente
File "/home/gjcode/PycharmProjects/es3_2016/model/componente.py", line 1, in <module>
from model.sorgente import Sorgente
File "/home/gjcode/PycharmProjects/es3_2016/model/sorgente.py", line 1, in <module>
from model.componente import Componente
ImportError: cannot import name 'Componente'

Upvotes: 1

Views: 74

Answers (1)

Druta Ruslan
Druta Ruslan

Reputation: 7402

You have circular dependent imports. you try to execute

from model.componente import Componente 

in two files, in sorgente.py and in impianto.py try to remove from sogrente.py

from model.componente import Componente

Upvotes: 2

Related Questions