Cannot find reference - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Cannot find reference (/thread-29334.html) |
Cannot find reference - spalisetty06 - Aug-28-2020 Hello, I have imported the module named 'module' and I am trying to use the members in another file, I have installed packages as well, why do I still get 'Cannot find reference'? I will get correct output though. from module import add as a, name as name, death as death, product as p print(name) print(death) a(10, 20) print("the product of num is",p(10, 20)) When googled, it says, it is a bug in Pycharm. Thank you. RE: Cannot find reference - deanhystad - Aug-28-2020 Is module in your module search path? RE: Cannot find reference - spalisetty06 - Aug-28-2020 It is in the same location. I am getting output as well. But I see red underscropes, when hovered over, it says, cannot find reference. I googled it, it says a bug in Pycharm. Thank you so much for trying to help me. RE: Cannot find reference - deanhystad - Aug-28-2020 Is the module named "module.py"? And what are you doing naming something "module"? RE: Cannot find reference - spalisetty06 - Aug-28-2020 The module name is module.py itself. name and and everything are members of module.py RE: Cannot find reference - deanhystad - Aug-28-2020 I need to look a the post more carefully. The problem is that you cannot do from module import add as a . You can import module as a and you can do from module import a , but you cannot do both. You are only allowed to rename namespaces, not items in the namespace.
RE: Cannot find reference - spalisetty06 - Aug-28-2020 Various possibilities of import: import module1 import module1, module2, module3 import module1 as m1 import module1 as m1, module2 as m2, module3 as m3 from module1 import member1 from module1 import member1, member2, member3 from module1 import * from module1 import member1 as m1 from module1 import member1 as m1, member2 as m2, member3 as m3 RE: Cannot find reference - deanhystad - Aug-28-2020 I was wrong. You can rename things that you import. This works fine for me: from module import x as b, y as c print(b)There is no reason to do from module import name as name as it does exactly the same thing as from module import name What was the error? The entire error message. RE: Cannot find reference - spalisetty06 - Aug-28-2020 '''module''' name = "suman" death = "not yet" def add(x, y): print("The sum of two numbers is",x + y) def product(x, y): return x * yThis file is my main file '''test.py''' '''member aliasing''' from module import add as a, name as name, death as death, product as p print(name) print(death) a(10, 20) print("the product of num is",p(10, 20)) RE: Cannot find reference - snippsat - Aug-28-2020 Don't try to import and name chance all at once,all can be access from module . >>> import module >>> module.name 'suman' >>> module.death 'not yet' >>> module.add(10, 20) The sum of two numbers is 30 >>> module.product(5, 5) 25Alternative. >>> from module import name, death, add, product >>> name 'suman' >>> add(4, 9) The sum of two numbers is 13If think product is to long to write. >>> from module import product as pr >>> pr(5, 8) 40 |