imp.loadmodule creates compiled file

Phlip phlip_cpp at yahoo.com
Wed Jun 19 10:13:00 EDT 2002


Neil Hodgson wrote:

>    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?

PyUnitTestBrowser uses find_module then load_module; it appears to only 
create pyc files.

    folder, mod = path.split(filePath)    
    mod = mod[:-3]   
    got = imp.find_module(mod, [folder])
    module = apply(imp.load_module, [mod] + list(got))

http://c2.com/cgi/wiki?PyUnitTestBrowser

-- 
  Phlip
          http://www.greencheese.org/ParodyMode
  --  Because I'm the sysadmin. That's why.  --




More information about the Python-list mailing list