Posts: 41
Threads: 18
Joined: Apr 2020
Aug-28-2020, 01:10 PM
(This post was last modified: Aug-28-2020, 01:23 PM by spalisetty06.)
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.
Posts: 6,792
Threads: 20
Joined: Feb 2020
Is module in your module search path?
Posts: 41
Threads: 18
Joined: Apr 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.
Posts: 6,792
Threads: 20
Joined: Feb 2020
Is the module named "module.py"? And what are you doing naming something "module"?
Posts: 41
Threads: 18
Joined: Apr 2020
Aug-28-2020, 01:34 PM
(This post was last modified: Aug-28-2020, 01:34 PM by spalisetty06.)
The module name is module.py itself. name and and everything are members of module.py
Posts: 6,792
Threads: 20
Joined: Feb 2020
Aug-28-2020, 01:40 PM
(This post was last modified: Aug-28-2020, 01:40 PM by deanhystad.)
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.
Posts: 41
Threads: 18
Joined: Apr 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
Posts: 6,792
Threads: 20
Joined: Feb 2020
Aug-28-2020, 01:52 PM
(This post was last modified: Aug-28-2020, 01:55 PM by deanhystad.)
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.
Posts: 41
Threads: 18
Joined: Apr 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 * y This 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))
Posts: 7,318
Threads: 123
Joined: Sep 2016
Aug-28-2020, 02:11 PM
(This post was last modified: Aug-28-2020, 02:12 PM by snippsat.)
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)
25 Alternative.
>>> from module import name, death, add, product
>>> name
'suman'
>>> add(4, 9)
The sum of two numbers is 13 If think product is to long to write.
>>> from module import product as pr
>>> pr(5, 8)
40
|