I have a base class A
in base.py
:
JavaScript
x
6
1
import module1
2
3
class A:
4
def test(self):
5
module1.sample("test")
6
Then in new.py
I created a new class B
which inherits A
and override test
method:
JavaScript
1
6
1
from base import A
2
class B(A):
3
def test(self):
4
module1.sample("test")
5
print("Testing...")
6
The problem is that the module1
is no longer available in new.py
. Is there any options that I do not need to import module1
again in new.py
?
Advertisement
Answer
One not recommended way to achieve what you want is to use __builtins__
. Add the following line to base.py
.
JavaScript
1
2
1
__builtins__['module1'] = module1
2
Then module1
is no longer undefined from new.py
. It is definitely defined in __builtins__
.
Again, it is not recommended, however, good to understand how Python works. You would better import module1
from new.py
as well.
JavaScript
1
5
1
import module1
2
3
from base import A
4
5