0

I have two modules in the same directory, one has the function in it. I am trying to call the function into my another module, but I am facing AttributeError

module1:

from tank import cal as c

def water():
    lev1=c.rec1
    lev2=c.rec2
    lev3=c.rec3
    print(lev1)
    print(lev2)
    print(lev3)

module2:

    from tank import level as lv

    a=input("enter the number")
    rec1=a[1:5]
    rec2=a[5:9]
    rec3=a[9:13]
    lv.water()

Error:

    AttributeError: module 'tank.level' has no attribute 'water'

Directory Structure:

Data --tank --__init__.py --cal.py --level.py

4
  • tank is the package which has both the modules Commented Oct 29, 2018 at 5:30
  • module 1: posted is tank.level and module 2: is cal Commented Oct 29, 2018 at 5:33
  • Show the directory structure for these module Commented Oct 29, 2018 at 5:39
  • updated question Commented Oct 29, 2018 at 5:51

1 Answer 1

1

You have two modules that are importing each other! You shouldn't have cyclical imports like this; one way to fix this is to have the water() function accept some arguments instead of directly trying to import values from the other module.

def water(lev1, lev2, lev3):
    print(lev1)
    print(lev2)
    print(lev3)
Sign up to request clarification or add additional context in comments.

3 Comments

you want me to replace rec to lev
Hey @Nithya! what @Michael is trying to say is, when you run cal.py interpreter goes on to import level.py, which in turn contains import cal, so it goes to cal again.. then level again. Read about circular imports in python. stackabuse.com/python-circular-imports
Thanks guys for the info. That gave me the knowledge what I was looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.