imp.loadmodule creates compiled file

Neil Hodgson nhodgson at bigpond.net.au
Wed Jun 19 09:17:19 EDT 2002


   I've been using imp.loadmodule with Python 2.2 (on both Windows and
Linux) to load changed versions of source code into a long running process
where they will run alongside objects already created from earlier versions
of the source code. An unexpected result of the call is to create a compiled
module on disk with a name based on the filename argument with "c" appended.

>>> f = file("x.py")
>>> import imp
>>> imp.load_module("y", f, "z", (".py", "r", 1))
<module 'y' from 'z'>

   At this point, a "zc" file is created. I would prefer no compiled file be
created. The documentation indicates that file and filename arguments can be
None and '' when the module is not being loaded from a file but this makes
litle sense as there is no other argument to provide the module contents
from other than a file. Trying '' as the filename argument leads to a
compiled file called "c". The compiled files can be unlinked but it would be
better to have a solution that avoided writing to disk at all as there may
be problems such as name clashes with other running instances or with being
in a protected directory.

   Any other techniques for creating mdoules from source code?

   Neil





More information about the Python-list mailing list