I have a module structure like the following:
.
└── testmodule
├── __init__.py
└── submodule
├── __init__.py
└── implementation.py
2 directories, 3 files
Here are the contents of each file
# testmodule/__init__.py
import submodule
# testmodule/submodule/__init__.py
from implementation import *
# testmodule/submodule/implementation.py
class Car(object):
def __init__(self):
self.doors = 2
self.color = 'red'
Why does the class implementation not "reload" when I use reload() as in the following test?
Python 2.7.12 (default, Oct 11 2016, 05:24:00)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import testmodule
>>> car = testmodule.submodule.Car()
>>> car.doors
2
>>> # I edit the file and change self.doors = 2 to self.doors = 4
>>> reload(testmodule)
<module 'testmodule' from 'testmodule/__init__.pyc'>
>>> car = testmodule.submodule.Car()
>>> car.doors
2
>>> # no more edits made before the Python REPL is restarted
me@laptop # python
Python 2.7.12 (default, Oct 11 2016, 05:24:00)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import testmodule
>>> car = testmodule.submodule.Car()
>>> car.doors
4
Update
By @wim's logic, I would have to reload testmodule.submodule.implementation, then reload testmodule.submodule to make this "work", and indeed that is the case. See these tests:
Python 2.7.12 (default, Oct 11 2016, 05:24:00)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import testmodule
>>> c = testmodule.submodule.Car()
>>> c.doors
2
>>> # I edit the file and change self.doors = 2 to self.doors = 4
>>> reload(testmodule.submodule.implementation)
<module 'testmodule.submodule.implementation' from 'testmodule/submodule/implementation.py'>
>>> c = testmodule.submodule.Car()
>>> c.doors
2
>>> reload(testmodule.submodule)
<module 'testmodule.submodule' from 'testmodule/submodule/__init__.pyc'>
>>> c = testmodule.submodule.Car()
>>> c.doors
4
me@laptop # python
Python 2.7.12 (default, Oct 11 2016, 05:24:00)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import testmodule
>>> c = testmodule.submodule.Car()
>>> c.doors
2
>>> # I edit the file and change self.doors = 2 to self.doors = 4
>>> reload(testmodule.submodule)
<module 'testmodule.submodule' from 'testmodule/submodule/__init__.pyc'>
>>> c = testmodule.submodule.Car()
>>> c.doors
2
>>> reload(testmodule.submodule.implementation)
<module 'testmodule.submodule.implementation' from 'testmodule/submodule/implementation.py'>
>>> c = testmodule.submodule.Car()
>>> c.doors
2
>>> reload(testmodule.submodule)
<module 'testmodule.submodule' from 'testmodule/submodule/__init__.pyc'>
>>> c = testmodule.submodule.Car()
>>> c.doors
4
reloadis far less thorough than people usually expect. This is only one of the ways in which it doesn't reload everything you'd want it to reload. It's usually better to just restart Python.from implementation import *, and also the lineimport submodule, are very bad practice. These are implicit relative imports, and your code will stop working properly on Python 3.