Hi all,

We just hit functions, and I assigned my students to write ten short functions to do things like convert Fahr. to Cels., etc.

My "brilliant" :rolleyes: plan was to create a test suite and have them import their file and run the tests. Because the filename wasn't absolutely specified, they had to modify this line: from my_asn import * and then the test suite would run the appropriate functions, spit out the results, and all would be sweetness and light.

Problem: naturally, some of the test results will fail and students will need to modify their code. BUT, when the line "from my_asn import *" is re-executed, the namespace already has all of the functions in it, so the module isn't reloaded.

Solution: Python has a builtin reload() function to reload modules.

Problem with the Solution: reload() can only work with imports in the form import my_file NOT from my_file import * :mad:

Partial workaround: IF the students start by rightclicking the test suite file, hitting "edit with IDLE", and then running it, they get the problem above. But IF the students start by running IDLE, then opening the test suite file, then running it, then IDLE kindly restarts the shell each time and accordingly reloads the module.

Question: Why does IDLE seem to have two different modes: one with a "Shell" menu and that restarts the shell every time a program is run, and one without and that doesn't?

Grr...

Jeff

The answer is in:
http://www.daniweb.com/techtalkforums/post139280-20.html

When you import a module then Python will look first for the .pyc compiled module. You might have to force your changed module to update the .pyc file. So, after saving your updated 'my_asn.py' file, run this little utility program ...

import py_compile 
py_compile.compile("my_asn.py")  # creates/updates  my_asn.pyc

Just a hunch on my part, please let me know if that works. I always thought Python took care of this by comparing the file creation dates. Maybe the way your system is set may prevent this.

No luck so far...

import py_compile
from test1 import *

print func(3)
del func

from test1 import *

print func(3)


py_compile.compile("test1.py")

del func
from test1 import *

print func(3)

Tested with the function

def func(x):
   return 4

which is later changed (and saved) to "return 5".

Results: 4, 4, 4.

Lol.

Jeff

HAHAHAHAHA :lol:

So I tried brute force:

import compileall

...

compileall.compile_path(force=True)

del func
from test1 import *

print func(3)

which promptly recompiled EVERYTHING:

