0

I have 2 modules A and B

A

def f(x):
     
    return x*2

B

import A
x=7
print(A.f(x))

When I run B I get error module A has no attribute f why?

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
Acx
  • 1
  • 2
    Are `A.py` and `B.py` in the same folder, and does that folder also contain an empty file called `__init__.py`? If not, can you try adding that file and doing so? – Green Cloak Guy Jul 29 '21 at 03:12
  • Related - See [this question](https://stackoverflow.com/questions/14050281/how-to-check-if-a-python-module-exists-without-importing-it), on how to check if a module exists without importing it. – BruceWayne Jul 29 '21 at 03:13
  • After `import A`, add `print(A.__file__)`. Is the file you think it is? – tdelaney Jul 29 '21 at 03:20
  • Cannot reproduce. If I write two files A.py and B.py in hte same directory, then run `python3 b.py`, it works. So, we have to guess what's different in your environment. You say modules A and B... do you mean A.py and B.py? Are these in an IDE? Have you saved the files before running? – tdelaney Jul 29 '21 at 03:23
  • Its is better if you can provide tree of the directory for the program. – Rizquuula Jul 29 '21 at 03:51

0 Answers0