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

Alex Martelli aleaxit at yahoo.com
Sun Apr 8 10:20:33 EDT 2001


"Martin Bless" <m.bless at gmx.de> wrote in message
news:3ad04333.273637 at news.muenster.de...
    [snip]
> How can I launch scripts that lie in different subdirs in my import
> paths from an arbitrary directory without having to specify the path
> of the script and without changing the directory?

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)
finally:
    file.close()
===

You can use this explicitly,
    python locator.py themodule its args
or play some site.py trickery to have such kind of
functionality executed automatically for you.


Of course, this will only be necessary on sufficiently-incapable
operating systems.  On any Unix variant, scripts in your PATH
which are marked as executable will be found on your behalf
by the shell (and they'll start with #!/usr/bin/python, or
whatever, to have the Python interpreter invoked) -- this, I
believe, should also work on Windows if you're using some kind
of halfway-decent shell (such as bash from CygWin, or a
recent-enough version of Microsoft's cmd.exe -- but in the
latter case some registry trickery will also be needed).


Alex






More information about the Python-list mailing list