Reputation: 2067
I am using a linux python shell and each time I make changes to the imported file I need restart the shell (I tried reimporting the file but the changes were not reflected)
I have a definition in a file called handlers.py
def testme():
print "Hello I am here"
I import the file in the python shell
>> import handlers as a
>> a.testme()
>> "Hello I am here"
I then change print statement to "Hello I am there", reimport handlers, it does not show the change?
Using Python 2.7 with Mint 17.1
Upvotes: 5
Views: 6834
Reputation: 77951
You need to explicitly reload
the module, as in:
import lib # first import
# later ....
import imp
imp.reload(lib) # lib being the module which was imported before
note that imp
module is pending depreciation in favor of importlib
and in python 3.4 one should use: importlib.reload
.
Upvotes: 9
Reputation: 107287
As an alternative answer inside reload
you can use
watchdog
.
A simple program that uses watchdog to monitor directories specified as command-line arguments and logs events generated:
From the website
Supported Platforms
Linux 2.6 (inotify)
Mac OS X (FSEvents, kqueue)
FreeBSD/BSD (kqueue)
Windows (ReadDirectoryChangesW with I/O completion ports; ReadDirectoryChangesW worker threads)
OS-independent (polling the disk for directory snapshots and comparing them periodically; slow and not recommended)
Upvotes: 1
Reputation: 2077
You should use reload every time you make a change and then import again:
reload( handlers ) import handlers a a
Upvotes: 1