Question: I have a conditional import in a self-initialized instance of a superclass, but subclass cannot see the module (python 2.7):

```
class A(object):
    def __init__(self, arg1):
        self.attr1 = self.method1(arg1)

    def method1(self, arg1):
        if arg1 == 'foo':
           import amodule
           return amodule.method1()
        else:
            return 'not a dependency on foo'

class B(A):
    def __init__(self, arg1):
        super(B, self).__init__(arg1)
        if arg1 == 'foo':
            self.attr2 = self.method2(self.attr1)

    def method2(self, attr1):
        return amodule.method2()

if __name__=='__main__':
    b = B("foo")
    print b.attr2

```

This throws `NameError: global name 'amodule' is not defined`. `a = A("foo")` works just fine

Shouldn't the `super` call have executed `import amodule` in this case? (And using `import` should have put the module into globals?)

Answer: 
> 
> Doesn't import add /amodule/ to the global namespace of the currently
>  executing module? (`__main__`)?
> 
> 
> 

No, the module is added to `sys.modules` but if it was imported locally then you won't have any references to it anymore. i.e the name `amodule` is now gone.

You could still access the module using [`sys.modules`](https://docs.python.org/2/library/sys.html#sys.modules):

```
def method2(self, attr1):
    import sys
    return sys.modules['amodule'].method2()

```

Or you could import it using `import amodule` again and it will be picked up from `sys.modules`.

---

```
# Here b.py contains
# print('Module b was imported')

def func1():
    print('inside func1')
    import b

def func2():
    print('inside func2')
    import sys
    print(sys.modules['b'])
    import b

def func3():
    print('inside func3')
    import b
    import sys
    print('Deleted b')
    del sys.modules['b']
    import b

func1()
print()
func2()
print()
func3()

```

**Demo:**

```
inside func1
Module b was imported

inside func2
<module 'b' from '/Users/ashwini/py/b.py'>

inside func3
Deleted b
Module b was imported

```
Try putting `import amodule` on the first line of the program.

The reason is that amodule is imported in method1, method2 does not have access.