Hello,

I have few question regarding importing modules. I have created a folder for my python work

/usr/home/pratz/pyfiles

I have exported this to set the $PATH. And now if I issue the command

echo $PATH

I am getting the string the "/usr/home/pratz/pyfiles" in the list. If I am not wrong then this means that the path is been set correctly. Also, I have set the python path in the environment variable and even that is correct.
Now I have a module called "mod.py" and it has a class "cls1" and this class contains a function func()

mod.py -> cls1 -> func()

Now I create another class cls2 and inherit the class cls1

import mod.py
class cls2 (cls1):

Python says that cls1 is not defined. Need help with this.
Also, how can I call the base class method / function from the derived class, I mean to say

c = cls1()
c.func()
or
mod.func()

I know that the first one is correct, is that the second one will also work?? Any help is appreciated. Thank you.

Most likely this should work ...

import mod

class cls2(mod.cls1):
    pass

you could use

from mod import cls1

class cls2(cls1):
    pass

or like the last comment says

import mod

class cls2(mod.cls1):
    pass

the way python imports work, when you say import MODULE you are importing the MODULE namespace into your current global namespace. so you can then access objects within MODULE via dot syntax ie: MODULE.class() or MODULE.function(). Now since always typing out your module name when you access its contents isn't always the best, easiest solution, you can also use from MODULE import name to import objects from a module directly into your global namespace, then you can access them with just their name, you could get the last object name from my last example just like that name. So if name happend to be an object with a get_name method defined you could call it with name.get_name().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.