Question about import and sys.path



Hi all

I am writing a business/accounting application. Once a user has logged
in they are presented with a menu. Each menu option has a description
and an associated file name and program name. The file name is the name
of a .py file (impName) and the program name is the name of a class in
that file which I instantiate to run the program (progName).

When a menu option is selected, I execute the program like this -
imp = __import__(impName)
app = getattr(imp,progName)()

All the .py files are stored in one directory, which I add to sys.path
at the beginning of the session. It all seems to work fine.

Now my program directory is getting cluttered, so I want to split it
into sub-directories, one per company (it is a multi-company system). I
can do that, and each of the subdirectories can be added to sys.path,
so it should work as at present.

However, I want the ability to have duplicate program names stored in
different subdirectories. At the time of selecting the menu option I
know which company is active, so I know which directory I want to run
the program from, but there does not seem to be a way to tell 'import'
to import from a particular directory. I could manipulate sys.path and
ensure that the correct company subdirectory is in the first position,
but that idea does not appeal to me. Maybe it is a good idea though - I
am open to suggestion.

Would 'execfile' be a good alternative? From what I can understand of
the docs it is almost equivalent, but I am not sure if there are any
implications. Here are some questions -

1. Does execfile create a .pyc file from a .py file, or does it compile
the contents of the file every time it is executed? If the latter, that
could create a performance problem.

2. The docs say 'it does not use the module administration -- it reads
the file unconditionally and does not create a new module'. What does
that mean? The main implication I can think of is that if the same
program is executed more than once in a given session, 'import' would
realise the second time that the module has already been imported, and
would not import it again, whereas 'execfile' would re-execute the file
every time. That could also create a performance hit, but I don't think
it would be too serious.

3. I assume that once a module has been imported, it stays in memory
for the life of the interpreter session. What happens with execfile?
Does it get garbage-collected after execution is complete?

4. Are there any other implications I should know about? I did read the
warning about modifying locals, but I don't do anything like that.

Thanks for any advice

Frank Millman

.



Relevant Pages

  • Re: Is there a way to speed up Index Fast Full Scan
    ... when processing those slow SQL statements. ... session by checking V$SESSION. ... execute one of the slow ... then have the session execute the slow SQL statements. ...
    (comp.databases.oracle.server)
  • Re: Is there a way to speed up Index Fast Full Scan
    ... when processing those slow SQL statements. ... session by checking V$SESSION. ... execute one of the slow ... then have the session execute the slow SQL statements. ...
    (comp.databases.oracle.server)
  • Re: CSS implication
    ... the author of the script does anything like this: ... Subject: CSS implication ... you're able to execute code on the web. ... I've thought about the implications ...
    (Vuln-Dev)
  • Re: VBScript with Sessionid and RDP
    ... execute the following commands at a Command Prompt in order to examine its ... If you get no output then there you must run this command so that you can ... Remote Desktop Session), only RD Session instance is killed. ... message - how about starting the batch file with "echo on"? ...
    (microsoft.public.scripting.vbscript)
  • Re: HTTPContext Session and worker thread (Fire and Forget)
    ... To kick of the job you insert record into tblJobs with MessageIn having ... Updates Session if job ... not execute job cause it was grabbed by other pooling thread between step 1 ...
    (microsoft.public.dotnet.framework.aspnet)