Refined: How do I launch 'somewhere/Sweety.py' without 'somewhere/'?

Martin Bless m.bless at gmx.de
Tue Apr 10 16:54:39 EDT 2001


Thanks, that's - almost - the kind of thing I'm looking for. There
only was a minor typo.

Alex Martelli:

>For example through a simple auxiliary script -- the
>following should suffice (but it's untested):
>
>file locator.py:
>===
>import sys, imp
>
>file, path, desc = imp.find_module(sys.argv[1])
>sys.argv[0] = path
>del sys.argv[1]
>try:
>    load_module('__main__', file, path, desc)
imp. load_module('__main__', file, path, desc)    # imp.  added, (mb)
>finally:
>    file.close()

>Of course, this will only be necessary on sufficiently-incapable
>operating systems.

 :-)  Yes, I'm aware of this. But currently I'm working in a windows
world.

I can imagine you wonder what I'm struggling for. I simply want to be
able to say "python mygeneralcmd" in each and every dir with
"mygeneralcmd" being python scripts somewhere in my import hierarchy.
And I have to keep Windows PATH short.

Locator.py is fine and - what I like very much - instructive to me.

But what I really need is a locator which can handle nested modules as
well like "mb.mytool". I tried to understand KNEE.PY which seems to
show the logic of importing, but my knowledge is still very limited.
Can I use KNEE.py to import ("chain to") for instance "mb.mblib"? And
will the __name__=="__main__" trick work?

Martin




More information about the Python-list mailing list