Listing C:\Python24\Lib\idlelib ...
Compiling C:\Python24\Lib\idlelib\AutoExpand.py ...
Compiling C:\Python24\Lib\idlelib\Bindings.py ...
Compiling C:\Python24\Lib\idlelib\CallTipWindow.py ...
Compiling C:\Python24\Lib\idlelib\CallTips.py ...
Compiling C:\Python24\Lib\idlelib\ClassBrowser.py ...
Compiling C:\Python24\Lib\idlelib\CodeContext.py ...
Compiling C:\Python24\Lib\idlelib\ColorDelegator.py ...
Compiling C:\Python24\Lib\idlelib\Debugger.py ...
Compiling C:\Python24\Lib\idlelib\Delegator.py ...
Compiling C:\Python24\Lib\idlelib\EditorWindow.py ...
Compiling C:\Python24\Lib\idlelib\FileList.py ...
Compiling C:\Python24\Lib\idlelib\FormatParagraph.py ...
Compiling C:\Python24\Lib\idlelib\GrepDialog.py ...
Compiling C:\Python24\Lib\idlelib\IOBinding.py ...
Compiling C:\Python24\Lib\idlelib\IdleHistory.py ...
Compiling C:\Python24\Lib\idlelib\MultiStatusBar.py ...
Compiling C:\Python24\Lib\idlelib\ObjectBrowser.py ...
Compiling C:\Python24\Lib\idlelib\OutputWindow.py ...
Compiling C:\Python24\Lib\idlelib\ParenMatch.py ...
Compiling C:\Python24\Lib\idlelib\PathBrowser.py ...
Compiling C:\Python24\Lib\idlelib\Percolator.py ...
Compiling C:\Python24\Lib\idlelib\PyParse.py ...
Compiling C:\Python24\Lib\idlelib\PyShell.py ...
Compiling C:\Python24\Lib\idlelib\RemoteDebugger.py ...
Compiling C:\Python24\Lib\idlelib\RemoteObjectBrowser.py ...
Compiling C:\Python24\Lib\idlelib\ReplaceDialog.py ...
Compiling C:\Python24\Lib\idlelib\ScriptBinding.py ...
Compiling C:\Python24\Lib\idlelib\ScrolledList.py ...
Compiling C:\Python24\Lib\idlelib\SearchDialog.py ...
Compiling C:\Python24\Lib\idlelib\SearchDialogBase.py ...
Compiling C:\Python24\Lib\idlelib\SearchEngine.py ...
Compiling C:\Python24\Lib\idlelib\StackViewer.py ...
Compiling C:\Python24\Lib\idlelib\ToolTip.py ...
Compiling C:\Python24\Lib\idlelib\TreeWidget.py ...
Compiling C:\Python24\Lib\idlelib\UndoDelegator.py ...
Compiling C:\Python24\Lib\idlelib\WidgetRedirector.py ...
Compiling C:\Python24\Lib\idlelib\WindowList.py ...
Compiling C:\Python24\Lib\idlelib\ZoomHeight.py ...
Compiling C:\Python24\Lib\idlelib\__init__.py ...
Compiling C:\Python24\Lib\idlelib\aboutDialog.py ...
Compiling C:\Python24\Lib\idlelib\buildapp.py ...
Compiling C:\Python24\Lib\idlelib\configDialog.py ...
Compiling C:\Python24\Lib\idlelib\configHandler.py ...
Compiling C:\Python24\Lib\idlelib\configHelpSourceEdit.py ...
Compiling C:\Python24\Lib\idlelib\configSectionNameDialog.py ...
Compiling C:\Python24\Lib\idlelib\dynOptionMenuWidget.py ...
Compiling C:\Python24\Lib\idlelib\idle.py ...
Compiling C:\Python24\Lib\idlelib\idlever.py ...
Compiling C:\Python24\Lib\idlelib\keybindingDialog.py ...
Compiling C:\Python24\Lib\idlelib\rpc.py ...
Compiling C:\Python24\Lib\idlelib\run.py ...
Compiling C:\Python24\Lib\idlelib\tabpage.py ...
Compiling C:\Python24\Lib\idlelib\testcode.py ...
Compiling C:\Python24\Lib\idlelib\textView.py ...
Listing C:\WINDOWS\system32\python24.zip ...
Can't list C:\WINDOWS\system32\python24.zip
Listing C:\Python24 ...
Compiling C:\Python24\test1.py ...
Compiling C:\Python24\test2.py ...
Listing C:\Python24\DLLs ...
Listing C:\Python24\lib ...
Compiling C:\Python24\lib\BaseHTTPServer.py ...
Compiling C:\Python24\lib\Bastion.py ...
Compiling C:\Python24\lib\CGIHTTPServer.py ...
Compiling C:\Python24\lib\ConfigParser.py ...
Compiling C:\Python24\lib\Cookie.py ...
Compiling C:\Python24\lib\DocXMLRPCServer.py ...
Compiling C:\Python24\lib\HTMLParser.py ...
Compiling C:\Python24\lib\MimeWriter.py ...
Compiling C:\Python24\lib\Queue.py ...
Compiling C:\Python24\lib\SimpleHTTPServer.py ...
Compiling C:\Python24\lib\SimpleXMLRPCServer.py ...
Compiling C:\Python24\lib\SocketServer.py ...
Compiling C:\Python24\lib\StringIO.py ...
Compiling C:\Python24\lib\UserDict.py ...
Compiling C:\Python24\lib\UserList.py ...
Compiling C:\Python24\lib\UserString.py ...
Compiling C:\Python24\lib\_LWPCookieJar.py ...
Compiling C:\Python24\lib\_MozillaCookieJar.py ...
Compiling C:\Python24\lib\__future__.py ...
Compiling C:\Python24\lib\__phello__.foo.py ...
Compiling C:\Python24\lib\_strptime.py ...
Compiling C:\Python24\lib\_threading_local.py ...
Compiling C:\Python24\lib\aifc.py ...
Compiling C:\Python24\lib\anydbm.py ...
Compiling C:\Python24\lib\asynchat.py ...
Compiling C:\Python24\lib\asyncore.py ...
Compiling C:\Python24\lib\atexit.py ...
Compiling C:\Python24\lib\audiodev.py ...
Compiling C:\Python24\lib\base64.py ...
Compiling C:\Python24\lib\bdb.py ...
Compiling C:\Python24\lib\binhex.py ...
Compiling C:\Python24\lib\bisect.py ...
Compiling C:\Python24\lib\calendar.py ...
Compiling C:\Python24\lib\cgi.py ...
Compiling C:\Python24\lib\cgitb.py ...
Compiling C:\Python24\lib\chunk.py ...
Compiling C:\Python24\lib\cmd.py ...
Compiling C:\Python24\lib\code.py ...
Compiling C:\Python24\lib\codecs.py ...
Compiling C:\Python24\lib\codeop.py ...
Compiling C:\Python24\lib\colorsys.py ...
Compiling C:\Python24\lib\commands.py ...
Compiling C:\Python24\lib\compileall.py ...
Compiling C:\Python24\lib\cookielib.py ...
Compiling C:\Python24\lib\copy.py ...
Compiling C:\Python24\lib\copy_reg.py ...
Compiling C:\Python24\lib\csv.py ...
Compiling C:\Python24\lib\dbhash.py ...
Compiling C:\Python24\lib\decimal.py ...
Compiling C:\Python24\lib\difflib.py ...
Compiling C:\Python24\lib\dircache.py ...
Compiling C:\Python24\lib\dis.py ...
Compiling C:\Python24\lib\doctest.py ...
Compiling C:\Python24\lib\dumbdbm.py ...
Compiling C:\Python24\lib\dummy_thread.py ...
Compiling C:\Python24\lib\dummy_threading.py ...
Compiling C:\Python24\lib\filecmp.py ...
Compiling C:\Python24\lib\fileinput.py ...
Compiling C:\Python24\lib\fnmatch.py ...
Compiling C:\Python24\lib\formatter.py ...
Compiling C:\Python24\lib\fpformat.py ...
Compiling C:\Python24\lib\ftplib.py ...
Compiling C:\Python24\lib\getopt.py ...
Compiling C:\Python24\lib\getpass.py ...
Compiling C:\Python24\lib\gettext.py ...
Compiling C:\Python24\lib\glob.py ...
Compiling C:\Python24\lib\gopherlib.py ...
Compiling C:\Python24\lib\gzip.py ...
Compiling C:\Python24\lib\heapq.py ...
Compiling C:\Python24\lib\hmac.py ...
Compiling C:\Python24\lib\htmlentitydefs.py ...
Compiling C:\Python24\lib\htmllib.py ...
Compiling C:\Python24\lib\httplib.py ...
Compiling C:\Python24\lib\ihooks.py ...
Compiling C:\Python24\lib\imaplib.py ...
Compiling C:\Python24\lib\imghdr.py ...
Compiling C:\Python24\lib\imputil.py ...
Compiling C:\Python24\lib\inspect.py ...
Compiling C:\Python24\lib\keyword.py ...
Compiling C:\Python24\lib\linecache.py ...
Compiling C:\Python24\lib\locale.py ...
Compiling C:\Python24\lib\macpath.py ...
Compiling C:\Python24\lib\macurl2path.py ...
Compiling C:\Python24\lib\mailbox.py ...
Compiling C:\Python24\lib\mailcap.py ...
Compiling C:\Python24\lib\markupbase.py ...
Compiling C:\Python24\lib\mhlib.py ...
Compiling C:\Python24\lib\mimetools.py ...
Compiling C:\Python24\lib\mimetypes.py ...
Compiling C:\Python24\lib\mimify.py ...
Compiling C:\Python24\lib\modulefinder.py ...
Compiling C:\Python24\lib\multifile.py ...
Compiling C:\Python24\lib\mutex.py ...
Compiling C:\Python24\lib\netrc.py ...
Compiling C:\Python24\lib\new.py ...
Compiling C:\Python24\lib\nntplib.py ...
Compiling C:\Python24\lib\ntpath.py ...
Compiling C:\Python24\lib\nturl2path.py ...
Compiling C:\Python24\lib\opcode.py ...
Compiling C:\Python24\lib\optparse.py ...
Compiling C:\Python24\lib\os.py ...
Compiling C:\Python24\lib\os2emxpath.py ...
Compiling C:\Python24\lib\pdb.py ...
Compiling C:\Python24\lib\pickle.py ...
Compiling C:\Python24\lib\pickletools.py ...
Compiling C:\Python24\lib\pipes.py ...
Compiling C:\Python24\lib\pkgutil.py ...
Compiling C:\Python24\lib\platform.py ...
Compiling C:\Python24\lib\popen2.py ...
Compiling C:\Python24\lib\poplib.py ...
Compiling C:\Python24\lib\posixfile.py ...
Compiling C:\Python24\lib\posixpath.py ...
Compiling C:\Python24\lib\pprint.py ...
Compiling C:\Python24\lib\profile.py ...
Compiling C:\Python24\lib\pstats.py ...
Compiling C:\Python24\lib\pty.py ...
Compiling C:\Python24\lib\py_compile.py ...
Compiling C:\Python24\lib\pyclbr.py ...
Compiling C:\Python24\lib\pydoc.py ...
Compiling C:\Python24\lib\quopri.py ...
Compiling C:\Python24\lib\random.py ...
Compiling C:\Python24\lib\re.py ...
Compiling C:\Python24\lib\reconvert.py ...
Compiling C:\Python24\lib\regex_syntax.py ...
Compiling C:\Python24\lib\regsub.py ...
Compiling C:\Python24\lib\repr.py ...
Compiling C:\Python24\lib\rexec.py ...
Compiling C:\Python24\lib\rfc822.py ...
Compiling C:\Python24\lib\rlcompleter.py ...
Compiling C:\Python24\lib\robotparser.py ...
Compiling C:\Python24\lib\sched.py ...
Compiling C:\Python24\lib\sets.py ...
Compiling C:\Python24\lib\sgmllib.py ...
Compiling C:\Python24\lib\shelve.py ...
Compiling C:\Python24\lib\shlex.py ...
Compiling C:\Python24\lib\shutil.py ...
Compiling C:\Python24\lib\site.py ...
Compiling C:\Python24\lib\smtpd.py ...
Compiling C:\Python24\lib\smtplib.py ...
Compiling C:\Python24\lib\sndhdr.py ...
Compiling C:\Python24\lib\socket.py ...
Compiling C:\Python24\lib\sre.py ...
Compiling C:\Python24\lib\sre_compile.py ...
Compiling C:\Python24\lib\sre_constants.py ...
Compiling C:\Python24\lib\sre_parse.py ...
Compiling C:\Python24\lib\stat.py ...
Compiling C:\Python24\lib\statcache.py ...
Compiling C:\Python24\lib\statvfs.py ...
Compiling C:\Python24\lib\string.py ...
Compiling C:\Python24\lib\stringold.py ...
Compiling C:\Python24\lib\stringprep.py ...
Compiling C:\Python24\lib\subprocess.py ...
Compiling C:\Python24\lib\sunau.py ...
Compiling C:\Python24\lib\sunaudio.py ...
Compiling C:\Python24\lib\symbol.py ...
Compiling C:\Python24\lib\symtable.py ...
Compiling C:\Python24\lib\tabnanny.py ...
Compiling C:\Python24\lib\tarfile.py ...
Compiling C:\Python24\lib\telnetlib.py ...
Compiling C:\Python24\lib\tempfile.py ...
Compiling C:\Python24\lib\textwrap.py ...
Compiling C:\Python24\lib\this.py ...
Compiling C:\Python24\lib\threading.py ...
Compiling C:\Python24\lib\timeit.py ...
Compiling C:\Python24\lib\toaiff.py ...
Compiling C:\Python24\lib\token.py ...
Compiling C:\Python24\lib\tokenize.py ...
Compiling C:\Python24\lib\trace.py ...
Compiling C:\Python24\lib\traceback.py ...
Compiling C:\Python24\lib\tty.py ...
Compiling C:\Python24\lib\types.py ...
Compiling C:\Python24\lib\tzparse.py ...
Compiling C:\Python24\lib\unittest.py ...
Compiling C:\Python24\lib\urllib.py ...
Compiling C:\Python24\lib\urllib2.py ...
Compiling C:\Python24\lib\urlparse.py ...
Compiling C:\Python24\lib\user.py ...
Compiling C:\Python24\lib\uu.py ...
Compiling C:\Python24\lib\warnings.py ...
Compiling C:\Python24\lib\wave.py ...
Compiling C:\Python24\lib\weakref.py ...
Compiling C:\Python24\lib\webbrowser.py ...
Compiling C:\Python24\lib\whichdb.py ...
Compiling C:\Python24\lib\whrandom.py ...
Compiling C:\Python24\lib\xdrlib.py ...
Compiling C:\Python24\lib\xmllib.py ...
Compiling C:\Python24\lib\xmlrpclib.py ...
Compiling C:\Python24\lib\zipfile.py ...
Listing C:\Python24\lib\plat-win ...
Can't list C:\Python24\lib\plat-win
Listing C:\Python24\lib\lib-tk ...
Compiling C:\Python24\lib\lib-tk\Canvas.py ...
Compiling C:\Python24\lib\lib-tk\Dialog.py ...
Compiling C:\Python24\lib\lib-tk\FileDialog.py ...
Compiling C:\Python24\lib\lib-tk\FixTk.py ...
Compiling C:\Python24\lib\lib-tk\ScrolledText.py ...
Compiling C:\Python24\lib\lib-tk\SimpleDialog.py ...
Compiling C:\Python24\lib\lib-tk\Tix.py ...
Compiling C:\Python24\lib\lib-tk\Tkconstants.py ...
Compiling C:\Python24\lib\lib-tk\Tkdnd.py ...
Compiling C:\Python24\lib\lib-tk\Tkinter.py ...
Compiling C:\Python24\lib\lib-tk\tkColorChooser.py ...
Compiling C:\Python24\lib\lib-tk\tkCommonDialog.py ...
Compiling C:\Python24\lib\lib-tk\tkFileDialog.py ...
Compiling C:\Python24\lib\lib-tk\tkFont.py ...
Compiling C:\Python24\lib\lib-tk\tkMessageBox.py ...
Compiling C:\Python24\lib\lib-tk\tkSimpleDialog.py ...
Compiling C:\Python24\lib\lib-tk\turtle.py ...
Listing C:\Python24\lib\site-packages ...
4

