[Python-Dev] Importing packages from command line

Alex Martelli aleaxit at yahoo.com
Tue Dec 23 04:05:41 EST 2003


On Tuesday 23 December 2003 12:09 pm, Dmitry Vasiliev wrote:
> Alex Martelli wrote:
> >>The main idea is to treating package as a program and run package
> >>initialization code from command line. The advantage is zipping all
> >>program modules in one zip archive and running the program from command
> >>line without unzipping it, like Java's jar's. But this idea need more
> >>thoughts however...
> >
> > Couldn't you use:
> >     python -c "import package_name"
> > for this purpose even today?
>
> python -c "import sys; sys.path.insert(0, 'module.zip'); import module"
>
> Seems ugly... :)

Well, if the zipfile isn't on your sys.path already, then you'd have to insert
it explicitly anyway -- surely, even if a switch "-p" to mean import existed,
you wouldn't want it to force python to snoop into EVERY zipfile around?!

PYTHONPATH=module.zip python -c "import module"

is one way you might express "insert into path and import" using a decent
shell (cygwin's bash on Windows, for example).  The proposed:

PYTHONPATH=module.zip python -p module

doesn't appear to offer a _major_ enhancement, just a very minor one,
in my personal opinion.


One potential advantage of -p might be to let it be present _together_
with one -c (even better might be to allow multiple -c, actually).  If you
want to run a "while foo.bep(): foo.zlup()" you can do it today only with
a shell that allows easy input of one multiline argument.  But again it
seems a rather marginal issue to me, personally.


Alex





More information about the Python-Dev mailing list