I wouldn't have minded so much if I had gotten "5"!

:p

Jeff

OK ... even weirder: I can *delete* test1.pyc and re-run test2, and I *still* get 4 instead of 5.

Hmm..

And THEN, I can delete it again, re-run test2, and look at the properties of test1.pyc: It says that it was created at 6:18PM (i.e., a half-hour ago), even though it should be created at the current time (6:47, which is indeed the modification date)! Does Python drag .pyc files out of the Recycle Bin?!

Jeff

I am on Windows XP and see this behaviour ...

# save as test1.py

def func1():
    """change the value here"""
    return 7

# test as module
if __name__ == '__main__':
    print "test =", func1()

.. this is my test program ...

# test program for test1.py/test1.pyc

import py_compile

import test1

print "import =", test1.func1()

# force compile to test1.pyc file
py_compile.compile("test1.py")

# this makes it update the changed module test1.pyc
reload(test1)

print "reload =", test1.func1()

"""
after test1 function has been changed to return 77:
import = 7
reload = 77

running the test prgram the second time:
import = 77
reload = 77    
"""

Compiling test1.py to test1.pyc and then reloading it seems to update the changed return value. Running the test program twice catches the changed test1.pyc file even with import. A little strange!

Right; reload() has no problem as long as you are doing

import test1

rather than

from test1 import *

*sigh*

Jeff

I think it depends on the version of Windows XP. I can repeat vegaseat's observation on my folks old desktop computer, but import uses the updated pyc file right away on a friend's brandnew laptop with the latest version of Windows XP.

Does anyone now how to display the version of Windows XP?

Note: Also found out that the module name and the file name have to absolutely match (case sensitive).

This question has already been answered. Start a new discussion instead.